博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Spring 自动依赖注入优化(qualifier)
阅读量:4049 次
发布时间:2019-05-25

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

– Start


自动依赖注入大大简化了我们的工作量,但是也有缺陷,如果一个接口有多个实现类,我们该注入哪一个呢?一种方法是给bean设置一个限定符(qualifier)

package shangbo.spring.example36;public interface MessageService {	String getMessage();}
package shangbo.spring.example36;public class MessageServiceDBImpl implements MessageService {	public String getMessage() {		return "This message from database";	}}
package shangbo.spring.example36;public class MessageServiceFileImpl implements MessageService {	public String getMessage() {		return "This message from file";	}}
package shangbo.spring.example36;import org.springframework.beans.factory.annotation.Qualifier;public class MessagePrinter {    private MessageService service;    // 使用限定符    @Qualifier("DB")    public void setService(MessageService service) {		this.service = service;	}	public void printMessage() {        System.out.println(service.getMessage());    }}
package shangbo.spring.example36;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 printer = context.getBean(MessagePrinter.class);				// 使用对象		printer.printMessage();	}}

下面是使用 Java 配置的例子。

package shangbo.spring.example37;public interface MessageService {	String getMessage();}
package shangbo.spring.example37;public class MessageServiceDBImpl implements MessageService {	public String getMessage() {		return "This message from database";	}}
package shangbo.spring.example37;public class MessageServiceFileImpl implements MessageService {	public String getMessage() {		return "This message from file";	}}
package shangbo.spring.example37;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.beans.factory.annotation.Qualifier;public class MessagePrinter {    private MessageService service;    // 限定符    @Autowired    @Qualifier("DB")    public void setService(MessageService service) {		this.service = service;	}	public void printMessage() {        System.out.println(service.getMessage());    }}
package shangbo.spring.example37;import org.springframework.beans.factory.annotation.Qualifier;import org.springframework.context.annotation.Bean;import org.springframework.context.annotation.Configuration;@Configurationpublic class AppConfig {	@Bean	@Qualifier("DB")	public MessageService dbMessageService() {		return new MessageServiceDBImpl();	}		@Bean	@Qualifier("FILE")	public MessageService fileMessageService() {		return new MessageServiceDBImpl();	}		@Bean	public MessagePrinter messagePrinter() {		return new MessagePrinter();	}}
package shangbo.spring.example37;import org.springframework.context.ApplicationContext;import org.springframework.context.annotation.AnnotationConfigApplicationContext;public class App {    public static void main( String[] args )    {		// 实例化 Spring IoC 容器		ApplicationContext context = new AnnotationConfigApplicationContext(AppConfig.class);				// 从容器中获得 MessagePrinter 的实例		MessagePrinter printer = context.getBean(MessagePrinter.class);				// 使用对象		printer.printMessage();    }}

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

你可能感兴趣的文章
模拟屏学习资料_模拟视频 入门
查看>>
西藏之旅
查看>>
Oracle中定时执行问题
查看>>
三时业
查看>>
佛教三宝-三皈依
查看>>
杂阿含经喻世间有四等马
查看>>
考研前夜涂笔
查看>>
英语复试自我介绍
查看>>
什么是熵?
查看>>
拼凑、摘抄-评李代平的软件工程第二版
查看>>
误传了数千年的几个名句
查看>>
韩复榘经典语录
查看>>
厅、部、局、司区分大小
查看>>
VS2005中使用C#编写MDI窗口根据子窗口个数控制菜单项的enabled属性
查看>>
北川邓家“刘汉小学”无一死亡奇迹背后的真相
查看>>
救灾,从来没有胜利
查看>>
.net 2.0中ConfigurationManager替代了原来的ConfigurationSettings
查看>>
Asp.net 2.0中使用Datawindow.net2.0
查看>>
常用命名法:骆驼命名法,匈牙利命名法和帕斯卡命名法
查看>>
Server.MapPath方法测试结果
查看>>