Autowired已经灭亡,请使用构造方法注入依赖
这篇文章没有什么好讲的,就是提醒自己不要再使用Autowired引入依赖。
@AutowiredOrderService orderService;
@Autowired
BusinessExamineService businessExamineService;
OrderService orderService;BusinessExamineService businessExamineService;
public OrderController(OrderService orderService, BusinessExamineService businessExamineService) {
this.orderService = orderService;
this.businessExamineService = businessExamineService;
}
1: @Autowired 引入的依赖有可能为null, 但方法注入的依赖不会为null
2:方法注入依赖更加高效。
3:开发者不再需要关注bean的构造顺序。 不需要关注 @Order。
数字支撑
org.springframework.boot 1.5.6 + Edgware.SR5
org.springframework.boot 2.2.1 +Finchley.SR2
@Autowired 使用量
100+
0
@Order
30+
0
备注,这种用法也可以引用相应得依赖。
@BeanSimpleMessageListenerContainer container(ConnectionFactory connectionFactory) {
SimpleMessageListenerContainer container = new SimpleMessageListenerContainer();
container.setConnectionFactory(connectionFactory);
container.setQueueNames(queueName);
MessageListenerAdapter listenerAdapter = new MessageListenerAdapter(receiver1, "receiveMessage");
container.setMessageListener(listenerAdapter);
return container;
}
而下面这种用法则是不提倡的
@AutowiredConnectionFactory connectionFactory ;
@Bean
SimpleMessageListenerContainer container() {
SimpleMessageListenerContainer container = new SimpleMessageListenerContainer();
container.setConnectionFactory(connectionFactory);
container.setQueueNames(queueName);
MessageListenerAdapter listenerAdapter = new MessageListenerAdapter(receiver1, "receiveMessage");
container.setMessageListener(listenerAdapter);
return container;
}
以上是 Autowired已经灭亡,请使用构造方法注入依赖 的全部内容, 来源链接: utcz.com/z/511245.html