1、通过@PostConstruct 和 @PreDestroy 方法 实现初始化和销毁bean之前进行的操作
//定义相关的实现类:
public class PersonService {
private String message;
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
@PostConstruct
public void init(){
System.out.println("I'm init method using @PostConstrut...."+message);
}
@PreDestroy
public void dostory(){
System.out.println("I'm destory method using @PreDestroy....."+message);
}
}
定义相关的配置文件:其中<context:annotation-config />告诉spring 容器采用注解配置:扫描注解配置;
<context:annotation-config />
<bean id="personService" class="com.myapp.core.annotation.init.PersonService">
<property name="message" value="123"></property>
</bean>
测试类:
public class MainTest {
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("resource/annotation.xml");
PersonService personService = (PersonService)context.getBean("personService");
personService.dostory();
}
}
2、通过 在xml中定义init-method 和 destory-method方法
<!--创建带有生命周期方法的bean -->
<bean id="book6" class="com.atguigu.spring.ioc.bean.Book" init-method="init" destroy-method="destroy">
<property name="bookId" value="999"></property>
<property name="title" value="BookName09"></property>
<property name="author" value="Author09"></property>
<property name="price" value="100.9"></property>
</bean>
public class Book {
private Integer bookId = 101 ;
private String title ="Bookname101";
private String author ="Author101";
private Double price =101.0;
//用于Bean对象的初始化,方法声明规则:必须是无参数。
public void init(){
this.bookId = 300 ;
this.title = "Bookname300";
this.author = "Author300";
this.price = 300.0 ;
System.out.println("init....");
}
//用于做销毁操作
public void destroy(){
this.bookId = null ;
this.title = null ;
this.author = null ;
this.price = null ;
System.out.println("destroy....");
}
}
初始化复制顺序:动态代码块--->默认构造器--->set方法--->init()方法
3、通过bean实现InitializingBean和 DisposableBean接口
@Service
public class GhtPayAgentPayBankMapInit implements InitializingBean , DisposableBean {
@Override
public void afterPropertiesSet() throws Exception {
System.out.println("init... ...");
}
@Override
public void destroy() throws Exception {
System.out.println("destroy... ...");
}
}
bean的后置处理器:对初始化方法init()进行拦截器,功能扩展
<!-- bean的后置处理器 所有的bean都会拦截 bean程序中不用去得到bean的时候可以不定义 id-->
<bean class="com.atguigu.spring.ioc.processor.MyBeanPostProcessor"></bean>
public class MyBeanPostProcessor implements BeanPostProcessor {
//在初始化方法后被调用
@Override
public Object postProcessAfterInitialization(Object beanObject, String id)throws BeansException {
//如果不想所有的bean都被拦截 进行一下判断 if(Object instanceof Book){ 不进行处理 }
System.out.println(beanObject.getClass().getName() + "postProcessAfterInitialization..."+id);
return beanObject;
}
//在初始化方法前被调用。
@Override
public Object postProcessBeforeInitialization(Object beanObject, String id)throws BeansException {
//如果不想所有的bean都被拦截 进行一下判断 if(Object instanceof Book){ 不进行处理 }
System.out.println(beanObject.getClass().getName() + "postProcessBeforeInitialization..."+id);
return beanObject;
}
}