关于Spring中自定义限定器的使用
来源:互联网 发布:淘宝的kindle 官方重封 编辑:程序博客网 时间:2024/06/01 15:32
除了使用Qualifier注释器,还可以使用自定义相关的限定器。使用自定义注释器时,要以Qualifier注释作为他的元注解。
如下面的例子:
@Target({ElementType.FIELD,ElementType.PARAMETER,ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Qualifier
public @interface PonitPerson {
}
@Retention(RetentionPolicy.RUNTIME)
@Qualifier
public @interface PonitPerson {
}
上面的代码自定了一个PointPerson限定器,相关的工程结构如下图所示。其中Person是一个接口,BlackMan与YelloMan实现了该接口,
代码如下:
package Bena;
@PonitPerson
public class BlackMan implements Person{
public String address="African";
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
}
要在WorldPerson里面注入一个Person类的Bean,其中BlackMan与YelloMan都可以进行相关的匹配,为了能够指明要匹配的Bean,在BlackMan内使用自定义的注释。然后WorldPerson代码如下:
@PonitPerson
public class BlackMan implements Person{
public String address="African";
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
}
要在WorldPerson里面注入一个Person类的Bean,其中BlackMan与YelloMan都可以进行相关的匹配,为了能够指明要匹配的Bean,在BlackMan内使用自定义的注释。然后WorldPerson代码如下:
package Bena;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
public class WorldPerson {
@Autowired
@PonitPerson
public Person person;//此时会注入的Bean是所有匹配的Bean中标记注释为PonitPerson的Bean,即BlackMan
public Person getPerson() {
return person;
}
public void setPerson(Person person) {
this.person = person;
}
}
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
public class WorldPerson {
@Autowired
@PonitPerson
public Person person;//此时会注入的Bean是所有匹配的Bean中标记注释为PonitPerson的Bean,即BlackMan
public Person getPerson() {
return person;
}
public void setPerson(Person person) {
this.person = person;
}
}
测试代码如下:
package Main;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import Bena.WorldPerson;
public class MainClass {
public static void main(String[]args)
{
ApplicationContext context=new ClassPathXmlApplicationContext("Bean.xml");
WorldPerson man=(WorldPerson)context.getBean("worldMan");
System.out.println(man.person.toString());
}
}
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import Bena.WorldPerson;
public class MainClass {
public static void main(String[]args)
{
ApplicationContext context=new ClassPathXmlApplicationContext("Bean.xml");
WorldPerson man=(WorldPerson)context.getBean("worldMan");
System.out.println(man.person.toString());
}
}
输出:Bena.BlackMan@10b40af
整个demo源码已经附上:
http://download.csdn.net/detail/muyundefeng/9477971
0 0
- 关于Spring中自定义限定器的使用
- Struts中关于用户权限限定的建议
- Struts中关于用户权限限定的建议
- Spring中使用自定义的注解校验器的实现
- spring aop 中@annotation()的使用,关于自定义注解,绝壁原创的文章
- spring中使用自定义的事件
- Spring Boot中使用自定义的properties
- 关于android中自定义contentprovider的使用
- Struts中关于用户权限限定的建议(转)
- 关于在Spring中注册自定义的PropertyEditor
- 关于在Spring中注册自定义的PropertyEditor
- spring mvc中关于json的使用
- 关于spring中自定义编辑器propertyEidtor
- 在 spring security 中使用自定义的错误消息
- 在自定义Listener中使用Spring容器管理的bean
- spring中自定义Event事件的使用和浅析
- Spring Boot项目中自定义注解的使用
- Spring Boot项目中自定义注解的使用
- JSP九大内置对象和四大作用域及其作用
- pagination in angularjs
- java调用存储过程
- Web jquery ajax,EL遍历 js刷新 jquery遍历json数组填充表格 等等随笔
- Billboard(线段树)
- 关于Spring中自定义限定器的使用
- mysql explain用法
- random walk
- ios: XcodeColors插件输出颜色不变
- Future的使用
- 最速下降法/steepest descent,牛顿法/newton,共轭方向法/conjugate direction,共轭梯度法/conjugate gradient 及其他
- error LNK2005: _DllMain@12 已经在 MSVCRTD.lib(dllmain.obj) 中定义
- MyBatis的like模糊查询
- 面试算法——寻找第K大的数(快速排序的应用)