Spring 的 Bean

哪些Bean可以被Spring管理?

1. 通过xml配置的 Bean

2. 通过注解被Spring管理的Bean

  • @component
  • @Service
  • @Configuration

Bean的注入方式

  只有被Spring管的Bean才可以通过注解进行注入;

  

1. @Autowired

2. @Resource

Bean注入失败的常见情况

  1. 如果一个Bean在A类中可以被注入,但是在B类中注入显示为null,则可能B类没有被Spring容器进行管理,可能是通过 new Object() 生成的;

猜你喜欢

转载自www.cnblogs.com/virgosnail/p/10165866.html