Spring 为什么春天没有';当@Configuration从applicationContext.xml加载bean时,它看不到@Configuration吗?
我有以下applicationContext.xml文件:Spring 为什么春天没有';当@Configuration从applicationContext.xml加载bean时,它看不到@Configuration吗?,spring,configuration,spring-java-config,Spring,Configuration,Spring Java Config,我有以下applicationContext.xml文件: <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schem
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.1.xsd">
<bean class="com.app.config.AppConfig"></bean>
</beans>
但是如果我运行应用程序,那么Spring将不会加载AppConfig,因为我在@Autowired
依赖项上获得NullPointerExceptions
。因此,就像Spring没有在AppConfig类中加载JavaConfig@Bean
定义,而是将该类视为一个简单的@Component
而不是@Configuration
组件,后者又可以包含Bean定义
只有在applicationContext.xml中添加
定义,而不是在AppConfig中添加@ComponentScan
注释时,一切才起作用,如下所示:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.1.xsd">
<bean class="com.app.config.AppConfig"></bean>
<context:component-scan base-package="com.app" />
</beans>
现在,为什么Spring在从
applicationContext.xml
加载AppConfig
bean时没有看到@Configuration注释,如果applicationContext.xml
没有
?<context:annotation-config/>
我猜您的xml中缺少注释配置。在appcontext.xml中包含以下内容。这是处理注释所必需的。谢谢
<context:annotation-config/>
我猜您的xml中缺少注释配置。在appcontext.xml中包含以下内容。这是处理注释所必需的。谢谢
<context:annotation-config/>
我猜您的xml中缺少注释配置。在appcontext.xml中包含以下内容。这是处理注释所必需的。谢谢
<context:annotation-config/>
谢谢,这是丢失的东西!谢谢,这是丢失的东西!谢谢,这是丢失的东西!谢谢,这是丢失的东西!