@Qualifier装配实例

可能会有这样一种情况,当你创建多个具有相同类型的 bean 时,并且想要用一个属性只为它们其中的一个进行装配,在这种情况下,你可以使用 @Qualifier 注解和 @Autowired 注解通过指定哪一个真正的 bean 将会被装配来消除混乱。下面是使用 @Qualifier 注解的一个示例。

一、编写Bean类

CustomerDao接口:

package com.yiidian.dao;
/**
 * 
 * @author http://www.yiidian.com
 *
 */
public interface CustomerDao {
	public void save();
}

CustomerDaoMySQLImpl实现:

package com.yiidian.dao.impl;

import org.springframework.stereotype.Repository;

import com.yiidian.dao.CustomerDao;
/**
 * @author http://www.yiidian.com
 *
 */
@Repository(value="customerDao1")
public class CustomerDaoMySQLImpl implements CustomerDao {

	@Override
	public void save() {
		System.out.println("customerDao的MySQL实现");
	}
	
}

CustomerDaoOracleImpl实现类:

package com.yiidian.dao.impl;

import org.springframework.stereotype.Repository;

import com.yiidian.dao.CustomerDao;
/**
 * @author http://www.yiidian.com
 *
 */
@Repository(value="customerDao2")
public class CustomerDaoOracleImpl implements CustomerDao {

	@Override
	public void save() {
		System.out.println("customerDao的Oracle实现");
	}
	
}

CustomerService接口:

package com.yiidian.service;
/**
 * 
 * @author http://www.yiidian.com
 *
 */
public interface CustomerService {

	public void save();
}

CustomerServiceImpl实现:

package com.yiidian.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.yiidian.dao.CustomerDao;
import com.yiidian.service.CustomerService;
/**
 * 
 * @author http://www.yiidian.com
 *
 */
@Service(value="customerService")
public class CustomerServiceImpl implements CustomerService {
	@Autowired
	private CustomerDao customerDao;

	@Override
	public void save() {
		customerDao.save();
	}
}

二、配置applicationContext.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:p="http://www.springframework.org/schema/p"
	xmlns:context="http://www.springframework.org/schema/context"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="
        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
	
	<!-- 开启SpringIOC注解扫描 -->
	<context:component-scan base-package="com.yiidian"></context:component-scan>
	
</beans>

三、编写测试类

package com.yiidian.test;

import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.yiidian.service.CustomerService;
/**
 * @author http://www.yiidian.com
 *
 */
public class Demo1 {
	
	@Test
	public void test1(){
		ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
		CustomerService customerService = (CustomerService)ac.getBean("customerService");
		customerService.save();
	}
}

四、运行之后,发现错误

运行测试类之后,发现后台报出以下错误:

No qualifying bean of type [com.yiidian.dao.CustomerDao] is defined: expected single matching bean but found 2: customerDao1,customerDao2

原因,CustomerServiceImpl尝试注入CustomerDao类型的对象,但是在IOC容器中存在两个CustomerDao接口的实现类对象,分别为CustomerDaoMySQLImpl和CustomerDaoOracleImpl,导致注入失败!

五、使用@Qualifier注解解决问题 

在CustomerServiceImpl注入CustomerDao同时使用@Autowired和@Qualifier指定注入的对象名称。


@Service(value="customerService")
public class CustomerServiceImpl implements CustomerService {
	@Autowired
	@Qualifier("customerDao1")  //关键是@@Qualifier注解指定了名称
	private CustomerDao customerDao;

	@Override
	public void save() {
		customerDao.save();
	}
}

这时运行测试类,结果正确啦:

源码下载:http://pan.baidu.com/s/1slwThRB

热门文章

优秀文章