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方法
Spring IOC 容器的更多相关文章
- Spring IoC容器的初始化过程
Spring IoC容器的初始化包括 BeanDefinition的Resource定位.载入和注册 这三个基本的过程.IoC容器的初始化过程不包含Bean依赖注入的实现.Bean依赖的注入一般会发生 ...
- 【Spring】非Spring IOC容器下获取Spring IOC上下文的环境
前言 在Spring Web项目中,有些特殊的时候需要在非Spring IOC容器下获取Spring IOC容器的上下文环境,比如获取某个bean. 版本说明 声明POM文件,指定需引入的JAR. & ...
- 学习Spring(一) 实例化Spring IoC容器
实例化Spring IoC容器 1,读取其配置来创建bean实例 2,然后从Spring IoC容器中得到可用的bean实例 Spring提供两种IoC容器实现类型 a,一种为bean工厂 b,应用程 ...
- MyEclipse Spring 学习总结一 Spring IOC容器
一.Spring IOC容器---- Spring AllicationContext容器 程序的结构如下: 1.首先在MyEclipse 创建创建Java Project 2.创建好后,添加spin ...
- 对Spring IoC容器实现的结构分析
本文的目标:从实现的角度来认识SpringIoC容器. 观察的角度:从外部接口,内部实现,组成部分,执行过程四个方面来认识SpringIoC容器. 本文的风格:首先列出SpringIoC的外部接口及内 ...
- spring IOC容器实例化Bean的方式与RequestContextListener应用
spring IOC容器实例化Bean的方式有: singleton 在spring IOC容器中仅存在一个Bean实例,Bean以单实例的方式存在. prototype 每次从容器中调用Bean时, ...
- 解读Spring Ioc容器设计图
在Spring Ioc容器的设计中,有俩个主要的容器系列:一个是实现BeanFactory接口的简单容器系列,这系列容器只实现了容器最基本的功能:另外一个是ApplicationContext应用上下 ...
- 纯注解快速使用spring IOC容器
使用spring的ioc容器实现对bean的管理与基本的依赖注入是再经典的应用了.基础使用不在详述. 这里主要介绍下使用注解实现零配置的spring容器.我相信你也会更喜欢使用这种方式.Spring ...
- Spring IOC容器分析(2) -- BeanDefinition
上文对Spring IOC容器的核心BeanFactory接口分析发现:在默认Bean工厂DefaultListableBeanFactory中对象不是以Object形成存储,而是以BeanDefin ...
- Spring IOC容器分析(4) -- bean创建获取完整流程
上节探讨了Spring IOC容器中getBean方法,下面我们将自行编写测试用例,深入跟踪分析bean对象创建过程. 测试环境创建 测试示例代码如下: package org.springframe ...
随机推荐
- Mongodb 分片操作实战
由于生产环境中一般使用zoomkeeper做config节点的仲裁节点,zoomkeeper会在三个config节点中挑选出一台作为主config节点.且mongos节点一般是两个节点,必须做高可用, ...
- 1105 C程序的推导过程
- 『编程题全队』Alpha 阶段冲刺博客Day4
1.每日站立式会议 1.会议照片 2.昨天已完成的工作统计 孙志威: 1.添加团队界面下的看板容器SlotWidget 2.实现SlotWidgets的动态布局管理 3.实现团队/个人界面之间的切换 ...
- build.xml
下载ant 解压ant 后设置ANT_HOME, PATH中添加ANT_HOME目录下的bin目录(如:ANT_HOME:,PATH:D:\apache-ant-1.9.2%ANT_HOME%\bin ...
- [微软官网]SQLSERVER的版本信息
来源:https://docs.microsoft.com/zh-cn/sql/sql-server/editions-and-components-of-sql-server-2017?view=s ...
- JVM内存管理机制
Java与C++之间有一堆由内存动态分配与垃圾收集技术所围成的“高墙”,墙外面的人想进去,墙里面的人却想出来. —— <深入理解Java虚拟机:JVM高级特性与最佳实践> Java虚拟机在 ...
- Handler,Looper,HandlerThread浅析
Handler想必在大家写Android代码过程中已经运用得炉火纯青,特别是在做阻塞操作线程到UI线程的更新上.Handler用得恰当,能防止很多多线程异常. 而Looper大家也肯定有接触过,只不过 ...
- Python 变量 (上)
Python通过变量引用内存中的值,变量的值占用多少空间是由变量的类型决定的.声明变量不需要指定变量的类型,解释器会自动根据值来判断.变量名称必须符合标识符的定义 标识符 标识符是由字母,数字和下划线 ...
- Luogu4885 灭顶之灾
显然1的位置确定了整个矩阵,每一段连续的数即是对该位置的限制.化一下式子可以发现是一段区间或一段区间的补集,最后要求的是他们的交.看起来不太好求,于是转为求补集的并,那么排个序就完了. #includ ...
- ava8并发教程:Threads和Executors
原文地址 原文作者:Benjamin Winterberg 译者:张坤 欢迎阅读我的Java8并发教程的第一部分.这份指南将会以简单易懂的代码示例来教给你如何在Java8中进行并发编程.这是一系列教 ...