spring bean中构造函数,afterPropertiesSet和init-method的执行顺序
2017-07-25 22:17
579 查看
http://blog.csdn.net/super_ccc/article/details/50728529
1.xml文件
[html] view plain copy
print?
<bean id="aaa" class="com.dingwang.Test.Aaa" init-method="init">
<constructor-arg name="name" value="ddd"></constructor-arg>
</bean>
2.java文件
[java] view plain copy
print?
public Aaa(String name) {
LOGGER.warn("--------------------Aaa-----------------Aaa");
this.setName(name);
}
public void init() {
LOGGER.warn("--------------------Aaa-----------------init");
}
/*
* (non-Javadoc)
* @see
* org.springframework.beans.factory.InitializingBean#afterPropertiesSet()
*/
@Override
public void afterPropertiesSet() throws Exception {
LOGGER.warn("--------------------Aaa-----------------afterPropertiesSet");
}
3.执行日志
[plain] view plain copy
print?
10:44:54.116 [main] DEBUG o.s.b.f.s.DefaultListableBeanFactory - Creating instance of bean 'aaa'
10:44:54.157 [main] WARN com.dingwang.Test.Aaa - --------------------Aaa-----------------Aaa
10:44:54.159 [main] DEBUG o.s.b.f.s.DefaultListableBeanFactory - Eagerly caching bean 'aaa' to allow for resolving potential circular references
10:44:54.171 [main] DEBUG o.s.b.f.s.DefaultListableBeanFactory - Invoking afterPropertiesSet() on bean with name 'aaa'
10:44:54.172 [main] WARN com.dingwang.Test.Aaa - --------------------Aaa-----------------afterPropertiesSet
10:44:54.172 [main] DEBUG o.s.b.f.s.DefaultListableBeanFactory - Invoking init method 'init' on bean with name 'aaa'
10:44:54.172 [main] WARN com.dingwang.Test.Aaa - --------------------Aaa-----------------init
10:44:54.173 [main] DEBUG o.s.b.f.s.DefaultListableBeanFactory - Finished creating instance of bean 'aaa'
4.结论
执行顺序:构造函数>afterPropertiesSet>init-method
Person类
[java] view plain copy
print?
public class Person {
private int i = 0;
public Person(){
System.out.println("实例化一个对象");
}
public void init(){
System.out.println("调用初始化方法....");
}
public void destory222(){
System.out.println("调用销毁化方法....");
}
}
applicationContext.xml
[html] view plain copy
print?
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
<!-- 配置初始化方法和销毁方法,但是如果要销毁方法生效scope="singleton" -->
<bean id="person" class="com.xxc.initAndDestory.domain.Person" scope="singleton" lazy-init="false" init-method="init" destroy-method="destory"></bean>
</beans>
测试类:
[java] view plain copy
print?
public class Test {
public static void main(String[] args) {
//如果要调用销毁方法必须用子类来声明,而不是ApplicationContext,因为ApplicationContext没有close()
ClassPathXmlApplicationContext ac = new ClassPathXmlApplicationContext("com/xxc/initAndDestory/applicationContext.xml");
Person p1 = (Person)ac.getBean("person");
Person p2 = (Person)ac.getBean("person");
ac.close();
}
}
如果用注解方式配置:
applicationContext.xml
[html] view plain copy
print?
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd">
<context:annotation-config/>
<!-- scope默认是 prototype:getBean()一次创建一个实例-->
<bean id="person" class="com.xxc.initAndDestory.domain.Person"></bean>
</beans>
Person类
[java] view plain copy
print?
public class Person {
private int i = 0;
public Person(){
System.out.println("实例化一个对象");
}
@PostConstruct //初始化方法的注解方式 等同与init-method=init
public void init(){
System.out.println("调用初始化方法....");
}
@PreDestroy //销毁方法的注解方式 等同于destory-method=destory222
public void destory(){
System.out.println("调用销毁化方法....");
}
}
1.xml文件
[html] view plain copy
print?
<bean id="aaa" class="com.dingwang.Test.Aaa" init-method="init">
<constructor-arg name="name" value="ddd"></constructor-arg>
</bean>
2.java文件
[java] view plain copy
print?
public Aaa(String name) {
LOGGER.warn("--------------------Aaa-----------------Aaa");
this.setName(name);
}
public void init() {
LOGGER.warn("--------------------Aaa-----------------init");
}
/*
* (non-Javadoc)
* @see
* org.springframework.beans.factory.InitializingBean#afterPropertiesSet()
*/
@Override
public void afterPropertiesSet() throws Exception {
LOGGER.warn("--------------------Aaa-----------------afterPropertiesSet");
}
3.执行日志
[plain] view plain copy
print?
10:44:54.116 [main] DEBUG o.s.b.f.s.DefaultListableBeanFactory - Creating instance of bean 'aaa'
10:44:54.157 [main] WARN com.dingwang.Test.Aaa - --------------------Aaa-----------------Aaa
10:44:54.159 [main] DEBUG o.s.b.f.s.DefaultListableBeanFactory - Eagerly caching bean 'aaa' to allow for resolving potential circular references
10:44:54.171 [main] DEBUG o.s.b.f.s.DefaultListableBeanFactory - Invoking afterPropertiesSet() on bean with name 'aaa'
10:44:54.172 [main] WARN com.dingwang.Test.Aaa - --------------------Aaa-----------------afterPropertiesSet
10:44:54.172 [main] DEBUG o.s.b.f.s.DefaultListableBeanFactory - Invoking init method 'init' on bean with name 'aaa'
10:44:54.172 [main] WARN com.dingwang.Test.Aaa - --------------------Aaa-----------------init
10:44:54.173 [main] DEBUG o.s.b.f.s.DefaultListableBeanFactory - Finished creating instance of bean 'aaa'
4.结论
执行顺序:构造函数>afterPropertiesSet>init-method
Person类
[java] view plain copy
print?
public class Person {
private int i = 0;
public Person(){
System.out.println("实例化一个对象");
}
public void init(){
System.out.println("调用初始化方法....");
}
public void destory222(){
System.out.println("调用销毁化方法....");
}
}
applicationContext.xml
[html] view plain copy
print?
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
<!-- 配置初始化方法和销毁方法,但是如果要销毁方法生效scope="singleton" -->
<bean id="person" class="com.xxc.initAndDestory.domain.Person" scope="singleton" lazy-init="false" init-method="init" destroy-method="destory"></bean>
</beans>
测试类:
[java] view plain copy
print?
public class Test {
public static void main(String[] args) {
//如果要调用销毁方法必须用子类来声明,而不是ApplicationContext,因为ApplicationContext没有close()
ClassPathXmlApplicationContext ac = new ClassPathXmlApplicationContext("com/xxc/initAndDestory/applicationContext.xml");
Person p1 = (Person)ac.getBean("person");
Person p2 = (Person)ac.getBean("person");
ac.close();
}
}
如果用注解方式配置:
applicationContext.xml
[html] view plain copy
print?
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd">
<context:annotation-config/>
<!-- scope默认是 prototype:getBean()一次创建一个实例-->
<bean id="person" class="com.xxc.initAndDestory.domain.Person"></bean>
</beans>
Person类
[java] view plain copy
print?
public class Person {
private int i = 0;
public Person(){
System.out.println("实例化一个对象");
}
@PostConstruct //初始化方法的注解方式 等同与init-method=init
public void init(){
System.out.println("调用初始化方法....");
}
@PreDestroy //销毁方法的注解方式 等同于destory-method=destory222
public void destory(){
System.out.println("调用销毁化方法....");
}
}
相关文章推荐
- spring @postConstruct init-method constructor和afterSetProperties() 执行顺序
- Spring InitializingBean的afterPropertiesSet与init-method、@PostConstruct
- 【转】比较init-method,afterPropertiesSet和BeanPostProcessor
- spring的InitializingBean的 afterPropertiesSet 方法 和 init-method配置的 区别联系
- 简单比较init-method,afterPropertiesSet和BeanPostProcessor
- Spring的init-method 与afterPropertiesSet
- spring中afterPropertiesSet方法与init-method配置描述
- 写代码简单比较init-method,afterPropertiesSet和BeanPostProcessor
- 简单比较init-method,afterPropertiesSet和BeanPostProcessor
- Spring启动,constructor,@PostConstruct,afterPropertiesSet,onApplicationEvent执行顺序 原创 2016年09月29日 11:39:2
- Spring启动,constructor,@PostConstruct,afterPropertiesSet,onApplicationEvent执行顺序
- 写代码简单比较init-method,afterPropertiesSet和BeanPostProcessor
- spring的InitializingBean的 afterPropertiesSet 方法 和 init-method配置的区别联系
- spring的InitializingBean的 afterPropertiesSet 方法 和 init-method配置的 区别联系
- 【Spring源码】从源码实现的角度解析spring的InitializingBean的afterPropertiesSet方法和init-method区别和应用
- init-method 与afterPropertiesSet
- Spring InitializingBean的afterPropertiesSet方法
- 用spring的InitializingBean的afterPropertiesSet来初始化
- spring InitializingBean init-method postConstruct 执行顺序
- 源码解析:init-method、@PostConstruct、afterPropertiesSet孰先孰后