ITeye博客

Spring

共 44 篇文章

Spring IOC registerBeanDefinition

IOC 是Spring的核心,我们以编程方式使用DefaultListableBeanFactory,尽管在现实的编码过程中几乎不会这种原始的方式,但是作为了解Spring IOC 实现还是很有必要的。 //创建IOC配置的抽象资源 ClassPathResource resource=new ClassPathResource("client.xml"); //创建一个BeanFactory DefaultListableBeanFactory beanFactory=new DefaultListableBeanFactory(); //创建一个BeanDefinition读区器 Xml…

2016-03-20 12:23 · Spring

Spring MVC之HandlerMap 初始化(RequestmappingHandlerMapping)

AbstractHandlerMethodMapping类实现了InitializingBean接口,在属性初始化完成后会调用afterPropertiesSet()方法,在该方法中调用initHandlerMethods();进行HandlerMethod初始化。 /** * 扫描ApplicationContext中的Bean,查找并注册 handlerMethod */ protected void initHandlerMethods() { if (logger.isDebugEnabled()) { logger.debug("Looking for request mappin…

2016-03-18 14:07 · Spring

Spring 事务处理(二)源码分析

Spring事物的配置样式如下: <bean id="baseTransactionProxy" class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean" abstract="true"> <property name="transactionManager" ref="transactionManager"/> <property name="transactionAttributes"> <props> <prop key="insert*">PROPAGATION_REQUIRED</…

2016-03-05 00:12 · Spring · Spring · 事物

Spring 基于事件的通信

Spring 应用上下文支持基于事件的Bean间通信。在基于事件的通信模式中,事件的发送者不需要关系,事件的监听者。这样可以使消息的发送者和监听者进行解耦。 在Spring中所有事件类必须继承自ApplicationEvent,这样任何bean都可以调用事件发布者的publishEvent()方法,发布一个事件。 public class MyEvent extends ApplicationEvent { /** */ private static final long serialVersionUID = 1L; /** * @param source */ public MyEvent…

2016-01-29 10:14 · Spring

Spring aop的多种配置方式以及源码分析

profile是Spring3.1提供的一个新的配置项,在下面的测试示例中,又有使用了多种AOP配置方式,为了将各种配置方式进行对比在此使用了profile.在测试用例中通过使用@ActiveProfiles("four")注解指定profile的值。 Aop 是Spring 框架的核心功能之一。 Advice 通知时定义在该连接点做什么,为切面增强提供织入接口。Advice是AOP联盟定义的一个接口,在Spring中对其进行了扩展,提供了更为具体的接口如:BeforeAdvice,AfterAdvice,ThrwosAdvice等。 Pointcut切点,决定advice通知应该作用于哪个…

2015-07-10 14:01 · Spring

BeanDefinitionParserDelegate 源码分析

public class BeanDefinitionParserDelegate { public static final String BEANS_NAMESPACE_URI = "http://www.springframework.org/schema/beans"; public static final String MULTI_VALUE_ATTRIBUTE_DELIMITERS = ",; "; /** @deprecated as of Spring 3.1 in favor of {@link #MULTI_VALUE_ATTRIBUTE_DELIMITERS} */ …

2015-07-09 16:57 · Spring

Spring lookup-method,replace-method,factory-method使用示例

在这里会介绍如下知识点: factory-method factory-bean lookup-method replace-method factory-method 在Spring中如果对一个bean的创建使用工厂方法模式就可以使用factory-method /** * factory-method demo * @author zhangwei_david * @version $Id: FactoryMethodDemo.java, v 0.1 2015年7月8日 下午11:03:58 zhangwei_david Exp $ */ public class FactoryMeth…

2015-07-08 23:10 · Spring

XmlWebApplicationContext源码学习(一)

在Spring web项目中XmlWebApplicationContext是如何创建的?首先在web.xml中我们可以看到如下配置 <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath*:META-INF/spring/*.xml</param-value> </context-param> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-c…

2015-07-01 22:18 · Spring

DispatchServlet源码分析

DispatcheServlet类图,我们根据其类图进行源码分析 GenericServlet 源码分析 /** * 定义一个通用的,协议无关的Servlet.如果需要编写一个在Web中使用的Http Severlet需要继承HttpServlet * GeneraicServlet实现了Servlet接口和ServletConfig接口。GenericServlet *可以直接通过Servlet扩展,虽然这是比较常见的一种是继承特殊协议的子类,如HttpServlet * GenericServlet使写Servlet变的更容易。它提供简单的版本的生命周期方法 init,destory和S…

2015-06-28 10:49 · Spring

Spring中使用自定义的注解校验器的实现

首先先学习一下注解,注解为我们在代码中添加信息提供了一种形式化的方法,使得我们在稍后的某个时刻可以方便地使用这些数据。 在日常的编码中我们一直都在使用注解,只是没有特别关注过,Java中内置了三种注解:@Override,@SuppressWarnings @Deprecated。相信只要学习过Java的同学一定是见过这些主角的 。 如果我们要写一个自定义的注解应该怎么呢? 首先需要定义一个注解标注出是自定义的注解 /** * * @author zhangwei_david * @version $Id: CustomerRule.java, v 0.1 2015年5月29日 下午10:1…

2015-05-31 12:21 · Spring

Spring MVC之DispatcherServlet请求处理(二)

回顾一下DispatcherServlet中doDispatch的处理流程 /** * Process the actual dispatching to the handler. * <p>The handler will be obtained by applying the servlet's HandlerMappings in order. * The HandlerAdapter will be obtained by querying the servlet's installed HandlerAdapters * to find the first that support…

2015-05-26 23:44 · Spring

Spring 之常用接口

1.ApplicationContextAware 任何期望在ApplicationContext运行的时候被通知到都可以实现该接口 /** * 测试Spring ApplicationContextAware接口 * @author zhangwei_david * @version $Id: TestApplicationContextAware.java, v 0.1 2015年1月3日 上午11:42:19 zhangwei_david Exp $ */ @Component public class TestApplicationContextAware implements Ap…

2015-01-03 12:20 · Spring · Spring

Spring MVC之视图呈现

在Spring MVC DispatcherServlet.doDispatch中请求在经过handler处理返回一个ModelAndView. 那么ModelAndView又是如何最终转换为一个具体的View的呢?下面就对视图呈现部分作出一些简介。 视图的渲染是处理一个请求的最后阶段: /** 根据ModelAndView中的视图名称进行解析,得到具体的视图对象 * Render the given ModelAndView. * <p>This is the last stage in handling a request. It may involve resolving the vi…

2015-01-02 20:07 · Spring · spring mvc

Spring MVC之HandlerMap 初始化

DispatcherServlet请求处理请求的过程中,会发现getHandler实际上是调用AbstractUrlHandlerMapping.getHandlerInternal()。 通过对该段代码进行走读后发现,是通过handlermap.get(urlPath)获取匹配的handler的,那么该handlerMap是在什么时候进行初始化的呢? 通过DefaultAnnotationHandlerMapping的继承关系可以去分析初始化的过程。 通过继承关系可以知道DefaultAnnotationHandlerMapping实现了ApplicationContextAware接口,…

2015-01-01 21:56 · Spring · spring mvc

Spring MVC之DispatcherServlet请求处理

处理请求是MVC中的C(Control)部分,他是MVC的核心内容。在初始化上下文是将所有的HandlerMapping都加载到handlerMappings中且根据Order排序。每个HandlerMapping 都持有URL到Controller的映射关系。 在MVC初始化完成之后,对Http请求的处理是在doService()方法中完成的。DispatcherServlet是HttptServlet的子类,对Http请求的处理是在doService()方法中完成的。 在初始化完成时,在上下文中配置的所有HandlerMapping都已经被加载了,( HandlerMap初始化) 并被排…

2014-12-29 10:05 · Spring · spring mvc

Spring MVC之DispatcherServlet初始化

使用Spring MVC 时,需要在web.xml中配置DispatchServlet,这个DispatchServlet可以看成一个控制器的具体实现。作为一个控制器所有的请求都要通过它来处理,进行转发、匹配、数据处理后并转由页面进行展示。因此DispatchServlet是Spring MVC的核心部分。 在完成ContextLoaderListener的初始化后,Web容器开始初始化DispatcherServlet,这个初始化的启动与在web.xml中对载入次序的定义有关。DispathcerServlet会建立自己的上下文来持有Spring MVC的Bean,在建立这个自己的IOC容…

2014-12-29 09:27 · Spring

Spring 中的事务处理

Spring 支持编程式事务和声明是事务处理。 编程式事务管理通过在业务方法中嵌入控制事务提交和回滚的事务管理代码来实现。 声明式事务管理时通过AOP框架支持的。 Spring 核心事务管理抽象基于PlatformTransactionManager接口。它封装了一组用户事务管理的技术独立方法。TransactionStatus getTransaction(TransactionDefinition definition) throws TransactionException; void commit(TransactionStatus status) throws Transactio…

2014-12-21 20:56 · Spring · Spring · transaction

Spring mvc 对Controller配置AOP

希望对所有Controller 的请求参数都统一做一个日志记录,如果没有controller都需要手工去写的化,不能保证格式统一也不能保证不会被遗漏! 如果在配置文件中仅仅配置: <aop:aspectj-autoproxy /> 则会发现AOP的配置对controller 根本不起作用! 是因为 proxy-target-class 默认是false ,无法对未实现接口的类进行代理,而controller ,但是如果将proxy-target-class="true"又会因为 这个配置导致启动报错!因 <aop:aspectj-autoproxy proxy-target-class="t…

2014-12-15 11:51 · Spring · MVC · Spring · spring mvc

ContentNegotiatingViewResolver 学习

ContentNegotiatingViewResolver视图解析器是Spring MVC 中常用的一个视图解析器。 这个实现了ViewResolver接口,基于请求文件和Accept 头部信息。ContentNagotiatingViewResolver自己并不解析视图,而是委派给其他的视图处理器。 回过头来,哪些其他的视图解析器 是自动地从应用上下文中挑选出来的,虽然它可以通过viewResolvers属性配置。为了使这个解析器正常工作,序号需要设置成比其他的视图处理器高的优先级。这个视图处理器使用必要的媒体类型为一个请求去选择一个合适的视图。媒体类型通过如下的标准决定: 如果请求路径…

2014-12-02 22:43 · Spring

Spring Aop 切点表达式

在Spring AOP 中,通常需要借助AspectJ 的切点表达式语言来定义切点。重要的是Spring 中仅支持AspectJ切点指示器的一个子集。 Spring 支持的AspectJ的切点指示器 AspectJ 指示器 描述 args() 限制连接点匹配参数为执行类型的执行方法 @args() 限制连接点匹配参数由执行注解标注的执行方法 execution() 匹配连接点的执行方法 this() 限制连接点匹配AOP代理的Bean引用类型为指定类型的Bean target() 限制连接点匹配目标对象为指定类型的类 @target() 限制连接点匹配目标对象被指定的注解标注的类 withi…

2014-12-01 11:43 · Spring · Spring · AOP · pointcut