标签:完成后 location xmlns instance .class 实现 param struct classpath
Spring 容器中的 Bean 是有生命周期的,Spring 允许在 Bean 在初始化完成后以及 Bean 销毁前执行特定的操作,常用的设定方式有以下十种:
那么这十种方式所定义方法执的行顺序是什么?
鄙人经过大量的代码实验得到了一些结果记录下来,以供用到时查阅。
文件 bean.xml 的代码如下:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
"
default-init-method="init_method" default-destroy-method="destroy_method">
<context:component-scan base-package="spring" />
<bean class="spring.InitHelloWorld2"/>
<bean class="spring.InitHelloWorld"/>
<bean id="hello" class="spring.Hello1" >
<property name="message" value="ccccccccc"/>
</bean>
</beans>
文件 Hello1.java 的代码如下:
package spring;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.*;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;
public class Hello1 implements InitializingBean, BeanNameAware,BeanFactoryAware,ApplicationContextAware,DisposableBean{
private String message;
public String getMessage(){
return message;
}
public void setMessage(String message){
System.out.println("Hello1 setMessage");
this.message = message;
}
public Hello1(){
System.out.println("Hello1 构造器");
}
public void hello(){
System.out.println("Hello1 我是:" + message);
}
public void init_method(){
System.out.println("Hello1 init-method ");
}
@PostConstruct
public void postConstruct(){
System.out.println("Hello1 @PostConstruct");
}
@Override
public void afterPropertiesSet() throws Exception{
System.out.println("Hello1 InitializingBean.afterPropertiesSet");
}
@Override
public void setBeanName(String s){
System.out.println("Hello1 setBeanName " + s);
}
@Override
public void setBeanFactory(BeanFactory beanFactory) throws BeansException{
System.out.println("Hello1 setBeanFactory " );
}
@Override
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException{
System.out.println("Hello1 setApplicationContext " );
}
@Override
public void destroy() throws Exception{
System.out.println("Hello1 DisposableBean.destroy " );
}
public void destroy_method() throws Exception{
System.out.println("Hello1 destroy-method " );
}
@PreDestroy
public void preDestroy() throws Exception{
System.out.println("Hello1 @PreDestroy " );
}
}
package spring;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.config.BeanPostProcessor;
import org.springframework.core.Ordered;
public class InitHelloWorld implements BeanPostProcessor,Ordered{
@Override
public Object postProcessBeforeInitialization(Object o, String s) throws BeansException{
System.out.println("BeanPostProcessor1.postProcessBeforeInitialization:" + s);
return o;
}
@Override
public Object postProcessAfterInitialization(Object o, String s) throws BeansException{
System.out.println("BeanPostProcessor1.postProcessAfterInitialization:" + s);
return o;
}
@Override
public int getOrder(){
return 10;
}
}
package spring;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanPostProcessor;
import org.springframework.core.Ordered;
public class InitHelloWorld2 implements BeanPostProcessor,Ordered{
@Override
public Object postProcessBeforeInitialization(Object o, String s) throws BeansException{
System.out.println("BeanPostProcessor2.postProcessBeforeInitialization:" + s);
return o;
}
@Override
public Object postProcessAfterInitialization(Object o, String s) throws BeansException{
System.out.println("BeanPostProcessor2.postProcessAfterInitialization:" + s);
return o;
}
@Override
public int getOrder(){
return 20;
}
}
package spring;
import org.springframework.context.support.AbstractApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class MainApp{
public static void main(String... args){
AbstractApplicationContext ctx = new ClassPathXmlApplicationContext("beans.xml");
Hello1 hello = (Hello1) ctx.getBean("hello");
hello.hello();
ctx.close();
}
}
Hello1 构造器
Hello1 setMessage
Hello1 setBeanName hello
Hello1 setBeanFactory
Hello1 setApplicationContext
BeanPostProcessor1.postProcessBeforeInitialization:hello
BeanPostProcessor2.postProcessBeforeInitialization:hello
Hello1 @PostConstruct
Hello1 InitializingBean.afterPropertiesSet
Hello1 init-method
BeanPostProcessor1.postProcessAfterInitialization:hello
BeanPostProcessor2.postProcessAfterInitialization:hello
Hello1 我是:ccccccccc
Hello1 @PreDestroy
Hello1 DisposableBean.destroy
Hello1 destroy-method
链接:https://www.jianshu.com/p/80d4fa132747
标签:完成后 location xmlns instance .class 实现 param struct classpath
原文地址:https://www.cnblogs.com/miracle77hp/p/10107106.html