标签:user 注意 logs public -- 示例 str code 调用
Java:
1.8
Maven:
3
SpringFramework版本以及各组件成员:
5.1.1.RELEASE
- spring-context
- spring-core
- spring-beans
官方文档: 1.9.4 Using Generics as Autowiring Qualifiers
您可以创建自己的自定义限定符注释。为此,请定义注释并在定义中提供@Qualifier注释.
背景: 影院影片播放程序.
让执行者(
Runner.java
)调用BeanUser
来选定特定的影片进行播放, 但它们都基于Bean
类创建, 这需要通过某些设定来做限定.
Runner.java
要做的是先放映动作片ActionMovie
, 再放映喜剧片ComedyMovie
实际上在这需求场景
@Qualifier
就够用了...... 自定义@Qualifier
只是在不使用Beanid
匹配时所提供的另一种弥补方案. (个人看法)
@Qualifier
package noioo;
import org.springframework.beans.factory.annotation.Qualifier;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
@Target({ElementType.FIELD, ElementType.PARAMETER})
@Retention(RetentionPolicy.RUNTIME)
@Qualifier
public @interface Genre{
// 注意: value指的是@Genre匹配时的值, 并不是注入的固定值.
String value();
}
Bean.java
package noioo;
public class Bean {
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
BeanUser.java
package noioo;
import org.springframework.beans.factory.annotation.Autowired;
public class BeanUser {
@Autowired
@Genre("ComedyMovie") //匹配的值(value)
private Bean comedyMovie;
private Bean actionMovie;
@Autowired
public BeanUser(@Genre("ActionMovie") Bean actionMovie) {
this.actionMovie = actionMovie;
}
public void watchTheActionMovie() {
System.out.println(actionMovie.getName());
}
public void watchTheComedyMovie() {
System.out.println(comedyMovie.getName());
}
}
spring-beans.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
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">
<context:annotation-config/>
<bean id="beanUser" class="noioo.BeanUser">
</bean>
<bean id="want" class="noioo.Bean">
<!--使用qualifier标签指定我们所自定义的注解-->
<!--注意: value指的是@Genre匹配时的值, 并不是注入的固定值.-->
<qualifier type="noioo.Genre" value="ActionMovie"/>
<!--以下才是注入的固定值-->
<property name="name" value="现在放映的是动作片"/>
</bean>
<bean class="noioo.Bean">
<qualifier type="noioo.Genre" value="ComedyMovie"/>
<property name="name" value="现在放映的是喜剧片"/>
</bean>
</beans>
Runner.java
package noioo;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Runner {
public static void main(String[] args){
ApplicationContext context = new ClassPathXmlApplicationContext("spring-beans.xml");
BeanUser beanUser = (BeanUser) context.getBean("beanUser");
beanUser.watchTheActionMovie();
System.out.println("........");
beanUser.watchTheComedyMovie();
}
}
现在放映的是动作片
........
现在放映的是喜剧片
Process finished with exit code 0
标签:user 注意 logs public -- 示例 str code 调用
原文地址:https://www.cnblogs.com/shwo/p/9863961.html