zoukankan      html  css  js  c++  java
  • Spring Bean的生命周期理解

    《Spring in action》 

    import org.springframework.beans.BeansException;
    import org.springframework.beans.factory.BeanFactory;
    import org.springframework.beans.factory.BeanFactoryAware;
    import org.springframework.beans.factory.BeanNameAware;
    import org.springframework.beans.factory.DisposableBean;
    import org.springframework.beans.factory.InitializingBean;
    import org.springframework.beans.factory.config.BeanPostProcessor;
    import org.springframework.context.ApplicationContext;
    import org.springframework.context.ApplicationContextAware;
    import org.springframework.stereotype.Component;
    
    @Component
    public class MyBean implements BeanNameAware,BeanFactoryAware,ApplicationContextAware,InitializingBean,BeanPostProcessor,DisposableBean {
        
        ApplicationContext applicationContext;
        BeanFactory beanFactory;
        String beanname;
        String properties;
        public MyBean(){
            System.out.println("MyBean 被创建了");
        }
        @Override
        public void setBeanName(String name) {
            System.out.println("BeanNameAware的setBeanName:"+name);
            beanname=name;
        }
        @Override
        public void setBeanFactory(BeanFactory beanFactory) throws BeansException {
            System.out.println("BeanFactoryAware的setbeanFactory:"+beanFactory);
            this.beanFactory=beanFactory;
        }
        @Override
        public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
            System.out.println("ApplicationContextAware的setApplicationContext:"+applicationContext);
            this.applicationContext=applicationContext;
        }
        //实现BeanPostProcessor的方法会被容器自动调用;
        @Override
        public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
            System.out.println(beanName+"开始实例化!");
            return bean;//对bean处理后放回容器,如果return null 容器将没有bean;
        }
        @Override
        public void afterPropertiesSet() throws Exception {
            properties="sdfsf";
            System.out.println("InitializingBean的afterPropertiesSet!");
        }
        @Override
        public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
            System.out.println(beanName+"实例化完成!");
            return bean;
        }
        @Override
        public void destroy() throws Exception {
            System.out.println("Bean销毁——destroy:"+this.toString());
        }
    }
  • 相关阅读:
    time模块
    Spring注入方式及注解配置
    Spring注入值得2种方式:属性注入和构造注入
    MySQL命令行登陆,远程登陆MySQL
    通过XMLHttpRequest和jQuery两种方式实现ajax
    Linux常用命令
    ASP.NET 打包下载文件
    从用户浏览器输入url到用户看到页面结果的过程,发生了什么事情?
    冒泡排序的三种实现
    字符串编码C#
  • 原文地址:https://www.cnblogs.com/mryangbo/p/10807082.html
Copyright © 2011-2022 走看看