不使用元件扫描的Spring中的Autowire注释
是否可以使用不使用元件扫描的Spring中的Autowire注释,spring,dependency-injection,ioc-container,autowired,Spring,Dependency Injection,Ioc Container,Autowired,是否可以使用@Autowired注释自动连接bean而不使用组件扫描 是的负责发现用@Component、@Controller、@Service、@Respository等注释的bean 为了处理注释(@Autowired,@Resource等),您需要。因此,在applicationContext.xml中列出的bean上处理注释 据我所知,自动激活 Spring2.5和3.0都是如此。(感谢skaffman)我从未尝试过不启用组件扫描,但是我可以确认@Autowire annotation
@Autowired
注释自动连接bean而不使用组件扫描 是的
负责发现用@Component
、@Controller
、@Service
、@Respository
等注释的bean
为了处理注释(@Autowired
,@Resource
等),您需要
。因此,在applicationContext.xml
中列出的bean上处理注释
据我所知,
自动激活
Spring2.5和3.0都是如此。(感谢skaffman)我从未尝试过不启用组件扫描,但是我可以确认@Autowire annotations在Spring 3.0.x中工作,即使使用通过XML定义的bean。使用时,注释配置处理器始终注册,这意味着在@ComponentScan级别禁用它们的任何尝试都将被忽略。如果问题中的意思是您应该明确声明:
-xml文件中的
(它启用
)或
-@ComponentScan在您的java配置中 然后答案是-是的,可以在代码或xml文件中启用组件扫描,而无需任何上述语句 另一种方法是使用
注释ConfigApplicationContext
:
AnnotationConfigApplicationContext context=
new AnnotationConfigApplicationContext("org.example.your.package");
其中“org.example.your.package”是构造型注释类的包:@Component、@Repository、@Service等。
AnnotationConfigApplicationContext将在基本包和内部包中搜索您的bean。@Bozho Good(+1)看看这个问题:(+1)是的,在3.0中仍然是这样,我使用的是Spring 3.1,并且自动布线仅适用于。事实上,我不需要在bean中激活自动布线