spring prototype怎么注入到singleton 里面
来源:互联网 发布:渲染软件哪个好 编辑:程序博客网 时间:2024/05/19 13:56
经过测试之后用注解的方式 不行 所以只能采用容器取的方式。例子代码如下
package com.dragon.service.test;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.context.annotation.Scope;import org.springframework.stereotype.Component;@Component("testBean")@Scope("singleton")public class TestBean {<span style="white-space:pre"></span>//@Autowired<span style="white-space:pre"></span>//private TestBean1 testBean1;public TestBean1 getTestBean1() {return (TestBean1) SpringBeanUtils.getBean("testBean1");}}
package com.dragon.service.test;import org.springframework.context.annotation.Scope;import org.springframework.stereotype.Component;@Component("testBean1")@Scope("prototype")public class TestBean1 {private Integer num = 1;public Integer getNum() {num++;return num;}}
package com.dragon.service.test;import java.util.Map;import org.slf4j.*;import org.springframework.beans.BeansException;import org.springframework.context.ApplicationContext;import org.springframework.context.ApplicationContextAware;import org.springframework.stereotype.Service;@Servicepublic class SpringBeanUtils implements ApplicationContextAware {private static ApplicationContext appContext;private Logger logger = LoggerFactory.getLogger(getClass());public void setApplicationContext(ApplicationContext applicationContext)throws BeansException {SpringBeanUtils.appContext = applicationContext;logger.debug("初始化Spring上下文成功。");}/** * 获取 ApplicationContext */public static ApplicationContext getApplicationContext() {return appContext;}/** * 获取 TestBean * * @param beanName * bean 名称 * @return */public static Object getBean(String beanName) {checkApplicationContext();return appContext.getBean(beanName);}/** * 获取 TestBean * * @param clazz * bean 类 * @return */@SuppressWarnings("unchecked")public static <T> T getBean(Class<T> clazz) {checkApplicationContext();Map<?, ?> map = appContext.getBeansOfType(clazz);return map.isEmpty() ? null : (T) map.values().iterator().next();}/** * 检查 ApplicationContext 是否注入 */private static void checkApplicationContext() {if (null == appContext) {throw new IllegalStateException("applicaitonContext未注入");}}}
测试类如下
package com.dragon.test;import org.junit.Test;import org.springframework.beans.factory.annotation.Autowired;import com.dragon.service.test.TestBean;public class TestBeanTest extends BaseTest {@Autowiredprivate TestBean testBean1;@Autowiredprivate TestBean testBean2;@Testpublic void testAdd() {System.out.println(testBean1.getTestBean1().getNum());System.out.println(testBean2.getTestBean1().getNum());}}
这样子输出的结果是 2,2 也就说明了 每次取到的testbean1对象都是新的对象
如果testbean里面采用注入的方式 打印的结果是2,3
1 0
- spring prototype怎么注入到singleton 里面
- 解决Spring中singleton中注入prototype Bean的问题
- Spring Singleton VS prototype
- spring中singleton和prototype
- spring的singleton和prototype的区别
- spring scope prototype与singleton区别
- 【Spring】scope:"singleton"与"prototype"学习记录
- Spring中scope singleton 和 prototype详解
- spring scope prototype与singleton区别
- spring scope prototype与singleton区别
- spring singleton与prototype的引用关系
- spring scope prototype与singleton区别
- Spring prototype bean 怎么了
- spring boot 两种方式注入到静态工具类里面
- Spring中Bean的作用域singleton, prototype
- spring 中scope的singleton和prototype区别
- spring scope="prototype" 和scope="singleton"区分
- Spring 受管理 Bean scope 的 prototype 和 singleton 区别
- ceph 添加/移除osd 设备
- listpopupwindow和popupwindow
- DevExpress 15.1.8重大变化说明及下载
- mysql统计一年中每周的数据
- Add Two Numbers
- spring prototype怎么注入到singleton 里面
- 谈谈 React Native
- Linux下杀死进程(kill)的N种方法
- 利用反射机制创建新类的两种方式及比较
- Linux Epool 使用学习笔记
- NSLog打印优化
- undefined与null的区别
- RocketMQ与Kafka对比(18项差异)
- Office 365 Starter Project for ASP.NET MVC