(3)Spring的三種實例 bean 的方式 以及 Spring中bean的作用域。 以及 bean的生命週期

第一種:

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();
	}
}



發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章