Spring IOC 容器

<bean name="userBean" class="com.nuts.demo.spring.UserBean">
     <property name="username" value="张三"/>
</bean>

 

1、BeanFactory:低级容器,也是其他容器的父类,能完成基本的Bean加载工作

BeanFactory factory = new XmlBeanFactory(new ClassPathResource("src/main/resources/bean.xml")); 
UserBean userBean
= (UserBean) factory.getBean("userBean");
System.out.println(JSON.toJSONString(userBean));

2、ApplicationContext :高级容器,继承自BeanFactory,被常使用的实现类有:FileSystemXmlAPplicationContext、WebXmlApplicationContext、ClassPathXmlApplicationContext

//通过Bean工厂新建Bean
ApplicationContext context = new FileSystemXmlApplicationContext("bean.xml");
UserBean userBean = (UserBean) context.getBean("userBean");
System.out.println(JSON.toJSONString(userBean));

 

通过注解的方式加载BEAN

@Data
public class UserBean2 implements InitializingBean,DisposableBean{
    private  String username;

    private int age;

    @Override
    public void afterPropertiesSet() throws Exception {
        System.out.println("属性设置完成了:"+this.getUsername());
    }

    @Override
    public void destroy() throws Exception {
        System.out.println("对象被销毁:"+this.getUsername());
    }

    //Bean的初始化方法
    public void init(){
        System.out.println("初始化完成了");
    }
}
@Configuration
public class Config {
    @Bean(initMethod = "init")
    public UserBean2 userBean2(){
        UserBean2 userBean2 = new UserBean2();
        userBean2.setAge(1);
        return userBean2;
    }
}
    public static void main(String[] args) {
        ApplicationContext context = new AnnotationConfigApplicationContext(Config.class);
        UserBean2 userBean2 = context.getBean(UserBean2.class);
        System.out.println(JSON.toJSONString(userBean2));
    }

 

 

 

 

 

3、创建Bean的几种方式:XML文件、配置配、注解

4、Bean的作用域:singleton(单例、默认)、prototype(多例)、request、session、global Session

5、Bean的生命周期:

  初始化完成后,Bean中通过implements InitializingBean接口,实现afterPropertiesSet方法;或在bean的xml中定义时加上init-method指定初始化完成后的方法

  销毁回调,Bean中通过implements DisposableBean接口,实现destroy方法;或在bean的xml中定义destory-method指定销毁的方法,可通过(AbstractApplicationContext)context.registerShutdownHook();来模拟关闭

  建议使用xml配置的方式来处理,init和destory方法处理起来非常灵活

6、Spring Bean后置处理器。BeanPostProcessor允许Bean在初始化前后对bean进行额外的处理,多个BeanPostProcessor可以通过实现Orderd接口,设置优先级

public class BeanInitialize implements BeanPostProcessor ,Ordered {

    @Override
    public Object postProcessBeforeInitialization(Object o, String s) throws BeansException {
        System.out.println("初始化之前1:"+s);
        return o;
    }

    @Override
    public Object postProcessAfterInitialization(Object o, String s) throws BeansException {
        System.out.println("初始化之后1:"+s);
        return o;
    }

    @Override
    public int getOrder() {
        return 7;
    }
}
@Data
public class UserBean implements InitializingBean,DisposableBean{
    private  String username;

    private int age;

    @Override
    public void afterPropertiesSet() throws Exception {
        System.out.println("属性设置完成了:"+this.getUsername());
    }

    @Override
    public void destroy() throws Exception {
        System.out.println("对象被销毁"+this.getUsername());
    }
}
<bean name="userBean" class="com.nuts.demo.spring.UserBean">
        <property name="username" value="张三"/>
    </bean>
    <bean name="beanInitialize" class="com.nuts.demo.spring.BeanInitialize"/>
初始化之前1:userBean        ---------> BeanPostProcessor实现类的postProcessBeforeInitialization方法
属性设置完成了:张三      -----------> InitializingBean 实现类UserBean的afterPropertiesSet方法
初始化之后1:userBean      --------------> BeanPostProcessor实现类的postProcessAfterInitialization方法
Bean 内容:{"age":0,"username":"张三"} 
对象被销毁:张三          ------------>
InitializingBean 实现类UserBean的destroy方法

 

 

  

posted @ 2019-04-04 10:31  阿狸哥哥  阅读(186)  评论(0编辑  收藏  举报