Spring-基于注解的配置[03Bean作用范围和生命周期方法]
- Bean的作用范围
- 实例
- Bean的生命周期方法
- 实例
Bean的作用范围
通过注解配置的Bean和通过<bean>
配置的Bean一样,默认的作用范围都是singleton。
Spring为注解配置提供了一个@Scope注解,可以通过它显示指定Bean的作用范围。
实例
代码已托管到Github—> https://github.com/yangshangwei/SpringMaster
package com.xgj.ioc.configuration.lifeCycle;import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;@Scope("prototype")
@Component
public class Teacher {private Student student;/*** * * @Title:Teacher* * @Description:构造函数*/public Teacher() {super();System.out.println("Teacher is initing....");}@Autowiredpublic void setStudent(Student student) {this.student = student;}
}
package com.xgj.ioc.configuration.lifeCycle;import org.springframework.stereotype.Component;@Component
public class Student {public Student() {super();System.out.println("Student is initing....");}}
配置文件
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"><!-- (1)声明Context命名空间以及Schema文件 (2)扫描类包以及应用注解定义的bean --><context:component-scan base-package="com.xgj.ioc.configuration.lifeCycle"/></beans>
测试类
package com.xgj.ioc.configuration.lifeCycle;import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;public class ScopeTest {public static void main(String[] args) {ApplicationContext ctx = new ClassPathXmlApplicationContext("classpath:com/xgj/ioc/configuration/lifeCycle/beanLifeCycle.xml");System.out.println("isPrototype:" + ctx.isPrototype("teacher"));System.out.println("isSingleton:" + ctx.isSingleton("teacher"));}
}
运行结果:
Bean的生命周期方法
@Scope注解通过入参指定Bean的作用范围。 在使用<bean>
进行配置可以通过init-method和destory属性指定Bean的初始化及容器销毁前执行的方法。
Spring从2.5开始支持JSR-250中定义的@PostConstruct和PreDestory注解。 在Spring中这两个注解相当于init-method和destroy-method属性的功能。 不过在使用注解时,可以在一个bean 中定义多个@PostConstruct和@PreDestory方法。
实例
我们取消掉Teacher类的 @Scope(“prototype”) 注解 (因为对于singleton的Bean,容器管理,prototype由调用者管理,Spring不管理) ,增加 @PostConstruct 和PreDestory。
改造如下
package com.xgj.ioc.configuration.lifeCycle;import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;//@Scope("prototype")
@Component
public class Teacher {private Student student;/*** * * @Title:Teacher* * @Description:构造函数*/public Teacher() {super();System.out.println("Teacher is initing....");}@Autowiredpublic void setStudent(Student student) {this.student = student;}@PostConstructpublic void init1() {System.out.println("init 1");}@PostConstructpublic void init2() {System.out.println("init 2");}@PreDestroypublic void destory1() {System.out.println("destory 1");}@PreDestroypublic void destory2() {System.out.println("destory 2");}}
测试类
package com.xgj.ioc.configuration.lifeCycle;import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;public class ScopeTest {public static void main(String[] args) {// 启动容器ApplicationContext ctx = new ClassPathXmlApplicationContext("classpath:com/xgj/ioc/configuration/lifeCycle/beanLifeCycle.xml");System.out.println("isPrototype:" + ctx.isPrototype("teacher"));System.out.println("isSingleton:" + ctx.isSingleton("teacher"));// 关闭容器,对于singleton的Bean,容器管理,prototype由调用者管理,Spring不管理((ClassPathXmlApplicationContext) ctx).destroy();}
}
运行结果:
由此可以看出,Spring先调用类的构造函数实例化Bean,然后在执行@Autowired进行自动注入,然后分别执行标注了@PostConstruct的方法,然后在容器关闭时,分别执行了标注@PreDestroy的方法。
Spring-基于注解的配置[03Bean作用范围和生命周期方法]相关推荐
- Spring 基于注解的配置
转载自 Spring 基于注解的配置 基于注解的配置 从 Spring 2.5 开始就可以使用注解来配置依赖注入.而不是采用 XML 来描述一个 bean 连线,你可以使用相关类,方法或字段声明的注 ...
- (spring-第4回【IoC基础篇】)spring基于注解的配置
(spring-第4回[IoC基础篇])spring基于注解的配置 基于XML的bean属性配置:bean的定义信息与bean的实现类是分离的. 基于注解的配置:bean的定义信息是通过在bean实现 ...
- spring基于注解的配置
转自:https://www.cnblogs.com/mesopotamia/p/4963659.html 基于XML的bean属性配置:bean的定义信息与bean的实现类是分离的. 基于注解的配置 ...
- Spring基于注解的配置概述
以下内容引用自http://wiki.jikexueyuan.com/project/spring/annotation-based-configuration.html: 从Spring 2.5开始 ...
- Spring中的bean的配置、作用范围、生命周期详细描述及使用(XML版上篇)
文章目录
- Spring IoC — 基于注解的配置
基于XML的配置,Bean定义信息和Bean实现类本身是分离的,而采用基于注解的配置方式时,Bean定义信息即通过在Bean实现类上标注注解实现. @Component:对类进行标注,Spring容器 ...
- spring 基于注解的控制器配置
http://ttaale.iteye.com/blog/787586 spring 基于注解的控制器配置 博客分类: spring SpringBeanServletMVCWeb 13.12. 基于 ...
- Spring(15)——基于注解的配置(二)
15.4 @Qualifier 15.4.1 简介 @Qualifier通常是配合@Autowired的一起使用的.我们知道使用@Autowired进行注入时默认是按照类型进行注入的.打个比方当我们使 ...
- Spring学习(六)-Bean作用域与基于注解的配置
Bean作用域 在配置文件中定义Bean时,用户不但可以配置Bean的属性值以及相互之间的依赖关系,还可以定义Bean的作用域.作用域将对Bean的生命周期和创建方式产生影响.Spring一般采用sc ...
最新文章
- 我的Pandas学习经历及动手实践
- Keep Walking!
- imx6 uboot lvds clock
- wxWidgets:TextCtrl示例
- rest_framework07:权限/频率/过滤组件/排序/异常处理封装Response对象
- MDaemon的邮件撤回功能详细介绍
- 五、stdout,stdoin和stderr
- 【luogu1816】忠(RMQ问题、线段树)
- 【Python 10】汇率兑换3.0(while循环)
- OPhone的多媒体模块支持的三种不同数据源
- 使用Leopard MVC
- Word2016以上版本兼容模式不能使用公式编辑器的解决办法
- 读书笔记202208 TRC2010 Multi-agent model predictive control of signaling split in urban traffic networks
- Altium net has no driving source问题
- 保温杯市场前景分析及行业研究报告
- 数字经济是党和国家定下的重要发展战略
- H264 概念之 I P B 帧
- 没有人能够一味地淡定,没有人能够一味地忍受
- Python海龟绘图,画花朵
- python设定字符串长度_python 修改字符串长度_Python 字符串操作
热门文章
- linux/CentOS7安装MySQL(完整版)【笔记自用】
- TensorRT trtexec的用法
- Python 位运算符号
- Leetcode 120. 三角形最小路径和 (每日一题 20210927)
- Leetcode 130. 被围绕的区域 (每日一题 20210720 同类型题)
- 拼接所有字符串产生字典顺序最小的大写字符串
- 机器学习应用方向(二)~概念漂移(concept drift)
- 基于MATLAB均值漂移图像分割技术
- Hadoop学习之MapReduce(四)
- MATLAB怎么查找矩阵中所有0的数据并赋值