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 ...
随机推荐
- 单片机FLASH与RAM、ROM的关系
片机FLASH主要用作程序存贮器,就是替代以前的ROM,最大的有有点是降低了芯片的成本并且可以做到电擦写,目前市场上单片机的FALSH寿命相差比较大,擦写次数从1000~10万的都有,但存储时间可以保 ...
- 命令行方式操作O365
这几天公司为O365启用了双因子认证,期间出了些小问题,导致我无法使用管理员账号登入控制台,但是Powershell的方法依旧可以.所以我觉得还是有必要把图形化操作改成脚本操作,以备不时之需.以后凡是 ...
- Java开发中的23种设计模式详解(转载)
前学习过一段时间的设计模式,总是感觉学习的不够清楚.现在再重新复习一下,原文地址:https://blog.csdn.net/doymm2008/article/details/13288067 一. ...
- git 常用命令(含删除文件)
git 常用命令(含删除文件) Git常用操作命令收集: 1) 远程仓库相关命令 检出仓库:$ git clone git://github.com/jquery/jquery.git 查看远程仓库: ...
- 删除XML文件中的空格
应要求需要删除xml文件中的空格,制表符等字符.要求双引号和xml的text属性中包含的空格不删除. bool delSpace(QFile &file, QString path) //删除 ...
- ubuntu在终端使用的常用命令
1.ubuntu系统显示IP地址:ifconfig 2.ubuntu系统文件命令: cat:显示文本文件内容,全部文本.格式:cat filename more:显示文件内容,分页显示,回车逐行下翻. ...
- zabbix自定义web检测
zabbix自定义web检测 本博客使用zabbix 版本 4.0.3 Web监控的原理 Web监控即对HTTP服务的监控,模拟用户去访问网站,对特定的结果进行比较,如状态码.返回字符串等特定的数据进 ...
- P2461 [SDOI2008]递归数列
题目描述 一个由自然数组成的数列按下式定义: 对于i <= k:ai = bi 对于i > k: ai = c1ai-1 + c2ai-2 + ... + ckai-k 其中bj 和 cj ...
- 学习Spring Boot:(九)统一异常处理
前言 开发的时候,每个controller的接口都需要进行捕捉异常的处理,以前有的是用切面做的,但是SpringMVC中就自带了@ControllerAdvice ,用来定义统一异常处理类,在 Spr ...
- BZOJ4589 Hard Nim 【FWT】
题目链接 BZOJ4589 题解 FWT 模板题 #include<algorithm> #include<iostream> #include<cstdlib> ...