当一个Bean依赖的Bean和自己生命周期不同的时候:如Bean A依赖Bean B,Bean A 是singleton,如果需要在Bean A每次用到Bean B的时候都用一个Bean B的新的实例,通过在配置文件中通过 property或者 contructor-arg是不能实现的.这时候只能在Bean A中用Bean B的时候动态得到.通常的做法有两种:
1,Bean A实现 ApplicationContextAware, Spring初始化的时候会将 ApplicationContext 传给Bean A,Bean A通过getBean("BeanB")方法每次得到Bean B.("BeanB"最好不要hardcode,通过property传入)
例:
public class ContextAwareBean implements ApplicationContextAware {
protected static final Log log = LogFactory.getLog(AnotherBean.class);
private String anotherBeanName;
private ApplicationContext applicationContext;
public String getAnotherBeanName() {
return anotherBeanName;
}
public void setAnotherBeanName(String anotherBeanName) {
this.anotherBeanName = anotherBeanName;
}
public void process() {
log.info("process applicationContext " + applicationContext);
AnotherBean anotherBean = createAnotheBean();
anotherBean.doSth();
}
protected AnotherBean createAnotheBean() {
return this.applicationContext.getBean(anotherBeanName, AnotherBean.class);
}
public void setApplicationContext(ApplicationContext applicationContext){
log.info("setApplicationContext " + applicationContext);
this.applicationContext = applicationContext;
}
}
public class AnotherBean {
protected static final Log log = LogFactory.getLog(AnotherBean.class);
public String doSth(){
log.info("AnotherBean.doSth");
return "do something";
}
}
<bean id="AnotherBean" class="com.test.spring.di.mtddi.AnotherBean" scope="prototype"/>
<bean id="ContextAwareBean" class="com.test.spring.di.mtddi.ContextAwareBean" >
<property name="anotherBeanName" value="AnotherBean"/>
</bean>
2,方法注入:在Bean A中定义一个方法,返回类型是Bean B,在配置文件中通过"lookup-method"告诉Spring动态覆盖该方法,并返回Bean B的一个实例:
public abstract class ReplacedBean {
protected static final Log log = LogFactory.getLog(ReplacedBean.class);
public void process() {
AnotherBean anotherBean = createAnotheBean();
anotherBean.doSth();
}
protected abstract AnotherBean createAnotheBean();
}
<bean id="AnotherBean" class="com.test.spring.di.mtddi.AnotherBean"
scope="prototype"/>
<bean id="ReplacedBean" class="com.test.spring.di.mtddi.ReplacedBean" >
<lookup-method name="createAnotheBean" bean="AnotherBean"/>
</bean>
客户端代码:
public class MtddiClient {
private static BeanFactory factory;
private static ApplicationContext ctx;
static {
Resource resource = new ClassPathResource("conf/mtddiAppcontext.xml");
factory = new XmlBeanFactory(resource);
ctx = new ClassPathXmlApplicationContext("conf/mtddiAppcontext.xml");
}
/**
* @param args
*/
public static void main(String[] args) {
/*不能通过bean factory的方式得到bean
ContextAwareBean bean = (ContextAwareBean) factory.getBean("ContextAwareBean");
bean.process();
*/
//ContextAwareBean bean = (ContextAwareBean) ctx.getBean("ContextAwareBean");
//bean.process();
ReplacedBean bean1 = (ReplacedBean) factory.getBean("ReplacedBean");
bean1.process();
}
}
*对于实现ApplicationContextAware的Bean,必须用 ApplicationContext的getBean方法.对于方法注入:用BeanFactory和ApplicationContext的getBean都可以.
如果要用BeanFactory,应该实现BeanFactoryAware:
public class BeanFactoryAwareBean implements BeanFactoryAware {
protected static final Log log = LogFactory.getLog(BeanFactoryAwareBean.class);
private String anotherBeanName;
private BeanFactory beanFactory;
public String getAnotherBeanName() {
return anotherBeanName;
}
public void setAnotherBeanName(String anotherBeanName) {
this.anotherBeanName = anotherBeanName;
}
public void process() {
log.info("process beanFactory " + beanFactory);
AnotherBean anotherBean = createAnotheBean();
anotherBean.doSth();
}
protected AnotherBean createAnotheBean() {
return this.beanFactory.getBean(anotherBeanName, AnotherBean.class);
}
@Override
public void setBeanFactory(BeanFactory beanFactory) throws BeansException {
this.beanFactory = beanFactory;
}
}
两种方法的比较:理论上来讲,第二种方法更体现了IoC的思想,而且在bean类里面没有依赖到Spring,只是一个POJO.客户端在使用它的时候可以是依靠Spring配置(lookup-method)来使用,也可以通过提供实现类来完成调用.
分享到:
相关推荐
JSP开发之Spring方法注入之替换方法实现 Spring提供了一种替换方法实现的机制,可以让我们改变某个bean某方法的实现。打个比方我们有一个bean,其中拥有一个add()方法可以用来计算两个整数的和,但这个时候我们想把...
spring注入方式 set注入 构造注入 自动注入
我们学习了spring框架spring框架里面有3个优势第一个是轻量级的IOC也叫控制反转后来改名为DI也叫依赖注入,依赖注入里面有3中注入方法分别是set注入,构造器注入,注解注入,我传的是set注入的视频
NULL 博文链接:https://zhangyulong.iteye.com/blog/856986
Spring依赖注入使用构造设注入demo。完整可运行。
Spring中的方法注入方式.Spring中的方法注入方式.
Spring注解注入属性
主要给大家介绍了关于spring boot中几种注入方法的一些个人看法,文中通过示例代码介绍的非常详细,对大家学习或者使用spring boot具有一定的参考学习价值,需要的朋友们下面来一起学习学习吧
花了些时间做了一个实验,彻底弄懂了spring Annotation注入的方式。凡带有@Component,@Controller,@Service,@Repository 标志的等于告诉Spring这类将自动产生对象,而@Resource则等于XML配置中的ref,告诉spring此处...
spring依赖注入底层详解,很不错的资源,欢迎大家来下载学习。
Spring依赖注入使用实例工厂设注入demo。完整可运行。
Spring依赖注入检查,适合初学者进一步了解Spring框架。
javaEE 开发中 现在最成熟的框架之一应该就是spring了 spring框架最强大的地方就是实现了依赖注入 也叫控制反转 最近的一个项目中用的就是 spring框架 spring框架是工厂模式的应用结合了MVC的设计思想 大家可以...
详细阐明spring依赖注入工作原理,基于注解的一个完整例子,当然数据库需要自己去创建
Spring IOC之方法注入 ,具体效果和过程看博文 http://blog.csdn.net/evankaka/article/details/45022649
java 无需数据库 简单实例 如何实现Spring依赖注入 AOP
SPRING DI注入例子 jar包没有包含
模拟Spring IOC 自己实现依赖注入 自己的Spring 自己的代码模拟spring的依赖注入 自己写的 绝对独一无二
spring 依赖注入三种方式测试源码,测试细节参考博文http://blog.csdn.net/u010679383/article/details/71305400