spring prototype怎么注入到singleton 里面

经过测试之后用注解的方式 不行 所以只能采用容器取的方式。例子代码如下


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;

@Service
public 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 {
	@Autowired
	private TestBean testBean1;
	@Autowired
	private TestBean testBean2;

	@Test
	public void testAdd() {
		System.out.println(testBean1.getTestBean1().getNum());
		System.out.println(testBean2.getTestBean1().getNum());
	}
}


这样子输出的结果是 2,2  也就说明了 每次取到的testbean1对象都是新的对象

如果testbean里面采用注入的方式 打印的结果是2,3

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

white......

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值