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

来源:互联网 发布:外国人用淘宝吗? 编辑:程序博客网 时间:2024/05/16 00:37

第一种:

1.使用类构造器实例化

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

[java] view plaincopyprint?
  1. import cn.itm.service.PersonService; 
  2.  
  3. public class PersonServiceBeanimplements PersonService{ 
  4.  
  5.     public void save(){ 
  6.         System.out.println("我是 save() 方法"); 
  7.     } 


[java] view plaincopyprint?
  1. public class SpringTest { 
  2.  
  3.     @BeforeClass 
  4.     public staticvoid setUpBeforeClass() throws Exception { 
  5.     } 
  6.  
  7.     // 专门用来实例化  Spring 容器的。 
  8.     @Test publicvoid instanceSpring(){ 
  9.         // 在类路径下,寻找配置文件来实例化容器。 
  10.         ApplicationContext ctx = new ClassPathXmlApplicationContext("beans.xml"); 
  11.          
  12.         // 填写 bean的名称,也就是 id属性的值:personService。获取后,就可以通过   接口  引用。 
  13.         PersonService personService = (PersonService) ctx.getBean("personService"); 
  14.         //调用  业务方法: 
  15.         personService.save(); 
  16.     } 

第二种:

2.使用静态工厂方法实例化

<bean id="personService2" class="cn.itm.service.impl.PersonServiceBeanFactory"
                    factory-method="createPersonServiceBean"></bean>

-----------------------------------------------------------------------------------------------------------------------------

    public staticPersonServiceBean createPersonServiceBean(){
        return new PersonServiceBean();
    }

[java] view plaincopyprint?
  1. package cn.itm.service.impl; 
  2.  
  3. public class PersonServiceBeanFactory { 
  4.  
  5.     // 新建一个工厂方法,这个工厂方法用来创建一个bean对象。 
  6.     /**
  7.      *  PersonServiceBean 使用 PersonServiceBeanFactory的createPersonServiceBean()创建的。
  8.      *
  9.      */ 
  10.     public static PersonServiceBean createPersonServiceBean(){ 
  11.         return new PersonServiceBean(); 
  12.     } 

[java] view plaincopyprint?
  1. public class SpringTest { 
  2.  
  3.     @BeforeClass 
  4.     public staticvoid setUpBeforeClass() throws Exception { 
  5.     } 
  6.  
  7.     // 专门用来实例化  Spring 容器的。 
  8.     @Test publicvoid instanceSpring(){ 
  9.         // 在类路径下,寻找配置文件来实例化容器。 
  10.         ApplicationContext ctx = new ClassPathXmlApplicationContext("beans.xml"); 
  11.          
  12.         // 填写 bean的名称,也就是 id属性的值:personService。获取后,就可以通过   接口  引用。 
  13.         PersonService personService = (PersonService) ctx.getBean("personService2"); 
  14.         //调用  业务方法: 
  15.         personService.save(); 
  16.     } 


第三种:使用实例工厂方法实例化:


           <bean id="personServiceFactory" class="cn.itm.service.impl.PersonServiceBeanFactory" ></bean>
           <bean id="personService3" factory-bean="personServiceFactory"
                           factory-method="createPersonServiceBean"></bean>


[java] view plaincopyprint?
  1. <?xml version="1.0" encoding="UTF-8"?> 
  2. <beans xmlns="http://www.springframework.org/schema/beans" 
  3.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
  4.        xsi:schemaLocation="http://www.springframework.org/schema/beans 
  5.            http://www.springframework.org/schema/beans/spring-beans-2.5.xsd"> 
  6.             
  7.             
  8.             
  9.            <bean id="personServiceFactory"class="cn.itm.service.impl.PersonServiceBeanFactory" ></bean> 
  10.            <bean id="personService3" factory-bean="personServiceFactory"  
  11.                         factory-method="createPersonServiceBean"></bean> 
  12.             
  13. </beans> 

相比第二种:把 static 关键字去掉了。


[java] view plaincopyprint?
  1. package cn.itm.service.impl; 
  2.  
  3. public class PersonServiceBeanFactory { 
  4.      
  5.     public PersonServiceBean createPersonServiceBean(){ 
  6.         return new PersonServiceBean(); 
  7.     } 
  8.      
  9.      

[java] view plaincopyprint?
  1. public class SpringTest { 
  2.  
  3.     @BeforeClass 
  4.     public staticvoid setUpBeforeClass() throws Exception { 
  5.     } 
  6.  
  7.     // 专门用来实例化  Spring 容器的。 
  8.     @Test publicvoid instanceSpring(){ 
  9.         // 在类路径下,寻找配置文件来实例化容器。 
  10.         ApplicationContext ctx = new ClassPathXmlApplicationContext("beans.xml"); 
  11.          
  12.         // 填写 bean的名称,也就是 id属性的值:personService。获取后,就可以通过   接口  引用。 
  13.         PersonService personService = (PersonService) ctx.getBean("personService3"); 
  14.         //调用  业务方法: 
  15.         personService.save(); 
  16.     } 





Spring中  bean的作用域:

[html] view plaincopyprint?
  1. <?xmlversion="1.0"encoding="UTF-8"?> 
  2. <beans xmlns="http://www.springframework.org/schema/beans" 
  3.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
  4.        xsi:schemaLocation="http://www.springframework.org/schema/beans 
  5.            http://www.springframework.org/schema/beans/spring-beans-2.5.xsd"> 
  6.             
  7.             
  8.             
  9.            <beanid="personServiceFactory"class="cn.itm.service.impl.PersonServiceBeanFactory"></bean> 
  10.            <beanid="personService3"factory-bean="personServiceFactory"  
  11.                         factory-method="createPersonServiceBean"></bean> 
  12.             
  13. </beans> 

[java] view plaincopyprint?
  1. public class SpringTest { 
  2.  
  3.     @BeforeClass 
  4.     public staticvoid setUpBeforeClass() throws Exception { 
  5.     } 
  6.  
  7.     // 专门用来实例化  Spring 容器的。 
  8.     @Test publicvoid instanceSpring(){ 
  9.         // 在类路径下,寻找配置文件来实例化容器。 
  10.         ApplicationContext ctx = new ClassPathXmlApplicationContext("beans.xml"); 
  11.          
  12.         // 填写 bean的名称,也就是 id属性的值:personService。获取后,就可以通过   接口  引用。 
  13.         PersonService personService1 = (PersonService) ctx.getBean("personService3"); 
  14.         PersonService personService2 = (PersonService) ctx.getBean("personService3"); 
  15.          
  16.         System.out.println( personService1 == personService2); 
  17.     } 

打印结果:true。


<bean id="personServiceFactory" class="cn.itm.service.impl.PersonServiceBeanFactory" ></bean>
           <bean id="personService3" factory-bean="personServiceFactory"
                           factory-method="createPersonServiceBean" scope="prototype"></bean>

[java] view plaincopyprint?
  1. public class SpringTest { 
  2.  
  3.     @BeforeClass 
  4.     public staticvoid setUpBeforeClass() throws Exception { 
  5.     } 
  6.  
  7.     // 专门用来实例化  Spring 容器的。 
  8.     @Test publicvoid instanceSpring(){ 
  9.         // 在类路径下,寻找配置文件来实例化容器。 
  10.         ApplicationContext ctx = new ClassPathXmlApplicationContext("beans.xml"); 
  11.          
  12.         // 填写 bean的名称,也就是 id属性的值:personService。获取后,就可以通过   接口  引用。 
  13.         PersonService personService1 = (PersonService) ctx.getBean("personService3"); 
  14.         PersonService personService2 = (PersonService) ctx.getBean("personService3"); 
  15.          
  16.         System.out.println( personService1 == personService2); 
  17.     } 

打印结果: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都是新的对象。


生命周期的测试:

[html] view plaincopyprint?
  1. <?xmlversion="1.0"encoding="UTF-8"?> 
  2. <beans xmlns="http://www.springframework.org/schema/beans" 
  3.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
  4.        xsi:schemaLocation="http://www.springframework.org/schema/beans 
  5.            http://www.springframework.org/schema/beans/spring-beans-2.5.xsd"> 
  6.             
  7.            <!-- 在容器实例化的时候,就会对bean进行实例化。。而加入  scope="prototype" 时,是在调用getBean方法的时候,实例化的 --> 
  8.            <beanid="personService"class="cn.itm.service.impl.PersonServiceBean"></bean> 
  9.             
  10.      
  11. </beans> 

[java] view plaincopyprint?
  1. import cn.itm.service.PersonService; 
  2.  
  3. public class PersonServiceBeanimplements PersonService{ 
  4.  
  5.     public PersonServiceBean(){ 
  6.         System.out.println("我被实例化了"); 
  7.     } 
  8.     public void save(){ 
  9.         System.out.println("我是 save() 方法"); 
  10.     } 

测试类:

[java] view plaincopyprint?
  1. public class SpringTest { 
  2.  
  3.     @BeforeClass 
  4.     public staticvoid setUpBeforeClass() throws Exception { 
  5.     } 
  6.  
  7.     // 专门用来实例化  Spring 容器的。 
  8.     @Test publicvoid instanceSpring(){ 
  9.         // 在类路径下,寻找配置文件来实例化容器。 
  10.         ApplicationContext ctx = new ClassPathXmlApplicationContext("beans.xml"); 
  11.          
  12. //      PersonService personService1 = (PersonService) ctx.getBean("personService3"); 
  13.          
  14.     } 

打印结果:我被实例化了。


在每个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"/>


[html] view plaincopyprint?
  1. <?xmlversion="1.0"encoding="UTF-8"?> 
  2. <beans xmlns="http://www.springframework.org/schema/beans" 
  3.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
  4.        xsi:schemaLocation="http://www.springframework.org/schema/beans 
  5.            http://www.springframework.org/schema/beans/spring-beans-2.5.xsd"> 
  6.             
  7.            <beanid="personService"class="cn.itm.service.impl.PersonServiceBean"  
  8.            lazy-init="false"destroy-method="destory"></bean> 
  9.             
  10.      
  11. </beans> 

[java] view plaincopyprint?
  1. package cn.itm.service.impl; 
  2.  
  3. import cn.itm.service.PersonService; 
  4.  
  5. public class PersonServiceBeanimplements PersonService{ 
  6.  
  7.     public PersonServiceBean(){ 
  8.         System.out.println("我被实例化了"); 
  9.     } 
  10.     public void save(){ 
  11.         System.out.println("我是 save() 方法"); 
  12.     } 
  13.      
  14.     public void destory(){ 
  15.         System.out.println("关闭"); 
  16.     } 

AbstractApplicationContext

[java] view plaincopyprint?
  1. public class SpringTest { 
  2.  
  3.     @BeforeClass 
  4.     public staticvoid setUpBeforeClass() throws Exception { 
  5.     } 
  6.  
  7.     // 专门用来实例化  Spring 容器的。 
  8.     @Test publicvoid instanceSpring(){ 
  9.          
  10.         AbstractApplicationContext ctx = new ClassPathXmlApplicationContext("beans.xml"); 
  11.         ctx.close(); 
  12.     } 


原创粉丝点击