Spring下的@Order和@Primary与javax.annotation-api下@Priority【Spring4.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

 

posted on 2019-10-30 12:31  木西-Muxy  阅读(1565)  评论(0编辑  收藏  举报

导航