spring bean装配 注册动态工厂和静态工厂
2017-05-15 14:28
453 查看
默认的装配方式:代码通过getbean()方式从容器获取指定的bean实例容器首先会调用bean的无参构造器。创建控制的实例对象注册动态的工厂applicationcontext.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="factory" class="com.bjpowernode.ba02.ServiceFactory"/> <!-- 注册Service:动态工厂Bean --> <bean id="myService" factory-bean="factory" factory-method="getSomeService"/> </beans>实现接口和实体类factory类
public class ServiceFactory { public ISomeService getSomeService() { return new SomeServiceImpl(); }
public class SomeServiceImpl implements ISomeService { private int a; public SomeServiceImpl() { System.out.println("执行无参构造器"); } /* public SomeServiceImpl(int a) { this.a = a; } */ @Override public void doSome() { System.out.println("执行doSome()方法"); } }接口
public interface ISomeService { void doSome(); }测试类
public class MyTest { @Test public void test01() { ISomeService service = new ServiceFactory().getSomeService(); service.doSome(); } @Test public void test02() { // 创建容器对象,加载Spring配置文件 String resource = "com/bjpowernode/ba02/applicationContext.xml"; ApplicationContext ac = new ClassPathXmlApplicationContext(resource); ISomeService service = (ISomeService) ac.getBean("myService"); service.doSome(); } }静态工厂:applicationcontext.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"><!-- 注册Service:静态工厂Bean --><bean id="myService" class="com.bjpowernode.ba03.ServiceFactory" factory-method="getSomeService"/></beans>接口:
public interface ISomeService { void doSome(); }实现类:public class SomeServiceImpl implements ISomeService { private int a; public SomeServiceImpl() { System.out.println("执行无参构造器"); } /* public SomeServiceImpl(int a) { this.a = a; } */ @Override public void doSome() { System.out.println("执行doSome()方法"); } }工厂类public class ServiceFactory {public static ISomeService getSomeService() {return new SomeServiceImpl();}}测试类public class MyTest {@Testpublic void test01() {// 创建容器对象,加载Spring配置文件String resource = "com/bjpowernode/ba03/applicationContext.xml";ApplicationContext ac = new ClassPathXmlApplicationContext(resource);ISomeService service = (ISomeService) ac.getBean("myService");service.doSome();}}
相关文章推荐
- Spring--IoC--Bean的装配--静态工厂Bean
- Spring-装配Bean的3种方式-工厂静态方法方式配置Bean
- 转:Spring使用静态工厂方法创建Bean
- Spring 学习总结 使用静态工厂创建Bean
- spring 动态注册bean
- spring 9 bean配置--通过工厂方法(静态工厂方法&实例工厂方法
- Spring动态注册bean实现动态多数据源
- Spring核心Bean工厂装配__Spring学习笔记
- spring动态bean注册示例分享
- spring中注册bean(通过代码动态注册)
- spring笔记9---静态工厂方法配置bean
- Spring运行时动态注册bean的方法
- Spring使用静态工厂方法创建Bean
- 【Spring】bean动态注册到spring
- spring学习小结7_使用工厂方法来配置bean:静态工厂方法和实例工厂方法
- Junit测试一个spring静态工厂实例化bean的例子,出现java.lang.IllegalArgumentException异常。
- [原创]java WEB学习笔记102:Spring学习---Spring Bean配置:bean配置方式(工厂方法(静态工厂方法 & 实例工厂方法)、FactoryBean) 全类名
- Spring--IoC--Bean的装配--工厂方法设计模式
- 学习Spring(二) 调用静态工厂方法创建Bean
- Spring入门学习——调用静态工厂方法创建Bean