标签:加载 int ann tail 控制 wired 项目 spring4 default
@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将被选择
public interface XStrategyService { } @Service @Order(1) public class XStrategyServiceImpl01 implements XStrategyService { } @Service @Order(2) public class XStrategyServiceImpl02 implements XStrategyService { } @Autowired private XStrategyService xStrategyService; // Error
public interface XStrategyService { } @Service @Qualifier("defaultStrategyService") public class XStrategyServiceImpl01 implements XStrategyService { } @Service public class XStrategyServiceImpl02 implements XStrategyService { } @Autowired @Qualifier("defaultStrategyService") private XStrategyService xStrategyService; // XStrategyServiceImpl01将被选择
public interface XStrategyService { } @Service("xStrategyServiceImpl01") public class XStrategyServiceImpl01 implements XStrategyService { } @Service("xStrategyServiceImpl02") public class XStrategyServiceImpl02 implements XStrategyService { } @Resource(name = "xStrategyServiceImpl01") private XStrategyService xStrategyService; // XStrategyServiceImpl01将被选择
public interface XStrategyService<T> { } @Service public class XStrategyServiceImpl01 implements XStrategyService<XXX> { } @Service public class XStrategyServiceImpl02 implements XStrategyService<YYY> { } @Autowired private XStrategyService<XXX> xStrategyService; // XStrategyServiceImpl01将被选择
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下@Priority【Spring4.1后】等方法控制多实现的依赖注入(转)
标签:加载 int ann tail 控制 wired 项目 spring4 default
原文地址:https://www.cnblogs.com/muxi0407/p/11764162.html