Spring基础之Bean的生命周期

       无论学习什么,了解它的生命周期是十分重要的。通过了解bean的生命周期,我们就可以在它的生命中做些什么,让它不虚度光阴。

       No image no truth:
这里写图片描述

       talk is cheap,show me the code:

配置文件
<?xml version="1.0" encoding="utf-8"?>
<!--
  - Application context definition for JPetStore's business layer.
  - Contains bean references to the transaction manager and to the DAOs in
  - dataAccessContext-local/jta.xml (see web.xml's "contextConfigLocation").
  -->
<beans xmlns="http://www.springframework.org/schema/beans"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xmlns:aop="http://www.springframework.org/schema/aop"
        xmlns:tx="http://www.springframework.org/schema/tx"
        xsi:schemaLocation="
            http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
            http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
            http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">

<bean id="dog" scope="singleton" destroy-method="mydestroy" init-method="init"  class="com.wmj.bean.Dog">
<property name="name" value="点点"/>
</bean>
<!--配置我们自己的后置处理器(类似于过滤器)  -->
<bean id="mybeanpostprocessor" class="com.wmj.bean.MyBeanPostProcessor"/>
</beans>

//Dog类
public class Dog implements BeanNameAware,BeanFactoryAware,
ApplicationContextAware,InitializingBean,DisposableBean{
private String name;

public String getName() {
    return name;
}

public void setName(String name) {
    this.name = name;
    System.out.println("调用set方法");
}
public Dog(){
    System.out.println("调用构造方法public Gog()");
}
public Dog(String name){
    System.out.println("调用构造方法public Gog(String name)");
    this.name = name;
}
public void sayhello(){
    System.out.println("wangwang"+name);
}
//arg0表示正在被实例化的bean的实例的id
@Override
public void setBeanName(String arg0) {

    System.out.println("现在正在被实例化的bean的ID是: "+arg0);
}
//该方法传递BeanFactory
@Override
public void setBeanFactory(BeanFactory arg0) throws BeansException {
    System.out.println("BeanFactory: "+arg0);   
}
//改革、方法传递ApplicationContext
@Override
public void setApplicationContext(ApplicationContext arg0)
        throws BeansException {

    System.out.println("ApplicationContext: "+arg0);
}

@Override
public void afterPropertiesSet() throws Exception {
    // TODO Auto-generated method stub
    System.out.println("afterPropertiesSet()被调用");
}
public void init(){
    System.out.println("我自己的init()方法");
}

@Override
public void destroy() throws Exception {
    // TODO Auto-generated method stub
    //这里我们可以关闭数据连接、socket、文件流、释放该bean的资源...
    System.out.println("destroy()...");
}
//定制我们自己的销毁方法
public void mydestroy(){
    System.out.println("我自己的销毁方法");
}
}

//后置处理器
public class MyBeanPostProcessor implements BeanPostProcessor {

    @Override
    public Object postProcessAfterInitialization(Object arg0, String arg1)
            throws BeansException {
        System.out.println("postProcessAfterInitialization方法被调用");
        return arg0;
    }

    @Override
    public Object postProcessBeforeInitialization(Object arg0, String arg1)
            throws BeansException {
        System.out.println("postProcessBeforeInitialization方法被调用");
        return arg0;
    }

}

//调用
public class App {
    public static void main(String[] args) {
          ApplicationContext ac = new ClassPathXmlApplicationContext("com/wmj/bean/beans.xml");
          Dog dog = (Dog) ac.getBean("dog");  
        /*BeanFactory factory = new XmlBeanFactory(new ClassPathResource("com/wmj/bean/beans.xml"));
        Dog dog = (Dog) factory.getBean("dog");*/
           dog.sayhello();
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值