前言

在applicationContext.xml中配置完bean之后,Bean的声明周期状态有哪些。生命周期的各个阶段可以做什么。在applicationContext.xml配置bean的作用域有哪些。其中各个作用域代表的是什么。适用于什么情况。这篇文章做一个记录。
生命周期
初始化
可以直接查看图片,图片来自Spring Bean Life Cycle
从上图看出,Bean初始化完成包括9个步骤。其中一些步骤包括接口的实现,其中包括BeanNameAware接口,BeanFactoryAware接口。ApplicationContextAware接口。BeanPostProcessor接口,InitializingBean接口。那么这些接口在整个生命周期阶段都起到什么作用?后面我们一一介绍。
实例化前
当Bean全部属性设置完毕后,往往需要执行一些特定的行为,Spring提供了两种方式来实现此功能:
指定初始化方法
如下:
package com.model;
public class InitBean {
public static final String NAME = "mark";
public static final int AGE = 20;
public InitBean() {
// TODO Auto-generated constructor stub
System.out.println("执行构造方法");
}
public String name;
public int age ;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public void init(){
System.out.println("调用init方法进行成员变量的初始化");
this.name = NAME;
this.age = AGE;
System.out.println("初始化完成");
}
}
编写加载器
package com.model;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.service.UserServiceImpl;
public class Main {
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("initbean.xml");
InitBean bean = (InitBean) context.getBean("init");
}
}
配置Bean
注意init-method参数
<?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:p="http://www.springframework.org/schema/p" xmlns:aop="http://www.springframework.org/schema/aop" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> <bean id="init" class="com.model.InitBean" init-method="init"/> </beans>
执行结果
实现InitializingBean接口
实现InitializingBean接口会实现afterPropertiesSet方法,这个方法会自动调用。但是这个方式是侵入性的。一般情况下,不建议使用。
实现afterPropertiesSet方法
package com.model;
import org.springframework.beans.factory.InitializingBean;
public class InitBean implements InitializingBean {
public static final String NAME = "mark";
public static final int AGE = 20;
public InitBean() {
// TODO Auto-generated constructor stub
System.out.println("执行构造方法");
}
public String name;
public int age ;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public void init(){
System.out.println("调用init方法进行成员变量的初始化");
this.name = NAME;
this.age = AGE;
System.out.println("初始化完成");
}
@Override
public void afterPropertiesSet() throws Exception {
// TODO Auto-generated method stub
System.out.println("调用init方法进行成员变量的初始化");
this.name = NAME;
this.age = AGE;
System.out.println("初始化完成");
}
}
配置xml
<?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:p="http://www.springframework.org/schema/p" xmlns:aop="http://www.springframework.org/schema/aop" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> <!-- <bean id="init" class="com.model.InitBean" init-method="init"/> --> <bean id="init" class="com.model.InitBean" init-method="init"/> </beans>
结果:
销毁
同样,上图中表示来Bean销毁时候的过程。包括DisposableBean接口。
使用destroy-method方法
package com.model;
import org.springframework.beans.factory.InitializingBean;
public class InitBean implements InitializingBean {
public static final String NAME = "mark";
public static final int AGE = 20;
public InitBean() {
// TODO Auto-generated constructor stub
System.out.println("执行构造方法");
}
public String name;
public int age ;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public void init(){
System.out.println("调用init方法进行成员变量的初始化");
this.name = NAME;
this.age = AGE;
System.out.println("初始化完成");
}
@Override
public void afterPropertiesSet() throws Exception {
// TODO Auto-generated method stub
System.out.println("调用init方法进行成员变量的初始化");
this.name = NAME;
this.age = AGE;
System.out.println("初始化完成");
}
public void close(){
System.out.println("bean被销毁");
}
}
配置Bean
<?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:p="http://www.springframework.org/schema/p" xmlns:aop="http://www.springframework.org/schema/aop" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> <!-- <bean id="init" class="com.model.InitBean" init-method="init"/> --> <bean id="init" class="com.model.InitBean" destroy-method="close"/> </beans>
配置加载器
package com.model;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.AbstractApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.service.UserServiceImpl;
public class Main {
public static void main(String[] args) {
AbstractApplicationContext context = new ClassPathXmlApplicationContext("initbean.xml");
context.registerShutdownHook();
InitBean bean = (InitBean) context.getBean("init");
}
}
结果:
实现DisposableBean接口
实现DisposableBean接口
package com.model;
import org.springframework.beans.factory.DisposableBean;
import org.springframework.beans.factory.InitializingBean;
public class InitBean implements InitializingBean,DisposableBean {
public static final String NAME = "mark";
public static final int AGE = 20;
public InitBean() {
// TODO Auto-generated constructor stub
System.out.println("执行构造方法");
}
public String name;
public int age ;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public void init(){
System.out.println("调用init方法进行成员变量的初始化");
this.name = NAME;
this.age = AGE;
System.out.println("初始化完成");
}
@Override
public void afterPropertiesSet() throws Exception {
// TODO Auto-generated method stub
System.out.println("调用init方法进行成员变量的初始化");
this.name = NAME;
this.age = AGE;
System.out.println("初始化完成");
}
public void close(){
System.out.println("bean被销毁");
}
@Override
public void destroy() throws Exception {
// TODO Auto-generated method stub
System.out.println("bean被销毁完成");
}
}
配置文件
<?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:p="http://www.springframework.org/schema/p" xmlns:aop="http://www.springframework.org/schema/aop" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> <!-- <bean id="init" class="com.model.InitBean" init-method="init"/> --> <!-- <bean id="init" class="com.model.InitBean" destroy-method="close"/> --> <bean id="init" class="com.model.InitBean"/> </beans>
Spring Bean的作用域
| 作用域 | 描述 |
| singleton | 该作用域将 bean 的定义的限制在每一个 Spring IoC 容器中的一个单一实例(默认)。 |
| prototype | 该作用域将单一 bean 的定义限制在任意数量的对象实例。 |
| request | 该作用域将 bean 的定义限制为 HTTP 请求。只在 web-aware Spring ApplicationContext 的上下文中有效。 |
| session | 该作用域将 bean 的定义限制为 HTTP 会话。 只在web-aware Spring ApplicationContext的上下文中有效。 |
| global-session | 该作用域将 bean 的定义限制为全局 HTTP 会话。只在 web-aware Spring ApplicationContext 的上下文中有效。 |
配置示例
<bean id="..." class="..." scope="singleton"> </bean>
使用方法注入协调作用域不同的Bean
正常情况下,如果singleton作用域依赖singleton作用域。即每次获取到的都是一样的对象。同理,prototype作用域依赖prototype作用域,每次获取到的都是新的对象。但是,如果singleton依赖prototype作用域,那么每次获取到的singleton中的prototype都是第一次创建的prototype。如何协调这种关系。保证每次获取到的都是正确的呢。
对于这种情况,Spring提供了lookup方法用来解决这种问题。
首先我们定义一个原型:
package com.model;
public class MyHelper {
public void doSomethingHelpful() {
}
}
然后通过接口注入:
package com.model;
public interface DemoBean {
MyHelper getHelper();
void somePeration();
}
配置一个单例:
package com.model;
/**
* 测试类
* @author kevin
*
*/
public abstract class AbstractLookupDemo implements DemoBean {
public abstract MyHelper getMyHelper();
@Override
public MyHelper getHelper() {
// TODO Auto-generated method stub
return getMyHelper();
}
@Override
public void somePeration() {
// TODO Auto-generated method stub
}
}
配置文件:
<?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:p="http://www.springframework.org/schema/p" xmlns:aop="http://www.springframework.org/schema/aop" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> <bean id="helper" class="com.model.MyHelper" scope="prototype"/> <bean id="standardLookupBean" class="com.model.StandardLookupDemo"> <property name="myHelper" ref="helper"></property> </bean> <bean id = "abstractLookupBean" class="com.model.AbstractLookupDemo"> <lookup-method name="getMyHelper" bean="helper"></lookup-method> </bean> </beans>
加载配置文件:
package com.model;
import org.springframework.context.support.AbstractApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.util.StopWatch;
public class Main {
public static void main(String[] args) {
AbstractApplicationContext context = new ClassPathXmlApplicationContext("lookBean.xml");
context.registerShutdownHook();
System.out.println("传递standardLookupBean");
test(context, "standardLookupBean");
System.out.println("传递AbstractLookupDemo");
test(context, "abstractLookupBean");
}
public static void test(AbstractApplicationContext context,String beanName) {
DemoBean bean = (DemoBean) context.getBean(beanName);
MyHelper helper1 = bean.getHelper();
MyHelper helper2 = bean.getHelper();
System.out.println("测试"+beanName);
System.out.println("两个helper是否相同?"+(helper1==helper2));
StopWatch stopWatch = new StopWatch();
stopWatch.start("lookupDemo");
for (int i = 0; i < 10000; i++) {
MyHelper helper = bean.getHelper();
helper.doSomethingHelpful();
}
stopWatch.stop();
System.out.println("获取10000次花费了"+stopWatch.getTotalTimeMillis()+"毫秒");
}
}
结果:
从上面的结果图看出,以前的方式生成的对象每次都是相同的。通过lookup方式注入每次是不同的。可以解决这种问题。但是有没有更简单的方式,感觉这种方式优点麻烦。
让Bean感知Spring容器
实现BeanNameAware,自定设置id值。
实现BeanFactoryAware,ApplicationContextAware 感知Spring容器。获取Spring容器。
Spring国际化支持
配置配置文件
<?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:p="http://www.springframework.org/schema/p" xmlns:aop="http://www.springframework.org/schema/aop" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> <bean id="messageSource" class="org.springframework.context.support.ResourceBundleMessageSource"> <property name="basenames"> <list> <value>message</value> </list> </property> </bean> </beans>
新建中文配置文件
message_zh_CN.properties:
hello=welcome,{0}
now=now is : {0}
新建英文配置文件
message_en_US.properties:
hello=\u4F60\u597D,{0}
now=\u73B0\u5728\u7684\u65F6\u95F4\u662F : {0}
加载配置文件
package com.model;
import java.util.Date;
import java.util.Locale;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.AbstractApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.util.StopWatch;
public class Main {
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("globalization.xml");
String[] a = {"读者"};
String hello = context.getMessage("hello",a, Locale.CHINA);
Object[] b = {new Date()};
String now = context.getMessage("now",b, Locale.CHINA);
System.out.println(hello);
System.out.println(now);
hello = context.getMessage("hello",a, Locale.US);
now = context.getMessage("now",b, Locale.US);
System.out.println(hello);
System.out.println(now);
}
}
结果
总结
好了,以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作能带来一定的帮助,如果有疑问大家可以留言交流,谢谢大家对的支持。
# spring
# bean生命周期
# bean作用域
# bean的作用域
# 浅谈Spring中Bean的作用域、生命周期
# Spring实战之Bean的作用域request用法分析
# Spring实战之Bean的作用域singleton和prototype用法分析
# 深入了解Spring中Bean的作用域和生命周期
# spring ioc的简单实例及bean的作用域属性解析
# JSP 中Spring Bean 的作用域详解
# 简单了解spring bean作用域属性singleton和prototype的区别
# Spring中Bean的作用域与生命周期详解
# 详解Spring中Bean的作用域与生命周期
# Spring 框架中的 Bean 作用域(Scope)使用详解
# 都是
# 配置文件
# 只在
# 加载
# 这篇文章
# 的是
# 好了
# 有哪些
# 两种
# 做什么
# 适用于
# 英文
# 可以直接
# 做一个
# 这种情况
# 谢谢大家
# 来实现
# 图中
# 自定
# 上图
相关文章:
相亲简历制作网站推荐大全,新相亲大会主持人小萍萍资料?
专业网站设计制作公司,如何制作一个企业网站,建设网站的基本步骤有哪些?
,制作一个手机app网站要多少钱?
厦门模型网站设计制作公司,厦门航空飞机模型掉色怎么办?
在线ppt制作网站有哪些软件,如何把网页的内容做成ppt?
导航网站建站方案与优化指南:一站式高效搭建技巧解析
建站中国必看指南:CMS建站系统+手机网站搭建核心技巧解析
如何在IIS中新建站点并解决端口绑定冲突?
公司门户网站制作公司有哪些,怎样使用wordpress制作一个企业网站?
佛山网站制作系统,佛山企业变更地址网上办理步骤?
建站之星后台搭建步骤解析:模板选择与产品管理实操指南
实现点击下箭头变上箭头来回切换的两种方法【推荐】
公司网站的制作公司,企业网站制作基本流程有哪些?
建站之星免费版是否永久可用?
如何获取上海专业网站定制建站电话?
电脑免费海报制作网站推荐,招聘海报哪个网站多?
黑客如何利用漏洞与弱口令入侵网站服务器?
如何用景安虚拟主机手机版绑定域名建站?
c++怎么编写动态链接库dll_c++ __declspec(dllexport)导出与调用【方法】
家庭服务器如何搭建个人网站?
如何通过二级域名建站提升品牌影响力?
如何选择建站程序?包含哪些必备功能与类型?
关于BootStrap modal 在IOS9中不能弹出的解决方法(IOS 9 bootstrap modal ios 9 noticework)
制作网站的软件免费下载,免费制作app哪个平台好?
网站微信制作软件,如何制作微信链接?
Android自定义控件实现温度旋转按钮效果
较简单的网站制作软件有哪些,手机版网页制作用什么软件?
在线流程图制作网站手机版,谁能推荐几个好的CG原画资源网站么?
如何高效配置IIS服务器搭建网站?
如何快速搭建高效可靠的建站解决方案?
如何构建满足综合性能需求的优质建站方案?
建站主机选哪种环境更利于SEO优化?
建站主机选购指南:核心配置与性价比推荐解析
测试制作网站有哪些,测试性取向的权威测试或者网站?
TestNG的testng.xml配置文件怎么写
常州企业网站制作公司,全国继续教育网怎么登录?
如何快速选择适合个人网站的云服务器配置?
教学网站制作软件,学习*后期制作的网站有哪些?
网站网页制作电话怎么打,怎样安装和使用钉钉软件免费打电话?
南平网站制作公司,2025年南平市事业单位报名时间?
如何用已有域名快速搭建网站?
如何使用Golang安装API文档生成工具_快速生成接口文档
义乌企业网站制作公司,请问义乌比较好的批发小商品的网站是什么?
网站规划与制作是什么,电子商务网站系统规划的内容及步骤是什么?
网站制作难吗安全吗,做一个网站需要多久时间?
网站视频怎么制作,哪个网站可以免费收看好莱坞经典大片?
北京营销型网站制作公司,可以用python做一个营销推广网站吗?
如何快速查询网站的真实建站时间?
浅析上传头像示例及其注意事项
零基础网站服务器架设实战:轻量应用与域名解析配置指南
*请认真填写需求信息,我们会在24小时内与您取得联系。