是否可以在不使用组件扫描的情况下使用@Autowired
注释自动装配bean?
是。 <context-component-scan .. />
负责发现带有@Component
,@Controller
,@Service
,@Respository
等注释的bean。
为了处理注释(@Autowired
,@Resource
等),您需要<context:annotation-config />
。因此,注释将在applicationContext.xml
中列出的bean上处理。
据我所知,<context-component-scan .. />
自动激活<context:annotation-config />
。
对于spring 2.5和3.0都是如此。 (感谢skaffman)
我从未尝试过不启用组件扫描,但是即使在通过XML定义的bean中,我也可以确认@Autowire注释在Spring 3.0.x中有效。
使用AnnotationConfigApplicationContext时,注解配置处理器总是被注册,这意味着在@ComponentScan级别禁用它们的任何尝试都将被忽略。