Spring中Bean的基於xml的三種實例化方式
1、JavaBean 是一種JAVA語言寫成的可重用組件。JavaBean 通過提供符合一致性設計模式的公共方法將內部域暴露成員屬性,其他Java 類可以通過自身機制發現和操作這些JavaBean 屬性。
2、Bean的種類
a)普通bean:由Spring直接創建實例。<bean id="" class="A">
b)FactoryBean:是一個特殊的bean,具有工廠生成對象能力,只能生成特定的對象。bean必須使用 FactoryBean接口,此接口提供方法 getObject() 用於獲得特定bean。<bean id=""class="FB">
c)BeanFactory:是一個工廠,用於生成任意Bean。
3、普通Bean基於xml的三種實例化方法
a) 默認構造 <beanid="" class="">
b) 靜態工廠:常用於與Spring整合其他框架,用於生成實例對象,所有的方法必須是static。
<bean id="" class="工廠全限定類名" factory-method="靜態方法">
c) 實例工廠,必須先有工廠實例對象,通過實例對象創建對象,所有的方法都是非靜態的。
下面編寫靜態工廠方法和實例工廠方法的實例、配置和測試類。
1、靜態工廠方法
編寫UserService接口、編寫UserServiceImpl實現類,編寫MyBeanFactory靜態創建實例類,配置bean.xml,編寫TestStaticFactory測試類。【不要忘了導包!】
/**
*
*/
package com.Lily.SpringLeanring.a_staticFactory;
/**
* *
* @author LilyLee
* @date 2017年6月14日
* @time 上午9:10:25
* @Version 1.0
* @email [email protected]
*
*/
public interface UserService {
public void addUser();
}
/**
*
*/
package com.Lily.SpringLeanring.a_staticFactory;
/**
* *
* @author LilyLee
* @date 2017年6月14日
* @time 上午9:11:13
* @Version 1.0
* @email [email protected]
*
*/
public class UserServiceImpl implements UserService {
/* (non-Javadoc)
* @see com.Lily.SpringLearning.a_staticFacory.UserService#addUser()
*/
@Override
public void addUser() {
// TODO Auto-generated method stub
System.out.println("Bean 靜態工廠方法 addUser()");
}
}
/**
*
*/
package com.Lily.SpringLeanring.a_staticFactory;
/**
* *
* @author LilyLee
* @date 2017年6月14日
* @time 上午9:12:09
* @Version 1.0
* @email [email protected]
*
*/
public class MyBeanFactory {
public static UserService createService(){
return new UserServiceImpl();
}
}
<?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.xsd">
<bean id="userServiceId" class="com.Lily.SpringLeanring.a_staticFactory.MyBeanFactory" factory-method="createService"></bean>
</beans>
/**
*
*/
package com.Lily.SpringLeanring.a_staticFactory;
import static org.junit.Assert.*;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
/**
* *
* @author LilyLee
* @date 2017年6月14日
* @time 上午9:22:16
* @Version 1.0
* @email [email protected]
*
*/
public class TestStaticFactory2 {
@Test
public void test() {
String xmlPath="com/Lily/SpringLeanring/a_staticFactory/beans.xml";
ApplicationContext ac=new ClassPathXmlApplicationContext(xmlPath);
UserService u=ac.getBean("userServiceId", UserService.class);
u.addUser();
}
}
2、實例工廠方法
編寫UserService接口、編寫UserServiceImpl實現類,編寫MyBeanFactory靜態創建實例類,配置bean.xml,編寫TestStaticFactory測試類。
與上面不同之處在於:
(1)MyBeanFactory的創建方法是非靜態的。
(2)xml首先要創建工廠實例對象,然後才能通過實例對象去創建對象。
/**
*
*/
package com.Lily.SpringLeanring.a_staticFactory;
/**
* *
* @author LilyLee
* @date 2017年6月14日
* @time 上午9:12:09
* @Version 1.0
* @email [email protected]
*
*/
public class MyBeanFactory {
public UserService createService(){
return new UserServiceImpl();
}
}
<?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.xsd">
<bean id="myBeanFactoryId" class="com.Lily.SpringLearning.b_Factory.MyBeanFactory"></bean>
<bean id="userServiceId" factory-bean="myBeanFactoryId" factory-method="createService"></bean>
</beans>