springCloud中@Qualifier用处,处理注入不准确的问题

lys2020年03月04日 0条评论

@Qualifier用处

@Qualifier作用为限定描述符,用于细粒度选择候选者,说人话就是注入的时候可能发现有多个可注入对象,比如说一个Service接口有3个实现类,分别为impl1,impl2,impl3,你注入service的时候注入的是接口,那么就可以通过@Qualifier(“你要注入的bean的名称”)来选择注入对象。

例子如下:

package com.springCloud;

import javax.sql.DataSource;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.SpringBootConfiguration;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.config.DbConfigBean;
import com.irisian.sharedjdbc.SessionFactory;
import com.irisian.sharedjdbc.rule.DbConfig;

import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.context.annotation.ComponentScan;

/**
 * Hello world!
 *
 */
@Controller
@EnableAutoConfiguration
@SpringBootApplication
@EnableDiscoveryClient
@EnableConfigurationProperties({DbConfigBean.class})
@ComponentScan(value = {"com.*","com.irisian.sharedjdbc.rule.*"})
public class App 
{	
	@Autowired
	@Qualifier("dataSource1")
	private  DataSource dataSource1;
	@Autowired
	@Qualifier("dataSource2")
	private  DataSource dataSource2;
	@Autowired
	private DbConfigBean dbConfigBean;
	@RequestMapping("/")
	@ResponseBody 
	public String index(){
		System.out.println(dataSource1.equals(dataSource2));
		return "hahahaha "+dbConfigBean.getDbName();
	}
    public static void main( String[] args )
    {
    	 SpringApplication.run(App.class, args);
    }
}


具体配置类如下(有两个都是创建DataSource的,依据配置application-test.yml):

server:
  port: 8084
  
spring:
  datasource1:
    url: jdbc:mysql://localhost:3306/db0?useUnicode=true&characterEncoding=utf-8&useSSL=true&serverTimezone=UTC
    username: ${dbName}
    password: ${dbPassword}
    driver-class-name: com.mysql.jdbc.Driver
    type: com.alibaba.druid.pool.DruidDataSource
  datasource2:
    url: jdbc:mysql://localhost:3306/db1?useUnicode=true&characterEncoding=utf-8&useSSL=true&serverTimezone=UTC
    username: ${dbName}
    password: ${dbPassword}
    driver-class-name: com.mysql.jdbc.Driver  
    type: com.alibaba.druid.pool.DruidDataSource0


package com.config;

import com.alibaba.druid.pool.DruidDataSource;
import com.alibaba.druid.support.http.StatViewServlet;
import com.alibaba.druid.support.http.WebStatFilter;

import org.springframework.boot.autoconfigure.jdbc.DataSourceBuilder;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;

import javax.sql.DataSource;
import java.lang.reflect.Array;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

@Configuration
public class DruidConfig1 {
	@Primary
	@Bean(name = "dataSource1")
    @ConfigurationProperties(prefix = "spring.datasource1")
    public DataSource dataSource1(){
    	return DataSourceBuilder.create().build();
    }
   

}
package com.config;

import com.alibaba.druid.pool.DruidDataSource;
import com.alibaba.druid.support.http.StatViewServlet;
import com.alibaba.druid.support.http.WebStatFilter;

import org.springframework.boot.autoconfigure.jdbc.DataSourceBuilder;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;

import javax.sql.DataSource;
import java.lang.reflect.Array;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

@Configuration
public class DruidConfig2 {

    @Bean(name = "dataSource2")
    @ConfigurationProperties(prefix = "spring.datasource2")
    public DataSource dataSource2(){
    	return DataSourceBuilder.create().build();
    }


}