3.裝配Bean 基於XML
阿新 • • 發佈:2018-08-13
Coding org xsd pac com enc mlp 方法名 定義
一、實例化方式
3種bean實例化方式:默認構造、靜態工廠、實例工廠
1.默認構造
<bean id="" class=""> 必須提供默認構造
2. 靜態工廠
l 常用與spring整合其他框架(工具)
l 靜態工廠:用於生成實例對象,所有的方法必須是static
<bean id="" class="工廠全限定類名" factory-method="靜態方法">
UserService.java
package com.jd.inject.static_factory; public interface UserService {public void addUser(); }
UserServiceImpl.java
package com.jd.inject.static_factory.impl; import com.jd.inject.static_factory.UserService; public class UserServiceImpl implements UserService { @Override public void addUser() { System.out.println("b_static_factory add user"); } }
MyBeanFactory.java
package com.jd.inject.static_factory; import com.jd.inject.static_factory.impl.UserServiceImpl; /** * @author weihu * @date 2018/8/12/012 18:54 */ public class MyBeanFactory { /** * 創建實例 * @return */ public static UserService createService(){ return newUserServiceImpl(); } }
beans.xml
<?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"> <!--將靜態工廠創建的實例交予spring class 確定靜態工廠全限定類名 factory-method 確定靜態方法名,還是創建UserServiceImpl對象 --> <bean id="userServiceId" class="com.jd.inject.static_factory.MyBeanFactory" factory-method="createService"></bean> </beans>
測試類
TestStaticFactory.java
package com.jd.test; import com.jd.inject.static_factory.MyBeanFactory; import com.jd.inject.static_factory.UserService; import org.junit.Test; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; /** * @author weihu * @date 2018/8/12/012 18:57 */ public class TestStaticFactory { @Test public void testStaticFactory(){ //自定義工廠 UserService userService = MyBeanFactory.createService(); userService.addUser(); } @Test public void testSpringStaticFactory(){ //spring工廠 String xmlPath="com/jd/inject/static_factory/beans.xml"; ApplicationContext applicationContext = new ClassPathXmlApplicationContext(xmlPath); UserService userServiceId = applicationContext.getBean("userServiceId", UserService.class); userServiceId.addUser(); } }
3. 實例工廠
實例工廠:必須先有工廠實例對象,通過實例對象創建對象。提供所有的方法都是“非靜態”的。
UserService.java
package com.jd.inject.factory; public interface UserService { public void addUser(); }
UserServiceImpl.java
package com.jd.inject.factory.impl; import com.jd.inject.factory.UserService; public class UserServiceImpl implements UserService { @Override public void addUser() { System.out.println("factory add user"); } }
MyBeanFactory.java
package com.jd.inject.factory; import com.jd.inject.factory.impl.UserServiceImpl; /** * @author weihu * @date 2018/8/12/012 18:54 */ public class MyBeanFactory { /** * 創建實例 * @return */ public UserService createService(){ return new UserServiceImpl(); } }
beans.xml
<?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.jd.inject.factory.MyBeanFactory"></bean> <!--獲得userservice factory-bean確定工廠實例 factory-method 確定普通方法 --> <bean id="userServiceId" factory-bean="myBeanFactoryId" factory-method="createService"></bean> </beans>
測試類
TestFactory.java
package com.jd.test; import com.jd.inject.factory.MyBeanFactory; import com.jd.inject.factory.UserService; import org.junit.Test; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; /** * @author weihu * @date 2018/8/12/012 19:16 */ public class TestFactory { @Test public void testFactory(){ MyBeanFactory myBeanFactory=new MyBeanFactory(); UserService userService = myBeanFactory.createService(); userService.addUser(); } @Test public void testSpringFactory(){ String xmlPath="com/jd/inject/factory/beans.xml"; ApplicationContext applicationContext = new ClassPathXmlApplicationContext(xmlPath); UserService userServiceId = applicationContext.getBean("userServiceId", UserService.class); userServiceId.addUser(); } }
3.裝配Bean 基於XML