bean生命周期

public class Person implements BeanNameAware, ApplicationContextAware,InitializingBean, BeanPostProcessor,DisposableBean{

    String name;

    @Autowired
    Car car;

    Person () {
        name = "zhangsan";
    }


    public void custom_destroy() {}

    @PostConstruct
    public void postConstruct() {}

    public void custom_init() {}

    ......
    //省略@Override
}
----------
src/main/java/configure.xml配置文件:
<bean class="com.example.beanlife.BeanLifeCycle" init-method="custom_init" destroy-method="custom_destroy"></bean>
----------
pom.xml
    <build>
        <resources>
            <resource>
                <directory>src/main/resources</directory>
                    <includes>
                    <include>application.properties</include>
                        <include>configure.xml</include>
                    </includes>
            </resource>
        </resources>
    </build>
----------
主类:
@ImportResource(locations = "classpath:configure.xml")
----------

bean生命周期

  1. 调用Person的默认构造函数.
  2. IOC注入,即调用Car的构造函数生成一个bean, 注入到Person实例.
  3. setBeanName(String beanName)回调,可通过该方法从参数中获取到本bean在ApplicationContext中的beanName. //override
  4. setApplicationContext(ApplicationContext var1)回调,可以获取到通过参数传递的ApplicationContext,借此可进一步获取到ApplicationContext中的其它bean,以及容器中的所有其它信息。//override
  5. postConstruct()//@PostConstruct
  6. afterPropertiesSet() //override
  7. init-method //xml
  8. postProcessBeforeInitialization(Object var1, String var2) //override
  9. postProcessAfterInitialization(Object var1, String var2) //override
    Bean可以使用了。
  10. destroy() //override
  11. custom_destroy() //xml

在pom中被include的文件会在编译时放在target里,如果是配置bean的xml文件,还需要使用@importResource (例如@ImportResource(locations = “classpath:configure.xml”)) configure.xml放在src/main/resources/里面,在project structure里面设置的。


点击参考链接

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值