Spring的方法注入
来源:互联网 发布:韶关网络问政平台 编辑:程序博客网 时间:2024/04/30 10:40
当一个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 只能从ApplicationContext获得bean //ContextAwareBean bean = (ContextAwareBean) ctx.getBean("ContextAwareBean"); //bean.process(); ReplacedBean bean1 = (ReplacedBean) factory.getBean("ReplacedBean"); bean1.process(); }}*对于实现ApplicationContextAware的Bean,必须用 ApplicationContext的getBean方法.对于方法注入(lookup-method方式):用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)来使用,也可以通过提供实现类来完成调用.
- Spring的方法注入
- Spring的方法注入
- Spring的注入方法小结
- spring 的 lookup方法注入
- Spring依赖注入的方法
- 【Spring】--Spring方法注入
- 【Spring】--Spring方法注入
- Spring基于xml的构造方法注入&setter注入
- 【Spring笔记】(二)集合注入、注入的方法
- Spring基于xml的构造方法注入&setter注入
- Spring Bean 注入 Servlet 的方法
- Spring方法注入非单例bean的调用
- Spring Bean 注入 Servlet 的方法
- Spring的方法注入lookup-method
- Spring中继承配置的注入方法
- Spring注入static属性的方法
- Spring中三种依赖注入的方法
- Spring参数注入的几种方法
- TBODY分行下载
- delphi编码规范文档
- 发布网站(1)之 允许更新此预编译站点
- WebSphere MQ6.0 for redhat4.6 setup
- MySQL中的mysqldump命令使用详解
- Spring的方法注入
- 如何建立svn版本库并运行它
- 页面滚动条 body的滚动条
- MySQL导入导出命令
- c++复习注意要点
- MySQL 服务无法启动 1067 错误 进程意外终止解决方法
- 使用java将word文档转换为swf格式
- 在WM5以上系统使用Notification Broker机制实现获取全部电话状态
- csdn windows live writer 发布日志