Third, @Qualifier: limited descriptors, for fine-grained selection of candidates;
@Autowired is injected by type by default, so if you have more than one type of bean candidate, you need to qualify one of the candidates, or you will throw an exception
@Qualifier qualified descriptors can be injected in addition to the name, more granular control over how to select candidates, using the following methods:
@Qualifier (value = "qualified identifier")
field, method, parameter
(1), according to the name of the <qualifier> tag based on the XML configuration to inject, specify the name as follows:
<qualifier type= "Org.springframework.beans.factory.annotation.Qualifier"
Where the type attribute is optional, specifying the type, the default is the qualifier annotation class, and name is specifying a qualified identifier for the bean candidate, and only a <qualifier> of the specified type is allowed in a bean definition; If there are more than one specified after the same type, the preceding is overwritten.
1. Prepare the test bean:
Datasource.java
Package Com.bean;
Public interface DataSource {public
void connection ();
}
Mysqldrivemanagerdatasource.java
Package Com.bean;
public class Mysqldrivemanagerdatasource implements datasource{public
void Connection () {
System.out.println ("MySQL Database connecting ...");}
Oracledrivemanagerdatasource.java
Package Com.bean;
public class Oracledrivemanagerdatasource implements datasource{public
void Connection () {
SYSTEM.OUT.PRINTLN ("Oracle Database Connecting ...");}
Testbean.java
Package Com.bean;
Import org.springframework.beans.factory.annotation.Autowired;
Import Org.springframework.beans.factory.annotation.Qualifier;
public class Testbean {
private DataSource DataSource;
@Autowired public
void InitDataSource (@Qualifier ("Oracledatasource") DataSource DataSource) {
This.datasource = DataSource;
}
Public DataSource Getdatasource () {
return DataSource;
}
}
One way to use @Qualifier in Testbean.java
@Autowired
@Qualifier (value= "Oracledatasource") public
void InitDataSource (DataSource DataSource) {
this.datasource = DataSource;
}
2. Add the following bean configuration in the Spring configuration file:
<bean id= "Testbean" class= "Com.bean.TestBean"/>
We use @qualifier ("Oracledatasource") to specify the qualified identifier of the candidate bean, and we need to use the <qualifier> tag in the configuration file to specify the qualified identifier of the candidate Bean " Oracledatasource ":
<bean id= "Mysqldatasourcebean" class= "Com.bean.MysqlDriveManagerDataSource" >
<qualifier value= " Mysqldatasource "/>
</bean>
<bean id=" Oracledatasourcebean "class=" Com.bean.OracleDriveManagerDataSource ">
<qualifier value=" Oracledatasource "/>
</bean>
3. The test method is as follows:
@Test public
void Autowiredtest () {
Testbean bean = Ctx.getbean ("Testbean", testbean.class);
DataSource DataSource = Bean.getdatasource ();
if (DataSource instanceof mysqldrivemanagerdatasource) {
System.out.println ("MySQL");
} else if (DataSource instanceof oracledrivemanagerdatasource) {
System.out.println ("Oracle");
}
Datasource.connection ();
try{
Ctx.getbean ("Mysqldatasource");
} catch (Exception e) {
if (e instanceof nosuchbeandefinitionexception) {
System.out.println ("@ Qualifier cannot be a bean identifier ");
}
E.printstacktrace ();
}
}
From the test, it can be seen that the qualified identifier specified with the <qualifier> tag can only be used by @qualifier, not as the bean identifier, such as "Ctx.getbean (" Mysqldatasource ")" is not get the bean.
(2), the default is injected according to the bean name : The most basic way is a fault-tolerant mechanism that does not specify the <qualifier> tag on the Bean, which is injected by default with the bean identifier, but fault tolerance will not occur if you specify the <qualifier> tag.
1. Prepare the test bean:
Package Com.bean;
Import org.springframework.beans.factory.annotation.Autowired;
Import Org.springframework.beans.factory.annotation.Qualifier;
public class Testbean {
private DataSource DataSource;
@Autowired
@Qualifier (value= "Mysqldatasourcebean") public
void InitDataSource (DataSource DataSource) {
this.datasource = DataSource;
}
Public DataSource Getdatasource () {
return DataSource;
}
}
2. Add the following bean configuration in the Spring configuration file:
<bean id= "Mysqldatasourcebean" class= "Com.bean.MysqlDriveManagerDataSource"/> <bean
id= " Oracledatasourcebean "class=" Com.bean.OracleDriveManagerDataSource "/>
3. The test method is as follows:
@Test public
void Autowiredtest () {
Testbean bean = Ctx.getbean ("Testbean", testbean.class);
DataSource DataSource = Bean.getdatasource ();
if (DataSource instanceof mysqldrivemanagerdatasource) {
System.out.println ("MySQL");
} else if (DataSource instanceof oracledrivemanagerdatasource) {
System.out.println ("Oracle");
}
Datasource.connection ();
}
Because @Qualifier tags are not used in the configuration file, we inject beans into the bean
@Qualifier (value= "Mysqldatasourcebean")
(3), extended @qualifier qualifier descriptor annotations (without parameters): The extension of the @qualifier to provide a fine-grained selection candidate;
The way to use it is to customize an annotation and use @qualifier annotations to use it.
First let's consider the question, if we have two data sources, MySQL and Oracle respectively, so inject the two related resources involved in the database related, such as when the DAO layer injected sessionfactory, of course, can adopt the way described in the front, But in order to be simple and intuitive, we want to adopt a custom annotation approach.
1. Extended @qualifier Qualifier descriptor annotations to represent MySQL and Oracle data sources separately
Package com.annotation;
Import Java.lang.annotation.ElementType;
Import java.lang.annotation.Retention;
Import Java.lang.annotation.RetentionPolicy;
Import Java.lang.annotation.Target;
Import Org.springframework.beans.factory.annotation.Qualifier;
@Target ({elementtype.type,elementtype.field,elementtype.parameter})
@Retention (retentionpolicy.runtime)
@Qualifier public
@interface Mysql {
}
Package com.annotation;
Import Java.lang.annotation.ElementType;
Import java.lang.annotation.Retention;
Import Java.lang.annotation.RetentionPolicy;
Import Java.lang.annotation.Target;
Import Org.springframework.beans.factory.annotation.Qualifier;
@Target ({elementtype.type,elementtype.field,elementtype.parameter})
@Retention (retentionpolicy.runtime)
@Qualifier public
@interface Oracle {
}
2. Prepare the test bean:
Package Com.bean;
Import org.springframework.beans.factory.annotation.Autowired;
Import Com.annotation.Mysql;
Import com.annotation.Oracle;
public class Testbean {
private DataSource mysqldatasource;
Private DataSource Oracledatasource;
@Autowired public
void InitDataSource (@Mysql DataSource mysqldatasource, @Oracle DataSource oracledatasource) {
this.mysqldatasource = Mysqldatasource;
This.oracledatasource = Oracledatasource;
}
Public DataSource Getmysqldatasource () {
return mysqldatasource;
}
Public DataSource Getoracledatasource () {
return oracledatasource;
}
}
3. Add the following bean configuration in the Spring configuration file:
<bean id= "Testbean" class= "Com.bean.TestBean"/>
4. Modify the two data sources defined in spring: