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 ...
随机推荐
- Angular与PHP之间的不同的请求方式(post/get)导致的传参问题
angularJS的$http发送POST请求,PHP无法接受数据的问题 使用jQuery进行ajax请求 $.ajax({ type: 'POST', url:'url.php', data: da ...
- HTML与URL两种录制模式分析(转)
如何选择两种模式? 1.基于浏览器的应用程序推荐使用HTML-Based Script. 2.不是基于浏览器的应用程序推荐使用URL-Based Script. 3.如果基于浏览器的应用程序中包含了J ...
- DataTable List 相互转换
This uses the FastMember's meta-programming API for maximum performance. If you want to restrict it ...
- String、StringBuffer与StringBuilder比较
关于这三个类在字符串处理中的位置不言而喻,那么他们到底有什么优缺点,到底什么时候该用谁呢?下面我们从以下几点说明一下 1.三者在执行速度方面的比较: StringBuilder > St ...
- xml 类详解
- java 每一个对象都是根据hashCode区别的 每次返回不同的内存地址
可以通过hashCode比较对象,hashCode如果重写的话 返回的内存地址是一样的 则不能创建对象
- Ubuntu和Windows相互共享文件夹
一.Ubuntu访问Windows共享文件夹 1.对需要共享文件夹右击->属性->共享 2.选择要与其共享的用户,选择好用户点击有点添加按钮添加,然后点击下方的共享按钮 3.完成共享 4. ...
- UVA12538 Version Controlled IDE
题意翻译 维护一种数据结构,资磁三种操作. 1.在p位置插入一个字符串s 2.从p位置开始删除长度为c的字符串 3.输出第v个历史版本中从p位置开始的长度为c的字符串 1≤n≤50000,所有字符串总 ...
- Java线程Dump分析工具--jstack
jstack用于打印出给定的java进程ID或core file或远程调试服务的Java堆栈信息,如果是在64位机器上,需要指定选项"-J-d64",Windows的jstack使 ...
- BZOJ4036 [HAOI2015]按位或 【minmax容斥 + 期望 + FWT】
题目链接 BZOJ4036 题解 好套路的题啊,,, 我们要求的,实际上是一个集合\(n\)个\(1\)中最晚出现的\(1\)的期望时间 显然\(minmax\)容斥 \[E(max\{S\}) = ...