FIRST THING FIRST
虽然说是笔记 但是和 拷整本书没有区别了, 谢谢作者. 请各位不要打我
购买链接: Spring 揭秘
IOC
IOC Inversion of Control
IOC 的三种注入方式
掌管大局的IOC Service Provider
IOC Service Provider 的职责
- 业务对象的构建管理
- 业务对象间的依赖绑定
注册对象管理信息的方式
直接编码方式
(伪代码)1
2
3
4IOCContainer container = ;
container.register(A.class, new A());
container.register(B.class, new B());
A a = (A)container.get(A.class);配置文件方式
1
2
3
4
5
6
7
8
9
10
11
12<beans>
<bean id = "newsProvider" class = "···.FXNewsProvider">
<property name = "newsListner">
<ref bean = "djNewsListener"/>
</property>
<property name = "newPersistener">
<ref bean = "djNewsPersister"/>
</property>
<bean id = "djNewsListener" class = "..Impl.DowJonesNewsListener"/>
<bean id = "nesPersistener" class= "...Impl.DowJonesNewsPersister"/>
</bean>
</beans>1
2container.readConfigurationFiles(...);
FXNewsProvider newsProvider = (FXNewsProvider)container.getBean("newsProvider");元数据方式
1
2
3
4
5
6public class FxNewsProvider {
private IFXNewsListener newsListener;
private IFXNewsPersister newPersistener;
}Spring的IoC 容器之BeanFactory
Spring 提供了两种容器类型 - BeanFactory
基础类型IoC容器,提供完整的IoC服务支持。如果没有特殊指定,默认采用延迟初始化策略(lazy-load)。只有当客户端对象需要访问容器中的某个受管对象的时候,才对 该受管对象进行初始化以及依赖注入操作。所以,相对来说,容器启动初期速度较快,所需 要的资源有限。对于资源有限,并且功能要求不是很严格的场景,BeanFactory是比较合适的 IoC容器选择。 - ApplicationContext
ApplicationContext在BeanFactory的基础上构建,是相对比较高 级的容器实现,除了拥有BeanFactory的所有支持,ApplicationContext还提供了其他高级
特性,比如事件发布、国际化信息支持等,这些会在后面详述。ApplicationContext所管理 的对象,在该类型容器启动之后,默认全部初始化并绑定完成。所以,相对于BeanFactory来 说,ApplicationContext要求更多的系统资源,同时,因为在启动时就完成所有初始化,容 器启动时间较之BeanFactory也会长一些。在那些系统资源充足,并且要求更多功能的场景中, ApplicationContext类型的容器是比较合适的选择。
BeanFactory
BeanFactory 是个接口 实现这个接口的类能从Ioc Service Container 中拿到相关的类
1 | public interface BeanFactory { |
有了BeanFactory 容器之后
1 | BeanFactory container = new XmlBeanFactory(new ClassPathResource("")); |
or
1 | ApplicationContext container = new ClassPathXmlApplicationContext("配置文件路径"); |
How does it work
1 | public static void main(String[] args) { |
打个比方说,BeanDefinitionRegistry就像图书馆的书架,所有的书是放在书架上的。虽然你 还书或者借书都是跟图书馆(也就是BeanFactory,或许BookFactory可能更好些)打交道,但书架才 是图书馆存放各类图书的地方。所以,书架相对于图书馆来说,就是它的“BookDefinitionRegistry”。
BeanFactory的XML之旅
Beans的属性
default-lazy-init
default-autowire
default-dependency-check
default-init-method
default-destory-method
如果Beans 标签下的所有Bean用了一毛一样的init和destory 方法 就可以用这样的方式进行指定
通过Xml 注入的方式生成Bean
1 | <bean id="djNewsProvider" class="..FXNewsProvider"> |
简化的方法
1 | <bean id="djNewsProvider" class="..FXNewsProvider"> |
指定参数的类型
1 | <bean id="mockBO" class="..MockBusinessObject"> |
指定构造函数的顺序
1 | <bean id="mockBO" class="..MockBusinessObject"> |
使用静态方法的方式
1 | <bean id="foo" class="...Foo"> |
1 | <bean id="foo" class="...Foo"> |
FactoryBean 登场.
对于不是静态方法的工厂
- 我们要 首先创建这个工厂
- 获得这个工厂创建的类
- 将这个类注入到被依赖的类中去
1
2
3
4
5public interface FactoryBean<T> {
T getObject() throws Exception;
Class<?> getObjectType();
boolean isSingleton();
}
但是 有了FactoryBean 就方便很多了,注入进被依赖对象的
1 | <bean id="nextDayDateDisplayer" class="...NextDayDateDisplayer"> |
常见的FactoryBean的实现
- JndiObjectFactoryBean
- LocalSessionFactoryBean
- SqlMapClientFactoryBean
- ProxyFactoryBean
- TransactionProxyFactoryBean
方法注入和方法替换
但是我们多次调用的时候会发现1
2
3
4
5
6<bean id = "newsBean" class = "..domain.FXNewsBean" scope = "prototype"/>
<bean id = "mockPersister" class = "..impl.MockNewsPersister">
<property name="newsBean">
<ref bean="newsBean"/>
</property>
</bean>输出结果1
2
3
4BeanFactory container = new XmlBeanFactory(new ClassPathResource(...));
MockNewsPersister persister = (MockNewsPersister) container.getBean("");
persister.persisterNews();
persister.persisterNews();
persist bean:..domain.FXNewsBean@1662dc8
persist bean:..domain.FXNewsBean@1662dc8
newsBean 是原型的. 当容器将一个FXNewsBean的实例注入 MockNewsPersister之后,MockNewsPersister就会一直持有这个FXNewsBean实例的引用(FXNewsBean 是singleton的?)
- 解决方法1要求 方法注入的样式要符合如下规定
1
2
3
4
5<bean id = "newsBean" class = "..domain.FXNewsBean" scope = "prototype"/>
<bean id = "mockPersister" class="..impl.MockNewsPersister">
<lookup-method name = "getNewsBean" bean = "newsBean"/>
</bean>
<public|protected> [abstract]theMethodName(no-arguments);
简而言之就是 要能被子类实现或者覆盖.
因为容器会为我们要进行方法注入的对象使用 Cglib动态生成一个子类实现,从而替代当前对象。
之后每次调用getNewsBean 的时候都会通过cglib 动态实现一个子类. - 解决方法2
MockNewsPersister 实现 BeanFactoryAware 接口使用ObejctFactoryCreatingFactoryBean or ServiceLocatorFactoryBean1
2
3
4
5
6
7
8
9
10
11
12
13
14
15public class MockNewsPersister implements IFXNewsPersister,BeanFactoryAware {
private BeanFactory beanFactory;
public void setBeanFactory(BeanFactory bf) throws BeansException{
this.beanFactory = bf;
}
public void persistNews(FXNewsBean bean) {
persistNews();
}
public void persistNews() {
System.out.println("persist bean:"+getNewsBean());
}
public FXNewsBean getNewsBean() {
return beanFactory.getBean("newsBean");
}
} - 方法替换
在讲这个之前 我们先看一下JDK提供的代理是怎么样的同样的在在Spring中实现一个MethodReplacer1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33public class Main {
public static void main(String[] args) throws NoSuchMethodException, IllegalAccessException, InvocationTargetException, InstantiationException {
Class<?> proxyClass = Proxy.getProxyClass(Main.class.getClassLoader(), Helloworld.class);
Constructor<?> cons = proxyClass.getConstructor(InvocationHandler.class);
MyInvocationHandler myInvocationHandler = new MyInvocationHandler(new HelloworldImpl());
Helloworld helloworld = (Helloworld)cons.newInstance(myInvocationHandler);
helloworld.sayHello();
}
}
interface Helloworld {
void sayHello();
}
class HelloworldImpl implements Helloworld {
public void sayHello() {
System.out.println("hello world");
}
}
class MyInvocationHandler implements InvocationHandler {
private Object object;
public MyInvocationHandler(Object object) {
this.object = object;
}
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
return method.invoke(object, args);
}
}1
2
3
4
5
6
7
8
9
10public class FXNewsProviderMethodReplacer implements MethodReplacer {
private static final transient Log logger = LogFactory.getLog(FXNewsProviderMethodReplacer.class);
public Object reimplement(Object target, Method method, Object[] args) throws Throwable {
logger.info("before executing method["+method.getName()+➥ "] on Object["+target.getClass().getName()+"].");
System.out.println("sorry,We will do nothing this time.");
logger.info("end of executing method["+method.getName()+➥ 15 "] on Object["+target.getClass().getName()+"].");
return null;
}
}1
2
3
4
5
6
7
8
9
10<bean id="djNewsProvider" class="..FXNewsProvider">
<constructor-arg index="0">
<ref bean="djNewsListener"/>
</constructor-arg>
<constructor-arg index="1">
<ref bean="djNewsPersister"/>
</constructor-arg>
<replaced-method name="getAndPersistNews" replacer="providerReplacer"/>
</bean>
<bean id="providerReplacer" class="..FXNewsProviderMethodReplacer"/>探索Spring Bean的生成
- 第一阶段
- 读取classpath 路径下的xml
- dom 分析
- 把bean的配置装备到(RootBeanDefinition, ChildBeanDefinition)
然后注册到 BeanDefinitionRegistry 中.
- 第二阶段
- 检查的请求的对象是否已经初始化, 没有的话进行组装. 如果有的话直接返回
BeanFactoryPostProcessor
该机制允许我们在容器实现实例话对应对象之前对BeanDefinition中所保存的信息做对应的修改. 比方说原来的bean里面放的是占位符etc
要自动实现BeanFactoryPostProcessor, 我们要实现BeanFactoryPostProcessor接口.同时要实现Ordered接口.
但是Spring 已经实现了几个现成的BeanFactoryPostProcess实现类.
- PropertyPlaceholderConfigurer
PropertyPlaceholerConfigurer 允许我们在XML中使用占位符(PlaceHoler ${jdbc.password}). 当BeanFactory在第一阶段加载完成所有的配置信息时, BeanFactory 中保存的是属性信息时${}这样的值保存的, 而PropertyPlaceholerConfigurer会使用配置文件中的信息来替换相应的BeanDefinition中占位符所表示的属性值.
PropertyPlaceholerConfigurer不单会从其配置的properties中加载配置项,还会检查Java的System类的Properties.可以通过配置来去掉这个行为. - PropertyOverrideConfigurer
PropertyOverrideConfigure 是直接就擦除掉了.
配置的规则时
beanName.properyName = value - CustomerEditorConfigurer
CustomerEditorConfigure 不会对BeanFactory中Definition做任何变化.它只是辅助性地将后期会用到的信息注册到容器,对BeanDefinition没有做任何变动。
在xml中记载的是spring类型,最终的应用程序是各种类型的对象.当中的转换,都需要某种转换规则相关,而CustomEditorConfigurer就是帮助我们传达类似信息的.Spring内部通过JavaBean的PropertyEditor来帮助进行String类型到其他类型的转换工作。- StringArrayPropertyEditor
PropertyEditor会将符合CSV格式的字符串转换成 String[]数组的形式,默认是以逗号(,)分隔的字符串,但可以指定自定义的字符串分隔符。 - ClassEditor
根据String类型的class名称,直接将其转换成相应的Class对象,相当于通 过Class.forName(String)完成的功效。 - FileEditor
- LocaleEditor
- PatternEditor
- StringArrayPropertyEditor
- 自定义PropertyEditor如果是BeanFactory的实现,需要手工编码应用到容器中
1
2
3
4
5
6
7
8
9
public class DataPropertyEditor extends PropertyEditorSupport {
private String datePattern;
public void setAsText(String text) throws IllegalArgumentException {
Date dataValue = new SimpleDateFormat.forPattern(datePattern).parse(text);
setValue(dateValue);
}
}如果是ApplicationContext, 会自动识别Bean1
2
3
4
5BeanFactory beanFactory = new XmlBeanFactory(new ClassPathResource(''...");
CustomerEditorConfigurer ceConfigurer = new CustomerEditorConfigurer();
Map<Class<?>, CustomerEdit> customerEditors = new HashMap();
customerEditors.put(java.util.Date.class,)
ceConfigurer.postProcessBeanFactory(beanFactory);1
<bean id = "dateFoo" class = "..."
1
2
3
4
5
6public class DataPropertyEditorRegistrar implements PropertyEditorRegistrar {
private PropertyEditor propertyEditor;
public void registerCustomEditors(PropertyEditorRegistry pRegistry) {
peRegistry.re
}
}
How does it work
对于BeanFactory的情况
1 | BeanFactory beanFactory = new XmlBeanFactory(new ClassPathResource("")); |
对于ApplicationContext 的情况
1 | <beans> |
Bean 的一生
容器启动之后,并不会马上就实例化相应的bean定义。我们知道,容器现在仅仅拥有所有对象的 BeanDefinition来保存实例化阶段将要用的必要信息。只有当请求方通过BeanFactory的getBean() 方法来请求某个对象实例的时候,才有可能触发Bean实例化阶段的活动。BeanFactory的getBean方 法可以被客户端对象显式调用,也可以在容器内部隐式地被调用。隐式调用有如下两种情况。
BeanFactroy
对于BeanFactory来说,对象实例化默认采用延迟初始化。通常情况下,当对象A被请求而需 要第一次实例化的时候,如果它所依赖的对象B之前同样没有被实例化,那么容器会先实例化 对象A所依赖的对象。这时容器内部就会首先实例化对象B,以及对象 A依赖的其他还没有被 实例化的对象。这种情况是容器内部调用getBean(),对于本次请求的请求方是隐式的。
Application
ApplicationContext在实现的过程中依然遵循Spring容器实现流程的两个阶段,只不过它 会在启动阶段的活动完成之后,紧接着调用注册到该容器的所有bean定义的实例化方法 getBean()。org.springframework.context.support. AbstractApplicationContext的refresh()方法。
实例化Bean
当 getBean()方法内部发现该bean定义之前还没有被实例化之后,会通过createBean()方法来进行具体 的对象实例化.
容器在内部实现的时候,采用“策略模式”来决定何种方式初始化bean实例.通过反射或者CGLIB动态字节码来初始化相应的bean实例或者动态子类.
org.springframework.beans.factory.support.InstantiationStrategy定义是实例化策略 的抽象接口,其直接子类SimpleInstantiationStrategy实现了简单的对象实例化功能,可以通过 反射来实例化对象实例,但不支持方法注入方式的对象实例化。CglibSubclassingInstantiation- Strategy继承了SimpleInstantiationStrategy的以反射方式实例化对象的功能,并且通过CGLIB 的动态字节码生成功能,该策略实现类可以动态生成某个类的子类,进而满足了方法注入所需的对象 实例化需求。默认情况下,容器内部采用的是CglibSubclassingInstantiationStrategy。通过策略模式生成的是以BeanWapper对构造完成的实例对象进行包裹,返回对应的BeanWapper.
BeanWrapper接口通常在Spring框架内部使用,它有一个实现类org.springframework.beans.BeanWrapperImpl。其作用是对某个bean进行“包裹”,然后对这个“包裹”的bean进行操作,比如设置或者获取bean的相应属性值。而在第一步结束后返回BeanWrapper实例而不是原先的对象实例, 就是为了第二步“设置对象属性”。BeanWrapper定义继承了org.springframework.beans.PropertyAccessor接口,可以以统一的 方式对对象属性进行访问;BeanWrapper定义同时又直接或者间接继承了PropertyEditorRegistry 和TypeConverter接口。不知你是否还记得CustomEditorConfigurer?当把各种PropertyEditor注 册给容器时,知道后面谁用到这些PropertyEditor吗?对,就是BeanWrapper!在第一步构造完成 对象之后,Spring会根据对象实例构造一个BeanWrapperImpl实例,然后将之前CustomEditor- Configurer注册的PropertyEditor复制一份给BeanWrapperImpl实例(这就是BeanWrapper同时又 是PropertyEditorRegistry的原因)。这样,当BeanWrapper转换类型、设置对象属性值时,就不 会无从下手了。
1 | Object provider = Class.forName("package.name.FXNewsProvider").newInstance(); |
1 | Object provider = Class.forName("package.name.FXNewsProvider").newInstance(); |
相关的aware接口
对于BeamFactory
- BeanNameAware
设置这个Bean的name - BeanClassLoaderAware
会加载当前Bean的ClassLoader 注入当前对象实例. 默认会使用Classloader. - BeanFactoryAware
如果对象声明实现了 BeanFactoryAware接口,BeanFactory容器会将自身设置到当前对象实例。这样,当前对象 实例就拥有了一个BeanFactory容器的引用,并且可以对这个容器内允许访问的对象按照需要 进行访问。对于ApplicationContext
- ResourceLoaderAware
获得当前Application已经被载入的的Resource - ApplicationEventPublisherAware
获得当前Application 的publish Event - MessageSourceAware
ApplicationContext通过Message- Source接口提供国际化的信息支持,即I18n(Internationalization)。通过这个接口获得MessageSourceAwareBeanPostProcess
和BeanFactoryPostProcessor之间的差别是BeanPostProcess 存在于对象实例化阶段, BeanFactoryPostProcessor存在在容器启动阶段.
通常比较常见的使用BeanPostProcessor的场景,是处理标记接口实现类,或者为当前对象提供 代理实现。ApplicationContext对应的那些Aware接口实际上就是通过BeanPostProcessor的方式进行处理的。
ApplicationContext对应的那些Aware接口实际上就是通过BeanPostProcessor的方式进行处理的。当ApplicationContext中每个对象的实例化过程走到BeanPostProcessor前置处理这一步时,ApplicationContext容器会检测到之前注册到容器的ApplicationContextAwareProcessor这个BeanPostProcessor的实现类,然后就会调用其postProcessBefore- Initialization()方法,检查并设置Aware相关依赖。1
2
3
4public interface BeanPostProcessor {
Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException;
Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException;
}除了检查标记接口以便应用自定义逻辑,还可以通过BeanPostProcessor对当前对象实例做更多 的处理。比如替换当前对象实例或者字节码增强当前对象实例等。Spring的AOP则更多地使用 BeanPostProcessor来为对象生成相应的代理对象,如org.springframework.aop.framework. autoproxy.BeanNameAutoProxyCreator。我们将在Spring AOP部分详细介绍该类和AOP相关概念。1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72class ApplicationContextAwareProcessor implements BeanPostProcessor {
private final ConfigurableApplicationContext applicationContext;
private final StringValueResolver embeddedValueResolver;
/**
* Create a new ApplicationContextAwareProcessor for the given context.
*/
public ApplicationContextAwareProcessor(ConfigurableApplicationContext applicationContext) {
this.applicationContext = applicationContext;
this.embeddedValueResolver = new EmbeddedValueResolver(applicationContext.getBeanFactory());
}
public Object postProcessBeforeInitialization(final Object bean, String beanName) throws BeansException {
AccessControlContext acc = null;
if (System.getSecurityManager() != null &&
(bean instanceof EnvironmentAware || bean instanceof EmbeddedValueResolverAware ||
bean instanceof ResourceLoaderAware || bean instanceof ApplicationEventPublisherAware ||
bean instanceof MessageSourceAware || bean instanceof ApplicationContextAware)) {
acc = this.applicationContext.getBeanFactory().getAccessControlContext();
}
if (acc != null) {
AccessController.doPrivileged(new PrivilegedAction<Object>() {
public Object run() {
invokeAwareInterfaces(bean);
return null;
}
}, acc);
}
else {
invokeAwareInterfaces(bean);
}
return bean;
}
private void invokeAwareInterfaces(Object bean) {
if (bean instanceof Aware) {
if (bean instanceof EnvironmentAware) {
((EnvironmentAware) bean).setEnvironment(this.applicationContext.getEnvironment());
}
if (bean instanceof EmbeddedValueResolverAware) {
((EmbeddedValueResolverAware) bean).setEmbeddedValueResolver(this.embeddedValueResolver);
}
if (bean instanceof ResourceLoaderAware) {
((ResourceLoaderAware) bean).setResourceLoader(this.applicationContext);
}
if (bean instanceof ApplicationEventPublisherAware) {
((ApplicationEventPublisherAware) bean).setApplicationEventPublisher(this.applicationContext);
}
if (bean instanceof MessageSourceAware) {
((MessageSourceAware) bean).setMessageSource(this.applicationContext);
}
if (bean instanceof ApplicationContextAware) {
((ApplicationContextAware) bean).setApplicationContext(this.applicationContext);
}
}
}
public Object postProcessAfterInitialization(Object bean, String beanName) {
return bean;
}
}
BeanPostProcessor是容器提供的对象实例化阶段的强有力的扩展点。为了进一步演示它的强大 威力,我们有必要实现一个自定义的BeanPostProcessor。1
2
3
4public interface PasswordDecodable {
String getEncodedPassword();
void setDecodedPassword(String password);
}实现相应的BeanPostProcessor对符合条件的Bean实例进行处理(应该是职责链模式)1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18public class DowJonesNewsListener implements IFXNewsListener,PasswordDecodable {
private String password;
public String[] getAvailableNewsIds() {
// 省略
}
public FXNewsBean getNewsByPK(String newsId) {
// 省略
}
public void postProcessIfNecessary(String newsId) {
// 省略
}
public String getEncodedPassword() {
return this.password;
}
public void setDecodedPassword(String password) {
this.password = password;
}
}1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17public class PasswordDecodePostProcessor implements BeanPostProcessor {
public Object postProcessAfterInitialization(Object object, String beanName) throws BeansException { return object;
}
public Object postProcessBeforeInitialization(Object object, String beanName) throws BeansException {
if(object instanceof PasswordDecodable) {
String encodedPassword = ((PasswordDecodable)object).getEncodedPassword();
String decodedPassword = decodePassword(encodedPassword);
((PasswordDecodable)object.setDecodedPassword(decodedPassword);
}
return object;
}
private String decodePassword(String encodedPassword) {
// 实现解码逻辑
return encodedPassword;
}
}or1
2ConfigurableBeanFactory beanFactory = new XmlBeanFactory(new ClassPathResource(...));
beanFactory.addBeanPostProcessor(new PasswordDecodePostProcessor());1
2
3
4
5
6
7<beans>
<bean id="passwordDecodePostProcessor" class="package.name.PasswordDecodePostProcessor">
只有将自定义的BeanPostProcessor实现类告知容器,容器才会在合适的时机应用它。所以,我 们需要将PasswordDecodePostProcessor注册到容器。
对于BeanFactory类型的容器来说,我们需要通过手工编码的方式将相应的BeanPostProcessor 注册到容器,也就是调用ConfigurableBeanFactory的addBeanPostProcessor()方法,见如下代码:
<!--如果需要,注入必要的依赖-->
</bean>
</beans>InitializingBean和init-method、DisposableBean与destroy-method
其作用在于,在对象实例化过程调用过“BeanPostProcessor的前置处理” 之后,会接着检测当前对象是否实现了InitializingBean接口,如果是,则会调用其afterProper- tiesSet()方法进一步调整对象实例的状态。Spring还提供了另一种方式来指定自定义的对象初始化操作,那就 是在XML配置的时候,使用的init-method属性。
容器将检查singleton类 型的bean实例,看其是否实现了org.springframework.beans.factory.DisposableBean接口。或 者其对应的bean定义是否通过的destroy-method属性指定了自定义的对象销毁方法。如果是, 就会为该实例注册一个用于对象销毁的回调(Callback),以便在这些singleton类型的对象实例销毁之 前,执行销毁逻辑。
##ApplicationContext - FileSystemXmlApplicationContext
- ClassPathXmlApplicationContext
- XmlWebApplicationContext
Resource
- ByteArrayResource
- ClassPathResource
- FileSystemResource
- UrlResource
ResourceLoader
- DefaultResourceLoader
- FileSystemResourceLoader
- ResourcePatternResolver 批量查找的ResourceLoader
ApplicationContext继承了ResourcePatternResolver,当然就间接实现了ResourceLoader接口。所以,任何的ApplicationContext实现都可以看作是一个 ResourceLoader甚至ResourcePatternResolver。而这就是ApplicationContext支持Spring内统一 资源加载策略的真相。
Spring IoC容器
@Autowired, @Qualifier, @PostConstruct, @PreDestroy
我们可以提供一个Spring 的IoC容器使用的BeanPostProcessor自定义实现,让这个BeanPostProcessor在实例化bean定义的 过程中,来检查当前对象是否有@Autowired标注的依赖需要注入。org.springframework.beans. factory.annotation.AutowiredAnnotationBeanPostProcessor就是Spring提供的用于这一目的 的BeanPostProcessor实现。
1 | Object[] beans = ...; |
context:annotation-config 不 但 帮 我 们 把 AutowiredAnnotationBeanPostProcessor 和 CommonAnnotationBeanPostProcessor注册到容器,同时还会把PersistenceAnnotationBeanPost- Processor和RequiredAnnotationBeanPostProcessor一并进行注册,可谓一举四得啊!
context:component-scan
context:component-scan在扫描相关类定义并将它们添加到容器的时候,会使用一种默认的 命名规则,来生成那些添加到容器的bean定义的名称(beanName)。
你或许会觉得有些诧异,因为我们并没有使用context:annotation-config甚至直接将相应 的BeanPostProcessor添加到容器中,而FXNewsProvider怎么会获得相应的依赖注入呢?这个得怪 context:component-scan“多管闲事”,它同时将AutowiredAnnotationBeanPostProcessor和 CommonAnnotationBeanPostProcessor一并注册到了容器中,所以,依赖注入的需求得以满足。
Spring AOP 概述和其实现
动态代理 (Dynamic Proxy)
1 | public class RequestCtrlIvocationHandler implements InvocationHandler { |
1 | ISubject subject = (ISubject) Proxy.newProxyInstance( |
默认情况下, 如果Spring AOP 发现目标对象实现了对应Interface,则采用动态代理机制为其生成代理对象实例.而如果目标对象没有实现任何Interface, Spring AOP会尝试使用一个称为cglib的为目标对象生成动态的代理.
1 | public class RequestCtrlCallback implements MethodInterceptor { |
1 | Enhancer enhancer = new EnHancer(); |
ProxyFactory
ProxyFactory waver = new ProxyFactory(youTargerObject);
Adviser advisor = “”;
waver.addAdvistor(advisor);
事务管理
一个典型的事物处理场景中,有几个参与者
- Resource Manager
负责存储并管理系统数据资源的状态, 数据库服务器、JMS消息服务器 - Transaction Processing Monitor
在分布式事务场景中协调多个RM的事物处理 - Transaction Manager
直接负责多RM之间事物处理的协调工作. 并提供食物界定,事务上下文传播等功能 - Application
以独立形式存在的或者运行于容器中的应用程序.- 全局事务
- 局部事务
因为JDBC的局部事务控制是由同一个java.sql.Connection来完成的,所以要保证两个DAO数据访问处在同一个事务中,我们就要保证他们使用的是同一个java.sql.Connection.要做到这一点通常会采用称为connection-passing的方式,即为同一个事务中的各个dao的数据范围方法传递当前事务对应的同一个java.sql.connection
1 | public class JdbcTransactionManager implement PlatformTransactionManager { |
因为Connection 在事务开始和结束期间都可以通过我们的TransactionResourceManager获得,所以所有的DAO层数据访问对象在使用JDBC进行数据访问的时候,就可以直接从TransactionResourceManager 中获得数据库连接进行访问.这样就可以保证在整个事务期间,所有访问的对象是同一个Connection.
Spring 的事务抽象
- PlatformTransactionManager
负责定义事务边界 - TransactionDefinition
负责定义事务相关属性(隔离级别、传播行为) - TransactionStatus
开启相关书屋. 对事务进行有限控制
TransactionDefinition
定义的内容包括
- 事务的隔离级别
IOSLATION_DEFAULT 默认 Read Commit
IOSLATION_READ_C - 事务传播行为
- 事务超时时间
- 是否为只读