Spring中的自动装配注释,不使用组件扫描

问题描述 投票:6回答:3

是否可以在不使用组件扫描的情况下使用@Autowired注释自动装配bean?

spring dependency-injection ioc-container autowired
3个回答
4
投票

是。 <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)


0
投票

我从未尝试过不启用组件扫描,但是即使在通过XML定义的bean中,我也可以确认@Autowire注释在Spring 3.0.x中有效。


0
投票

使用AnnotationConfigApplicationContext时,注解配置处理器总是被注册,这意味着在@ComponentScan级别禁用它们的任何尝试都将被忽略。

© www.soinside.com 2019 - 2024. All rights reserved.