Spring下的@Order和@Primary与javax.annotation-api下@PrioritySpring4.1后等方法控制多实现的依赖注入(转)

Posted 木西 - Muxy

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Spring下的@Order和@Primary与javax.annotation-api下@PrioritySpring4.1后等方法控制多实现的依赖注入(转)相关的知识,希望对你有一定的参考价值。

@Order 可以作用在类、方法、属性。 影响加载顺序。 若不加,spring的加载顺序是随机的。

@Primary 当注入bean冲突时,以@Primary定义的为准。

@Order是控制配置类的加载顺序,还能控制List<XXX> 里面放的XXX的实现注入的顺序,下面介绍使用Spring下的@Order和@Primary与javax.annotation-api下@Priority【Spring4.1后,参考:https://jira.spring.io/browse/SPR-10548】控制多实现的依赖注入,因为在项目中可能会遇到接口多实现的问题,那么要具体注入那个接口的实现就需要我们来控制了,那么如何控制,请看下面的例子示例:

示例一:

public interface XStrategyService {
}
 
@Service
@Priority(1)
public class XStrategyServiceImpl01 implements XStrategyService {
}
 
@Service
@Priority(2)
public class XStrategyServiceImpl02 implements XStrategyService {
}
 
@Autowired
private XStrategyService xStrategyService;  // XStrategyServiceImpl01将被选择

示例二:

public interface XStrategyService {
}
 
@Service
@Primary
public class XStrategyServiceImpl01 implements XStrategyService {
}
 
@Service
public class XStrategyServiceImpl02 implements XStrategyService {
}
 
@Autowired
private XStrategyService xStrategyService;  // XStrategyServiceImpl01将被选择

示例三:

public interface XStrategyService {
}
 
@Service
@Order(1)
@Primary
public class XStrategyServiceImpl01 implements XStrategyService {
}
 
@Service
@Order(2)
public class XStrategyServiceImpl02 implements XStrategyService {
}
 
@Autowired
private XStrategyService xStrategyService;  // XStrategyServiceImpl01将被选择

注意,下面的是不能注入的,就是单独使用@Order(1), @Order(2) Order注解仅仅用于控制组件的加载顺序,不能控制注入优先级

public interface XStrategyService {
}
 
@Service
@Order(1)
public class XStrategyServiceImpl01 implements XStrategyService {
}
 
@Service
@Order(2)
public class XStrategyServiceImpl02 implements XStrategyService {
}
 
@Autowired
private XStrategyService xStrategyService;  // Error

一些其他方法完成多实现的注入,使用@Qualifier限定符

public interface XStrategyService {
}
 
@Service
@Qualifier("defaultStrategyService")
public class XStrategyServiceImpl01 implements XStrategyService {
}
 
@Service
public class XStrategyServiceImpl02 implements XStrategyService {
}
 
@Autowired
@Qualifier("defaultStrategyService")
private XStrategyService xStrategyService;  // XStrategyServiceImpl01将被选择

 

使用@Resource,通过Bean的名称限定选择,和@Qualifier限定符类似

public interface XStrategyService {
}
 
@Service("xStrategyServiceImpl01")
public class XStrategyServiceImpl01 implements XStrategyService {
}
 
@Service("xStrategyServiceImpl02")
public class XStrategyServiceImpl02 implements XStrategyService {
}
 
@Resource(name = "xStrategyServiceImpl01")
private XStrategyService xStrategyService; // XStrategyServiceImpl01将被选择

特例:Spring4.0后的新特性,就是根据泛型类型可以自动选择注入 

public interface XStrategyService<T> {
}
 
@Service
public class XStrategyServiceImpl01 implements XStrategyService<XXX> {
}
 
@Service
public class XStrategyServiceImpl02 implements XStrategyService<YYY> {
}
 
@Autowired
private XStrategyService<XXX> xStrategyService; // XStrategyServiceImpl01将被选择

补充@Order结合@Bean的用法:

public interface XStrategyService {
}
 
@Service
public class XStrategyServiceImpl01 implements XStrategyService {
}
 
@Service
public class XStrategyServiceImpl02 implements XStrategyService {
}
 
@Bean
@Order(1)
XStrategyServiceImpl01 xStrategyServiceImpl01() {
    return new XStrategyServiceImpl01();
}
@Bean
@Order(2)
XStrategyServiceImpl02 xStrategyServiceImpl02() {
    return new XStrategyServiceImpl02();
}
@Autowired
private List<XStrategyService> xStrategyServices; // XStrategyServiceImpl01 first

原文链接:https://blog.csdn.net/weixin_42465125/article/details/88574670

 

以上是关于Spring下的@Order和@Primary与javax.annotation-api下@PrioritySpring4.1后等方法控制多实现的依赖注入(转)的主要内容,如果未能解决你的问题,请参考以下文章

Spring Boot 2.1 bean 覆盖与 Primary

关于@Profile@Conditional@Primary@Qualifier及@Scope等实现高级装配的spring注解

如何更换在HA SSO模式下的Primary WLC或安装license

Shiro —— Spring 环境下的使用

spring的@primary和@qualifier注解解决一个接口多个实现的注入问题

Spring注解@Primary的意思