首页 > 解决方案 > @ComponentScan 在 org.springframework.context.annotation 包中不存在

问题描述

我是新的 spring 框架,我正在练习 java Configuration(No XML)

在这个例子中,我需要@Configuration 和@ComponentScan(),它们应该存在于org.springframework.context.annotation包中,但在这个包中@Configuration 可用但@ComponentScan 不可用。

每当我尝试使用 @ComponentScan 时,eclipse 都会建议我从org.springframework.config.java.plugin.context无法正常工作的包中导入 @ComponentScan。

导入时

org.springframework.config.java.plugin.context.ComponentScan

我收到以下错误。

Exception in thread "main" org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'name' is defined

NoXmlConfig.java <code>org.springframework.context.annotation</code> 包

标签: javaspring

解决方案


您使用的 Spring 版本 3.0.1 是不存在 @ComponentScan 的旧版本。@ComponentScan 是在 3.1 中引入的,你可以在这里查看 spring 文档

据说它从 3.1 开始就存在

在此处输入图像描述

请尝试 3.1 或更高版本。


推荐阅读