3.装配Bean 基于XML
Posted weihu
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了3.装配Bean 基于XML相关的知识,希望对你有一定的参考价值。
一、实例化方式
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 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"> <!--将静态工厂创建的实例交予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的主要内容,如果未能解决你的问题,请参考以下文章