为什么 Spring 在从 applicationContext.xml 加载 bean 时看不到 @Configuration?
Why Spring doesn't see @Configuration when it loads a bean from applicationContext.xml?
我有以下 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/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>
以及以下配置 class:
package com.app.config;
@Configuration
@ComponentScan("com.app")
public class AppConfig {
// actual beans definition
@Bean
...
@Bean
...
@Bean
...
}
但是如果我 运行 应用程序然后 Spring 将不会加载 AppConfig 因为我在 @Autowired
依赖项上得到 NullPointerExceptions
。所以就像 Spring 没有在 AppConfig class 中加载 JavaConfig @Bean
定义,而是将 class 视为简单的 @Component
而不是 @Configuration
组件又可以包含 bean 定义。
仅当我在 applicationContext.xml 中添加 <context:component-scan>
定义而不是在 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>
AppConfig 变得简单:
package com.app.config;
@Configuration // no more @ComponentScan (it's inside applicationContext.xml)
public class AppConfig {
// actual beans definition
@Bean
...
@Bean
...
@Bean
...
}
现在,如果 applicationContext.xml
没有 <context:component-scan>
?
我猜您的 xml 中缺少注释配置。在您的 appcontext.xml 中包含以下内容。这是处理您的注释所必需的。谢谢
<context:annotation-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/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>
以及以下配置 class:
package com.app.config;
@Configuration
@ComponentScan("com.app")
public class AppConfig {
// actual beans definition
@Bean
...
@Bean
...
@Bean
...
}
但是如果我 运行 应用程序然后 Spring 将不会加载 AppConfig 因为我在 @Autowired
依赖项上得到 NullPointerExceptions
。所以就像 Spring 没有在 AppConfig class 中加载 JavaConfig @Bean
定义,而是将 class 视为简单的 @Component
而不是 @Configuration
组件又可以包含 bean 定义。
仅当我在 applicationContext.xml 中添加 <context:component-scan>
定义而不是在 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>
AppConfig 变得简单:
package com.app.config;
@Configuration // no more @ComponentScan (it's inside applicationContext.xml)
public class AppConfig {
// actual beans definition
@Bean
...
@Bean
...
@Bean
...
}
现在,如果 applicationContext.xml
没有 <context:component-scan>
?
我猜您的 xml 中缺少注释配置。在您的 appcontext.xml 中包含以下内容。这是处理您的注释所必需的。谢谢
<context:annotation-config/>