首页 > 代码库 > Spring单例Bean中注入多例Bean
Spring单例Bean中注入多例Bean
问题:
当在一个单例Bean中注入一个多例Bean的时候,是获取不到那个多例对象的,因为,单例在初始化的时候,就直接初始化,这个多例Bean啦, 一直获取的是第一次初始化的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" xmlns:aop="http://www.springframework.org/schema/aop" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd"> <bean id="single" class="com.lzd.spring.context.resource.test.bean.SingletonObject"> <property name="prototypeObject" ref="prototype"></property> </bean> <bean id="prototype" class="com.lzd.spring.context.resource.test.bean.PrototypeObject" scope="prototype"> <property name="name" value="http://www.mamicode.com/刘东"></property> <property name="password" value="http://www.mamicode.com/123"></property> <!-- <aop:scoped-proxy/> --> </bean> </beans> |
SingletonObject对象:
package com.lzd.spring.context.resource.test.bean; public class SingletonObject { private PrototypeObject prototype; public void setPrototypeObject(PrototypeObject prototype){ this.prototype = prototype; } public PrototypeObject getPrototype(){ return this.prototype; } } |
PrototypeObject对象:
package com.lzd.spring.context.resource.test.bean; /** * * <P>Description: 这是一个多例的对象</P> * @ClassName: PrototypeObject * @author lzd 2017年2月15日 上午10:54:52 */ public class PrototypeObject { private String name; private String password; public String getName() { return name; } public void setName(String name) { this.name = name; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } } |
ApplicationResourceTest测试类:
package com.lzd.spring.context.resource.test; import org.junit.Test; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import com.lzd.spring.context.resource.test.bean.SingletonObject; public class ApplicationResourceTest { @Test public void applicationResource(){ ApplicationContext context = new ClassPathXmlApplicationContext(new String[]{"classpath:application.xml"}); SingletonObject s = (SingletonObject) context.getBean("single"); for (int i = 0; i < 10; i++) System.out.println(s.getPrototype()); } } |
问题解决:
配置文件:
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd"> <bean id="single" class="com.lzd.spring.context.resource.test.bean.SingletonObject"> <property name="prototypeObject" ref="prototype"></property> </bean> <bean id="prototype" class="com.lzd.spring.context.resource.test.bean.PrototypeObject" scope="prototype"> <property name="name" value="http://www.mamicode.com/刘东"></property> <property name="password" value="http://www.mamicode.com/123"></property> <!-- 添加下面,一行,每次创建的,都是一个代理对象,然后注入获取的都是新的对象 --> </bean> </beans> |
测试结果:
Spring单例Bean中注入多例Bean
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。