Spring 框架基础(02):Bean的生命周期,作用域,装配总结
本文源码:GitHub·点这里 || GitEE·点这里
一、装配方式
Bean的概念:Spring框架管理的应用程序中,由Spring容器负责创建,装配,设置属性,进而管理整个生命周期的对象,称为Bean对象。
1、XML格式装配
Spring最传统的Bean的管理方式。
- 配置方式
<bean id="userInfo" class="com.spring.mvc.entity.UserInfo">
<property name="name" value="cicada" />
</bean>
- 测试代码
ApplicationContext context01 = new ClassPathXmlApplicationContext("/bean-scan-02.xml");
UserInfo userInfo = (UserInfo)context01.getBean("userInfo") ;
System.out.println(userInfo.getName());
2、注解扫描
在实际开发中:通常使用注解 取代 xml配置文件。
- 常见注解
@Component <==> <bean class="Class">
@Component("id") <==> <bean id="id" class="Class">
@Repository :Mvc架构中Dao层Bean的注解
@Service:Mvc架构中Service层Bean的注解
@Controller:Mvc架构中Controller层Bean的注解
- 使用案例
// 1、注解代码块
@Component("infoService")
public class InfoServiceImpl implements InfoService {
@Override
public void printName(String name) {
System.out.println("Name:"+name);
}
}
// 2、配置代码块
@ComponentScan // 组件扫描注解
public class BeanConfig {
}
- 测试代码
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = BeanConfig.class)
public class Test01 {
@Autowired
private InfoService infoService ;
@Test
public void test1 (){
infoService.printName("cicada");
System.out.println(infoService==infoService);
}
}
3、XML配置扫描
上面使用 ComponentScan 注解,也可在配置文件进行统一的配置,效果相同,还简化代码。
<context:component-scan base-package="com.spring.mvc" />
4、Java代码装配
这种基于Configuration注解,管理Bean的创建,在SpringBoot和SpringCloud的框架中,十分常见。
- 配置类代码
@Configuration // 配置类注解
public class UserConfig {
@Bean
public UserInfo userInfo (){
System.out.println("userInfo...");
return new UserInfo() ;
}
}
- 测试代码
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = UserConfig.class)
public class Test03 {
@Autowired
private UserInfo userInfo ;
@Autowired
private UserInfo userInfo1 ;
@Test
public void test1 (){
/*
* userInfo...
* true
*/
System.out.println(userInfo==userInfo1);
}
}
二、属性值设置
上面是Bean的装配几种常见方式,下面来看看Bean属性值设置,这里就基于Xml配置的方式。
1、基础类型和集合
- 配置代码
<!-- 配置Employee公共属性 -->
<bean id="emp1" class="com.spring.mvc.entity.Employee">
<property name="name" value="cicada" />
<property name="id" value="1" />
</bean>
<bean id="emp2" class="com.spring.mvc.entity.Employee">
<property name="name" value="smile" />
<property name="id" value="2" />
</bean>
<!-- 配置Department属性 -->
<bean id="department" class="com.spring.mvc.entity.Department">
<!-- 普通属性值的注入 -->
<property name="name" value="IT部门" />
<!-- 给数组注入值 -->
<property name="empName">
<list>
<value>empName1</value>
<value>empName2</value>
<value>empName3</value>
</list>
</property>
<!-- 给List注入值:可以存放相同的值 -->
<property name="empList">
<list>
<ref bean="emp1"/>
<ref bean="emp2"/>
<ref bean="emp1"/>
</list>
</property>
<!-- 配置Set属性,相同的对象会被覆盖 -->
<property name="empSet">
<set>
<ref bean="emp1"/>
<ref bean="emp2"/>
<ref bean="emp1"/>
</set>
</property>
<!-- 配置Map属性,key相同的话,后面的值会覆盖前面的 -->
<property name="empMap">
<map>
<entry key="1" value-ref="emp1" />
<entry key="2" value-ref="emp2" />
<entry key="2" value-ref="emp1" />
</map>
</property>
<!-- 配置属性集合 -->
<property name="pp">
<props>
<prop key="pp1">Hello</prop>
<prop key="pp2">World</prop>
</props>
</property>
</bean>
- 测试代码
public class Test05 {
@Test
public void test01 (){
ApplicationContext context = new ClassPathXmlApplicationContext("/bean-value-03.xml");
Department department = (Department) context.getBean("department");
System.out.println(department.getName());
System.out.println("--------------------->String数组");
for (String str : department.getEmpName()){
System.out.println(str);
}
System.out.println("--------------------->List集合");
for (Employee smp : department.getEmpList()){
System.out.println(smp.getId()+":"+smp.getName());
}
System.out.println("--------------------->Set集合");
for (Employee emp : department.getEmpSet()){
System.out.println(emp.getId()+":"+emp.getName());
}
System.out.println("--------------------->Map集合");
for (Map.Entry<String, Employee> entry : department.getEmpMap().entrySet()){
System.out.println(entry.getKey()+":"+entry.getValue().getName());
}
System.out.println("--------------------->Properties");
Properties pp = department.getPp();
System.out.println(pp.get("pp1"));
System.out.println(pp.get("pp2"));
}
}
2、配置构造函数
根据配置的参数个数和类型,去映射并加载Bean的构造方法。
- 配置代码
<!-- 这里配置2个参数,所有调用2个参数的构造函数 -->
<bean id="employee" class="com.spring.mvc.entity.Employee">
<constructor-arg index="0" type="java.lang.String" value="cicada"/>
<constructor-arg index="1" type="int" value="1"/>
</bean>
- 测试代码
public class Test06 {
@Test
public void test01 (){
ApplicationContext context = new ClassPathXmlApplicationContext("/bean-value-04.xml");
Employee employee = (Employee) context.getBean("employee");
System.out.println(employee.getId()+":"+employee.getName());
}
}
3、配置继承关系
- 配置代码
<!-- 配置父类信息 -->
<bean id="student" class="com.spring.mvc.entity.Student">
<property name="name" value="Spring" />
<property name="age" value="22" />
</bean>
<!-- 配置子类信息 -->
<bean id="grade" class="com.spring.mvc.entity.Grade">
<!-- 覆盖 -->
<property name="name" value="Summer" />
<property name="degree" value="大学" />
</bean>
- 测试代码
public class Test07 {
@Test
public void test01 (){
ApplicationContext context = new ClassPathXmlApplicationContext("/bean-value-05.xml");
Grade grade = (Grade) context.getBean("grade");
/* Summer;0;大学 */
System.out.println(grade.getName()+";"+grade.getAge()+";"+grade.getDegree());
}
}
三、作用域
作用域:用于确定spring创建bean实例个数,比如单例Bean,原型Bean,等等。
类型 | 说明 |
---|---|
singleton | IOC容器仅创建一个Bean实例,IOC容器每次返回的是同一个单例Bean实例,默认配置。 |
prototype | IOC容器可以创建多个Bean实例,每次返回的Bean都是新的实例。 |
request | 每次HTTP请求都会创建一个新的Bean,适用于WebApplicationContext环境。 |
session | 同一个HTTP Session共享一个Bean实例。不同HTTP Session使用不同的实例。 |
global-session | 同session作用域不同的是,所有的Session共享一个Bean实例。 |
四、生命周期
在Spring框架中Bean的生命周期非常复杂,过程大致如下:实例化,属性加载,初始化前后管理,销毁等。下面基于一个案例配置,会更加的清楚。
1、编写BeanLife类
public class BeanLife implements BeanNameAware {
private String name ;
public String getName() {
return name;
}
public void setName(String name) {
System.out.println("设置名称:"+name);
this.name = name;
}
@Override
public void setBeanName(String value) {
System.out.println("BeanNameAware..SetName:"+value);
}
public void initBean() {
System.out.println("初始化Bean..");
}
public void destroyBean() {
System.out.println("销毁Bean..");
}
public void useBean() {
System.out.println("使用Bean..");
}
@Override
public String toString() {
return "BeanLife [name = " + name + "]";
}
}
2、定制加载过程
实现BeanPostProcessor接口。
public class BeanLifePostProcessor implements BeanPostProcessor {
// 初始化之前对bean进行增强处理
@Override
public Object postProcessBeforeInitialization(Object obj, String beanName) throws BeansException {
System.out.println("初始化之前..."+beanName);
return obj ;
}
// 初始化之后对bean进行增强处理
@Override
public Object postProcessAfterInitialization(Object obj, String beanName) throws BeansException {
System.out.println("初始化之后..."+beanName);
// 改写Bean的名称
if (obj instanceof BeanLife){
BeanLife beanLife = (BeanLife)obj ;
beanLife.setBeanName("myBeanLifeTwo");
return beanLife ;
}
return obj ;
}
}
3、配置文件
<!-- 加载Bean的处理器 -->
<bean class="com.spring.mvc.BeanLifePostProcessor" />
<!-- 指定初始化和销毁方法 -->
<bean id="beanLife" class="com.spring.mvc.entity.BeanLife"
init-method="initBean" destroy-method="destroyBean">
<property name="name" value="myBeanLifeOne" />
</bean>
4、测试过程
- 测试代码
public class Test08 {
@Test
public void test01 (){
ApplicationContext context = new ClassPathXmlApplicationContext("/bean-value-06.xml");
BeanLife beanLife = (BeanLife) context.getBean("beanLife");
System.out.println("测试结果BeanLife:"+beanLife.getName()) ;
beanLife.useBean();
// 关闭容器
((AbstractApplicationContext) context).close();
}
}
- 输出结果
1、设置名称:myBeanLifeOne
2、BeanNameAware..SetName:beanLife
3、初始化之前...beanLife
4、初始化Bean..
5、初始化之后...beanLife
6、BeanNameAware..SetName:myBeanLifeTwo
7、测试结果BeanLife:myBeanLifeOne
8、使用Bean..
9、销毁Bean..
这里梳理Bean的生命周期,过程十分清晰。
五、源代码地址
GitHub·地址
https://github.com/cicadasmile/spring-mvc-parent
GitEE·地址
https://gitee.com/cicadasmile/spring-mvc-parent
Spring 框架基础(02):Bean的生命周期,作用域,装配总结的更多相关文章
- Spring基础14——Bean的生命周期
1.IOC容器中的Bean的生命周期方法 SpringIOC容器可以管理Bean的生命周期,Spring允许在Bean生命周期的特定点执行定制的任务.SpringIOC容器对Bean的生命周期进行管理 ...
- Spring系列13:bean的生命周期
本文内容 bean的完整的生命周期 生命周期回调接口 Aware接口详解 Spring Bean的生命周期 面试热题:请描述下Spring的生命周期? 4大生命周期 从源码角度来说,简单分为4大阶段: ...
- spring深入学习(二)-----bean的生命周期、IOC容器bean装配
bean的生命周期 1.实例化Bean对于BeanFactory容器,当客户向容器请求一个尚未初始化的bean时,或初始化bean的时候需要注入另一个尚未初始化的依赖时,容器就会调用createBea ...
- spring IOC 容器中 Bean 的生命周期
IOC 容器中 Bean 的生命周期: 1.通过构造器或工厂方法创建 Bean 实例 2.为 Bean 的属性设置值和对其他 Bean 的引用 3.调用 Bean 后置处理器接口(BeanPostPr ...
- MyEclipse Spring 学习总结二 Bean的生命周期
文件结构可以参考上一节 Bean的生命周期有方法有:init-method,destroy-method ApplicationContext.xml 文件配置如下: <?xml version ...
- Spring IOC容器中Bean的生命周期
1.IOC容器中Bean的生命周期 构造器函数 设置属性 初始化函数(在Bean配置中 init-method) 使用Bean 结束时关闭容器(在Bean中配置destroy-method) 2.Be ...
- Spring学习记录(八)---Bean的生命周期
之前说过,在调用下面时,就创建了容器和对象 ApplicationContext ctx = new ClassPathXmlApplicationContext("bean.xml&quo ...
- Spring应用上下文中Bean的生命周期
Bean装载到Spring应用上下文的生命周期,如图: Bean在Spring容器中从创建到销毁经历了若干个阶段,每一阶段都可以对Spring如何管理Bean进行个性化定制,以下我们通过代码去验证生命 ...
- android 基础02 - Activity 的生命周期及状态
返回栈 Android 中的 Activity 是可以层叠的,当我们启动一个新的 Activity 时,就会覆盖在原有的 Activity 之上, 点击 Back 会销毁当前 Activity,下面的 ...
随机推荐
- elasticsearch7.5.0+kibana-7.5.0+cerebro-0.8.5集群生产环境安装配置及通过elasticsearch-migration工具做新老集群数据迁移
一.服务器准备 目前有两台128G内存服务器,故准备每台启动两个es实例,再加一台虚机,共五个节点,保证down一台服务器两个节点数据不受影响. 二.系统初始化 参见我上一篇kafka系统初始化:ht ...
- Blazor(WebAssembly) + .NETCore 实现斗地主
之前群里大神发了一个 html5+ .NETCore的斗地主,刚好在看Blazor WebAssembly 就尝试重写试试. 还有就是有些标题党了,因为文章里几乎没有斗地主的相关实现:),这里主要介绍 ...
- 手摸手教你编写你人生中第一个HTML页面
本文是<HTML5与CSS3基础语法自学教程>的第二篇,首发于[前端课湛]微信公众号. 导读:本小节主要讲解 HTML 的基础语法内容,将通过编写第一个 HTML 页面来学习 HTML 的 ...
- Linux 命令行初始化MySQL数据库
作为一个技术纯小白,在Linux服务器初始化MySQL数据库的时候遇到了一点小问题: 1.不会使用MySQL图形工具,几乎没玩过 2.客户的VPN没有开放3306端口,没法用navicat等工 ...
- ORACLE spool打印
问题描述:spool让我想起来了spooling假脱机,但是这个spool是oracle下的命令,将select查询出来的数据打印出来 1.linuxi下 spool +路径+文件名,这里的文件如果不 ...
- java开发,入职半年。对未来迷茫,如何发展?
蛮多人私密我一些问题,关于面试,关于技术的,我只能说有些路只能靠自己去走,没人可以帮到自己,哪怕偶尔帮一到两次,但是技术的路这么长,总归需要自己独自成长的.附一张自己藏书的照片,与各位共勉 工作三年多 ...
- ElementUI table 点击编辑按钮进行编辑实现示例
<!DOCTYPE html> <html > <head> <meta charset="UTF-8"> <meta nam ...
- How to: Calculate a Property Value Based on Values from a Detail Collection 如何:基于详细信息集合中的值计算属性值
This topic describes how to implement a business class, so that one of its properties is calculated ...
- 《C#并发编程经典实例》学习笔记—3.1 数据的并行处理
问题 有一批数据,需要对每个元素进行相同的操作.该操作是计算密集型的,需要耗费一定的时间. 解决方案 常见的操作可以粗略分为 计算密集型操作 和 IO密集型操作.计算密集型操作主要是依赖于CPU计算, ...
- LiggEasyWinApp-103-Ligg.EasyWinForm:View
首先请在VS里打开下面的文件,我们将对源码分段进行说明: View(视图):一个Function(功能)对应多个View(视图),本框架用View(视图)实现多窗体(MDI)的效果. 一个View(视 ...