(3)Spring的三种实例 bean 的方式 以及 Spring中bean的作用域。 以及 bean的生命周期

本文介绍Spring框架中Bean的三种实例化方式:构造器实例化、静态工厂方法实例化及实例工厂方法实例化,并详细讲解Bean的作用域、生命周期管理及初始化与销毁方法的指定。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

第一种:

1.使用类构造器实例化

 <bean id="personService" class="cn.itm.service.impl.PersonServiceBean"></bean>

import cn.itm.service.PersonService;

public class PersonServiceBean implements PersonService{

	public void save(){
		System.out.println("我是 save() 方法");
	}
}


public class SpringTest {

	@BeforeClass
	public static void setUpBeforeClass() throws Exception {
	}

	// 专门用来实例化  Spring 容器的。
	@Test public void 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 static void setUpBeforeClass() throws Exception {
	}

	// 专门用来实例化  Spring 容器的。
	@Test public void 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 static void setUpBeforeClass() throws Exception {
	}

	// 专门用来实例化  Spring 容器的。
	@Test public void instanceSpring(){
		// 在类路径下,寻找配置文件来实例化容器。
		ApplicationContext ctx = new ClassPathXmlApplicationContext("beans.xml");
		
		// 填写 bean的名称,也就是 id属性的值:personService。获取后,就可以通过   接口  引用。
		PersonService personService = (PersonService) ctx.getBean("personService3");
		//调用  业务方法:
		personService.save();
	}
}





Spring中  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>

public class SpringTest {

	@BeforeClass
	public static void setUpBeforeClass() throws Exception {
	}

	// 专门用来实例化  Spring 容器的。
	@Test public void 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 static void setUpBeforeClass() throws Exception {
	}

	// 专门用来实例化  Spring 容器的。
	@Test public void 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都是新的对象。


生命周期的测试:

<?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进行实例化。。而加入  scope="prototype" 时,是在调用getBean方法的时候,实例化的 -->
           <bean id="personService" class="cn.itm.service.impl.PersonServiceBean"></bean>
           
    
</beans>

import cn.itm.service.PersonService;

public class PersonServiceBean implements PersonService{

	public PersonServiceBean(){
		System.out.println("我被实例化了");
	}
	public void save(){
		System.out.println("我是 save() 方法");
	}
}

测试类:

public class SpringTest {

	@BeforeClass
	public static void setUpBeforeClass() throws Exception {
	}

	// 专门用来实例化  Spring 容器的。
	@Test public void 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"/>


<?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="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 PersonServiceBean implements 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 static void setUpBeforeClass() throws Exception {
	}

	// 专门用来实例化  Spring 容器的。
	@Test public void instanceSpring(){
		
		AbstractApplicationContext ctx = new ClassPathXmlApplicationContext("beans.xml");
		ctx.close();
	}
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值