(三)Spring-初始化bean的三种方式以及bean的后置处理器

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;
    }
}
  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值