(2)Spring的三种实例 bean 的方式 以及 Spring中bean的作用域。 以及 bean的生命周期
来源:互联网 发布:外国人用淘宝吗? 编辑:程序博客网 时间:2024/05/16 00:37
第一种:
1.使用类构造器实例化
<bean id="personService" class="cn.itm.service.impl.PersonServiceBean"></bean>
- import cn.itm.service.PersonService;
- public class PersonServiceBeanimplements PersonService{
- public void save(){
- System.out.println("我是 save() 方法");
- }
- }
- public class SpringTest {
- @BeforeClass
- public staticvoid setUpBeforeClass() throws Exception {
- }
- // 专门用来实例化 Spring 容器的。
- @Test publicvoid instanceSpring(){
- // 在类路径下,寻找配置文件来实例化容器。
- ApplicationContext ctx = new ClassPathXmlApplicationContext("beans.xml");
- // 填写 bean的名称,也就是 id属性的值:personService。获取后,就可以通过 接口 引用。
- PersonService personService = (PersonService) ctx.getBean("personService");
- //调用 业务方法:
- personService.save();
- }
- }
第二种:
2.使用静态工厂方法实例化
<bean id="personService2" class="cn.itm.service.impl.PersonServiceBeanFactory"
factory-method="createPersonServiceBean"></bean>
-----------------------------------------------------------------------------------------------------------------------------
public staticPersonServiceBean createPersonServiceBean(){
return new PersonServiceBean();
}
- package cn.itm.service.impl;
- public class PersonServiceBeanFactory {
- // 新建一个工厂方法,这个工厂方法用来创建一个bean对象。
- /**
- * PersonServiceBean 使用 PersonServiceBeanFactory的createPersonServiceBean()创建的。
- *
- */
- public static PersonServiceBean createPersonServiceBean(){
- return new PersonServiceBean();
- }
- }
- public class SpringTest {
- @BeforeClass
- public staticvoid setUpBeforeClass() throws Exception {
- }
- // 专门用来实例化 Spring 容器的。
- @Test publicvoid instanceSpring(){
- // 在类路径下,寻找配置文件来实例化容器。
- ApplicationContext ctx = new ClassPathXmlApplicationContext("beans.xml");
- // 填写 bean的名称,也就是 id属性的值:personService。获取后,就可以通过 接口 引用。
- PersonService personService = (PersonService) ctx.getBean("personService2");
- //调用 业务方法:
- personService.save();
- }
- }
第三种:使用实例工厂方法实例化:
<bean id="personServiceFactory" class="cn.itm.service.impl.PersonServiceBeanFactory" ></bean>
<bean id="personService3" factory-bean="personServiceFactory"
factory-method="createPersonServiceBean"></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"
- xsi:schemaLocation="http://www.springframework.org/schema/beans
- http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
- <bean id="personServiceFactory"class="cn.itm.service.impl.PersonServiceBeanFactory" ></bean>
- <bean id="personService3" factory-bean="personServiceFactory"
- factory-method="createPersonServiceBean"></bean>
- </beans>
相比第二种:把 static 关键字去掉了。
- package cn.itm.service.impl;
- public class PersonServiceBeanFactory {
- public PersonServiceBean createPersonServiceBean(){
- return new PersonServiceBean();
- }
- }
- public class SpringTest {
- @BeforeClass
- public staticvoid setUpBeforeClass() throws Exception {
- }
- // 专门用来实例化 Spring 容器的。
- @Test publicvoid instanceSpring(){
- // 在类路径下,寻找配置文件来实例化容器。
- ApplicationContext ctx = new ClassPathXmlApplicationContext("beans.xml");
- // 填写 bean的名称,也就是 id属性的值:personService。获取后,就可以通过 接口 引用。
- PersonService personService = (PersonService) ctx.getBean("personService3");
- //调用 业务方法:
- personService.save();
- }
- }
Spring中 bean的作用域:
- <?xmlversion="1.0"encoding="UTF-8"?>
- <beans xmlns="http://www.springframework.org/schema/beans"
- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xsi:schemaLocation="http://www.springframework.org/schema/beans
- http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
- <beanid="personServiceFactory"class="cn.itm.service.impl.PersonServiceBeanFactory"></bean>
- <beanid="personService3"factory-bean="personServiceFactory"
- factory-method="createPersonServiceBean"></bean>
- </beans>
- public class SpringTest {
- @BeforeClass
- public staticvoid setUpBeforeClass() throws Exception {
- }
- // 专门用来实例化 Spring 容器的。
- @Test publicvoid instanceSpring(){
- // 在类路径下,寻找配置文件来实例化容器。
- ApplicationContext ctx = new ClassPathXmlApplicationContext("beans.xml");
- // 填写 bean的名称,也就是 id属性的值:personService。获取后,就可以通过 接口 引用。
- PersonService personService1 = (PersonService) ctx.getBean("personService3");
- PersonService personService2 = (PersonService) ctx.getBean("personService3");
- System.out.println( personService1 == personService2);
- }
- }
打印结果:true。
<bean id="personServiceFactory" class="cn.itm.service.impl.PersonServiceBeanFactory" ></bean>
<bean id="personService3" factory-bean="personServiceFactory"
factory-method="createPersonServiceBean" scope="prototype"></bean>
- public class SpringTest {
- @BeforeClass
- public staticvoid setUpBeforeClass() throws Exception {
- }
- // 专门用来实例化 Spring 容器的。
- @Test publicvoid instanceSpring(){
- // 在类路径下,寻找配置文件来实例化容器。
- ApplicationContext ctx = new ClassPathXmlApplicationContext("beans.xml");
- // 填写 bean的名称,也就是 id属性的值:personService。获取后,就可以通过 接口 引用。
- PersonService personService1 = (PersonService) ctx.getBean("personService3");
- PersonService personService2 = (PersonService) ctx.getBean("personService3");
- System.out.println( personService1 == personService2);
- }
- }
打印结果:false。
【1】singleton
在每个Spring IoC容器中一个bean定义只有一个对象实例。
默认情况下会在容器启动时初始化bean,但我们可以指定Bean节点的lazy-init=“true”来延迟初始化bean,这时候,只有第一次获取bean会才初始化bean。如:
<bean id="xxx" class="cn.itcast.OrderServiceBean" lazy-init="true"/>
如果想对所有bean都应用延迟初始化,可以在根节点beans设置default-lazy-init=“true“,如下:
<beans default-lazy-init="true“ ...>
【2】prototype
每次从容器获取bean都是新的对象。
生命周期的测试:
- <?xmlversion="1.0"encoding="UTF-8"?>
- <beans xmlns="http://www.springframework.org/schema/beans"
- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xsi:schemaLocation="http://www.springframework.org/schema/beans
- http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
- <!-- 在容器实例化的时候,就会对bean进行实例化。。而加入 scope="prototype" 时,是在调用getBean方法的时候,实例化的 -->
- <beanid="personService"class="cn.itm.service.impl.PersonServiceBean"></bean>
- </beans>
- import cn.itm.service.PersonService;
- public class PersonServiceBeanimplements PersonService{
- public PersonServiceBean(){
- System.out.println("我被实例化了");
- }
- public void save(){
- System.out.println("我是 save() 方法");
- }
- }
测试类:
- public class SpringTest {
- @BeforeClass
- public staticvoid setUpBeforeClass() throws Exception {
- }
- // 专门用来实例化 Spring 容器的。
- @Test publicvoid instanceSpring(){
- // 在类路径下,寻找配置文件来实例化容器。
- ApplicationContext ctx = new ClassPathXmlApplicationContext("beans.xml");
- // PersonService personService1 = (PersonService) ctx.getBean("personService3");
- }
- }
打印结果:我被实例化了。
在每个Spring IoC容器中一个bean定义只有一个对象实例。
默认情况下会在容器启动时初始化bean,但我们可以指定Bean节点的lazy-init=“true”来延迟初始化bean,这时候,只有第一次获取bean会才初始化bean。如:
<bean id="xxx" class="cn.itcast.OrderServiceBean" lazy-init="true"/>
如果想对所有bean都应用延迟初始化,可以在根节点beans设置default-lazy-init=“true“,如下:
<beans default-lazy-init="true“ ...>
指定Bean的初始化方法和销毁方法
<bean id="xxx" class="cn.itcast.OrderServiceBean" init-method="init"
destroy-method="close"/>
- <?xmlversion="1.0"encoding="UTF-8"?>
- <beans xmlns="http://www.springframework.org/schema/beans"
- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xsi:schemaLocation="http://www.springframework.org/schema/beans
- http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
- <beanid="personService"class="cn.itm.service.impl.PersonServiceBean"
- lazy-init="false"destroy-method="destory"></bean>
- </beans>
- package cn.itm.service.impl;
- import cn.itm.service.PersonService;
- public class PersonServiceBeanimplements PersonService{
- public PersonServiceBean(){
- System.out.println("我被实例化了");
- }
- public void save(){
- System.out.println("我是 save() 方法");
- }
- public void destory(){
- System.out.println("关闭");
- }
- }
AbstractApplicationContext
- public class SpringTest {
- @BeforeClass
- public staticvoid setUpBeforeClass() throws Exception {
- }
- // 专门用来实例化 Spring 容器的。
- @Test publicvoid instanceSpring(){
- AbstractApplicationContext ctx = new ClassPathXmlApplicationContext("beans.xml");
- ctx.close();
- }
- }
- (2)Spring的三种实例 bean 的方式 以及 Spring中bean的作用域。 以及 bean的生命周期
- (3)Spring的三种实例 bean 的方式 以及 Spring中bean的作用域。 以及 bean的生命周期
- spring三种实例化Bean的方式和管理Bean的作用域和Bean的生命周期
- 【SSH三大框架】Spring基础第一篇:搭建Spring环境、实例化Bean、管理Bean的作用域以及Bean的生命周期
- Spring中Bean的理解以及@Bean的作用
- Spring中Bean的作用域、生命周期
- spring实例bean的三种方式
- Spring中bean工厂创建bean的三种方式
- Bean作用域的配置以及 Spring各种注入方式实例 list set map props
- Bean作用域的配置以及 Spring各种注入方式实例 list set map props
- Spring之Bean容器初始化以及bean实例的获取
- Spring中实例化Bean的三种方式
- Spring中 三种实例化bean的方式
- Spring中bean实例化的三种方式
- Spring Bean--Bean的配置项、作用域、生命周期
- Spring - bean的作用域与生命周期
- spring bean的作用域和生命周期
- Spring Bean的生命周期和作用域
- 面向对象(三)
- STM32 ID
- Android系统进程间通信(IPC)机制Binder中的Server启动过程源代码分析
- Android系统进程间通信(IPC)机制Binder中的Client获得Server远程接口过程源代码分析
- (1)搭建与测试 Spring 的开发环境
- (2)Spring的三种实例 bean 的方式 以及 Spring中bean的作用域。 以及 bean的生命周期
- Android系统进程间通信Binder机制在应用程序框架层的Java接口源代码分析
- (3) 利用 Setter方式实现 【第一种方式】 依赖注入,编码剖析Spring依赖注入的原理
- 内核模块
- (4) 编码剖析Spring装配基本属性的原理【附加:注入依赖对象的两种方式】
- 君已老
- (5) Spring 如何装配 集合类 以及 【第二种 依赖注入 方式】使用 构造器 注入
- 电容基础知识 之 钽电解电容
- Android系统匿名共享内存Ashmem(Anonymous Shared Memory)简要介绍和学习计划