@Qualifier注解用来指定自动注入时合作bean需要满足的qualifier的名称,如果一个bean没有指定qualifier属性,则用它的id为qualifier,来给别的bean提供注入。
示例文章来源:https://www.toymoban.com/news/detail-670278.html
package cn.edu.tju.domain;
public class MovieCatalog {
}
package cn.edu.tju.domain;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
public class MovieRecommender {
@Autowired
@Qualifier("secondary")
private MovieCatalog movieCatalog;
}
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:mvc="http://www.springframework.org/schema/mvc"
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/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
<context:annotation-config/>
<bean id = "main" class="cn.edu.tju.domain.MovieCatalog">
<qualifier value="secondary"/>
</bean>
<bean id="recommender" class="cn.edu.tju.domain.MovieRecommender">
</bean>
</beans>
package cn.edu.tju.test;
import cn.edu.tju.domain.People;
import cn.edu.tju.domain.Person;
import com.alibaba.druid.pool.DruidDataSource;
import com.alibaba.druid.pool.DruidPooledConnection;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.ConfigurationClassPostProcessor;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import java.sql.SQLException;
public class Test09 {
public static void main(String[] args) throws Exception {
ConfigurableApplicationContext ctx = new
ClassPathXmlApplicationContext("spring9.xml");
}
}
##########################################################文章来源地址https://www.toymoban.com/news/detail-670278.html
package cn.edu.tju.domain;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
public class MovieRecommender2 {
private MovieCatalog movieCatalog;
public MovieCatalog getMovieCatalog() {
return movieCatalog;
}
public void setMovieCatalog(MovieCatalog movieCatalog) {
this.movieCatalog = movieCatalog;
}
@Autowired
public void prepare(@Qualifier("secondary") MovieCatalog movieCatalog) {
this.movieCatalog = movieCatalog;
}
}
到了这里,关于Spring复习:(59)@Qualifier注解的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!