博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Spring 注入不同作用域对象
阅读量:4049 次
发布时间:2019-05-25

本文共 2804 字,大约阅读时间需要 9 分钟。

– Start


当你想把短生命周期的对象(如: prototype)注入到长生命周期的对象(如:singleton)时需要特别注意,Spring 提供了如下的解决办法。

使用代理 aop:scoped-proxy/

package shangbo.spring.example29;public interface MessageService {	String getMessage();}
package shangbo.spring.example29;public class MessageServiceImpl implements MessageService {	private int times = 0;	public String getMessage() {		times++;		return "Hello World-" + times;	}}
package shangbo.spring.example29;public class MessagePrinter {    final private MessageService service;    public MessagePrinter(MessageService service) {        this.service = service;    }    public void printMessage() {        System.out.println(this.service.getMessage());    }}
package shangbo.spring.example29;import org.springframework.context.ApplicationContext;import org.springframework.context.support.ClassPathXmlApplicationContext;public class App {	public static void main(String[] args) {		// 实例化 Spring IoC 容器		ApplicationContext context = new ClassPathXmlApplicationContext("example.xml", MessagePrinter.class);				// 从容器中获得 MessagePrinter 的实例		MessagePrinter printer1 = context.getBean(MessagePrinter.class);		printer1.printMessage();				// 再次在容器中获得 MessagePrinter 的实例		MessagePrinter printer2 = context.getBean(MessagePrinter.class);		printer2.printMessage();	}}

使用 lookup 方法

package shangbo.spring.example30;public interface MessageService {	String getMessage();}
package shangbo.spring.example30;public class MessageServiceImpl implements MessageService {	private int times = 0;	public String getMessage() {		times++;		return "Hello World-" + times;	}}
package shangbo.spring.example30;public abstract class MessagePrinter {    public void printMessage() {    	MessageService service = createMessageService();        System.out.println(service.getMessage());    }        // 注意,这是传说中的 lookup 方法    protected abstract MessageService createMessageService();}
package shangbo.spring.example30;import org.springframework.context.ApplicationContext;import org.springframework.context.support.ClassPathXmlApplicationContext;import shangbo.spring.example29.MessagePrinter;public class App {	public static void main(String[] args) {		// 实例化 Spring IoC 容器		ApplicationContext context = new ClassPathXmlApplicationContext("example.xml", MessagePrinter.class);				// 从容器中获得 MessagePrinter 的实例		MessagePrinter printer1 = context.getBean(MessagePrinter.class);		printer1.printMessage();				// 再次在容器中获得 MessagePrinter 的实例		MessagePrinter printer2 = context.getBean(MessagePrinter.class);		printer2.printMessage();	}}

Spring 还提供了其他方法,如注入 ,或实现 ApplicationContextAware 接口,但是它们需要继承 Spring 的接口,与 Spring 耦合在一起,不推荐使用。

– 声 明:转载请注明出处
– Last Updated on 2017-05-23
– Written by ShangBo on 2017-05-23
– End

你可能感兴趣的文章
北川邓家“刘汉小学”无一死亡奇迹背后的真相
查看>>
救灾,从来没有胜利
查看>>
.net 2.0中ConfigurationManager替代了原来的ConfigurationSettings
查看>>
Asp.net 2.0中使用Datawindow.net2.0
查看>>
常用命名法:骆驼命名法,匈牙利命名法和帕斯卡命名法
查看>>
Server.MapPath方法测试结果
查看>>
Asp.net 默认配置下,Session莫名丢失的原因及解决办法
查看>>
Datawindow.net中如何使用Calendar控件
查看>>
如何在Datawindow.net中实现让当前行选中,并且当前行以其他颜色显示
查看>>
Datawindow.net如何使用导航栏
查看>>
如何利用Datawindow.net提取Sequence数据
查看>>
小诗,纪念我即将到来的结婚两周年
查看>>
自勉文[出处不详,待考证]
查看>>
中国行政级别
查看>>
国家公务员的级别
查看>>
悼念地震死难者:使整个网页变黑白色(灰色)的特效代码
查看>>
asp.net优化完全技巧
查看>>
道 经
查看>>
德 经
查看>>
藏太甲于桐宫-从电视剧康熙王朝中学到的历史知识
查看>>