<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方法

  

Spring IOC 容器的更多相关文章

  1. Spring IoC容器的初始化过程

    Spring IoC容器的初始化包括 BeanDefinition的Resource定位.载入和注册 这三个基本的过程.IoC容器的初始化过程不包含Bean依赖注入的实现.Bean依赖的注入一般会发生 ...

  2. 【Spring】非Spring IOC容器下获取Spring IOC上下文的环境

    前言 在Spring Web项目中,有些特殊的时候需要在非Spring IOC容器下获取Spring IOC容器的上下文环境,比如获取某个bean. 版本说明 声明POM文件,指定需引入的JAR. & ...

  3. 学习Spring(一) 实例化Spring IoC容器

    实例化Spring IoC容器 1,读取其配置来创建bean实例 2,然后从Spring IoC容器中得到可用的bean实例 Spring提供两种IoC容器实现类型 a,一种为bean工厂 b,应用程 ...

  4. MyEclipse Spring 学习总结一 Spring IOC容器

    一.Spring IOC容器---- Spring AllicationContext容器 程序的结构如下: 1.首先在MyEclipse 创建创建Java Project 2.创建好后,添加spin ...

  5. 对Spring IoC容器实现的结构分析

    本文的目标:从实现的角度来认识SpringIoC容器. 观察的角度:从外部接口,内部实现,组成部分,执行过程四个方面来认识SpringIoC容器. 本文的风格:首先列出SpringIoC的外部接口及内 ...

  6. spring IOC容器实例化Bean的方式与RequestContextListener应用

    spring IOC容器实例化Bean的方式有: singleton 在spring IOC容器中仅存在一个Bean实例,Bean以单实例的方式存在. prototype 每次从容器中调用Bean时, ...

  7. 解读Spring Ioc容器设计图

    在Spring Ioc容器的设计中,有俩个主要的容器系列:一个是实现BeanFactory接口的简单容器系列,这系列容器只实现了容器最基本的功能:另外一个是ApplicationContext应用上下 ...

  8. 纯注解快速使用spring IOC容器

    使用spring的ioc容器实现对bean的管理与基本的依赖注入是再经典的应用了.基础使用不在详述. 这里主要介绍下使用注解实现零配置的spring容器.我相信你也会更喜欢使用这种方式.Spring ...

  9. Spring IOC容器分析(2) -- BeanDefinition

    上文对Spring IOC容器的核心BeanFactory接口分析发现:在默认Bean工厂DefaultListableBeanFactory中对象不是以Object形成存储,而是以BeanDefin ...

  10. Spring IOC容器分析(4) -- bean创建获取完整流程

    上节探讨了Spring IOC容器中getBean方法,下面我们将自行编写测试用例,深入跟踪分析bean对象创建过程. 测试环境创建 测试示例代码如下: package org.springframe ...

随机推荐

  1. 17_常用API_第17天(包装类、System、Math、Arrays、大数据运算)_讲义

    今日内容介绍 1.基本类型包装类 2.System类 3.Math类 4.Arrays类 5.大数据运算 01基本数据类型对象包装类概述 *A:基本数据类型对象包装类概述 *a.基本类型包装类的产生 ...

  2. WPF里面制作圆角文本框

    转自:http://www.cnblogs.com/mengxin523/archive/2010/04/04/1704448.html 本以为WPF里面的XAML会很强大,可以设置很多属性,比如文本 ...

  3. DispatcherServlet的作用

    DispatcherServlet是前端控制器设计模式的实现,提供Spring Web MVC的集中访问点,而且负责职责的分派,而且与Spring IoC容器无缝集成,从而可以获得Spring的所有好 ...

  4. pygame学习笔记(5)——精灵

    转载请注明:@小五义 http://www.cnblogs.com/xiaowuyi 据说在任天堂FC时代,精灵的作用相当巨大,可是那时候只知道怎么玩超级玛丽.魂斗罗,却对精灵一点也不知.pygame ...

  5. [转帖]HDD磁盘,非4K无以致远

    https://blog.csdn.net/swingwang/article/details/54880918 机械硬盘的未来要靠高容量作为依托,在财报中,希捷表示未来18个月内它们将推出14和16 ...

  6. Iaas

    IaaS(Infrastructure as a Service),即基础设施即服务. 消费者通过Internet 可以从完善的计算机基础设施获得服务.这类服务称为基础设施即服务.基于 Interne ...

  7. js & listen mouse click

    js & listen mouse click how to listen mouse click in js https://www.kirupa.com/html5/mouse_event ...

  8. nilcms file类 简单文件缓存实现

    实现简单的文件缓存,参照CI的部分设计,在这里记录一下子. class File { const CACHE_PATH = 'nil_file_cache'; /*其他函数省略了*/ /** * 获取 ...

  9. linux下怎么修改grub.cfg

    一.grub2的启动配置文件grub.cfggrub2的启动配置文件grub.cfg是/boot/grub/grub.cfg,而不是以前的memu.lst.如果你是多系统,有Ubuntu和window ...

  10. 【2018CCPC秦皇岛】

    递推式的线段树可以用矩阵维护.