如何以编程方式启用“annotation-config"?在 Spring ApplicationContext 中 [英] How do I programmatically enable "annotation-config" in spring ApplicationContext
问题描述
通常在 spring 中启用注解(例如 @Autowired)是通过将其包含在 spring XML 中来完成的.
Normally enabling annotations in spring such as @Autowired is done by including this in the spring XML.
<context:annotation-config/>
有没有一种方法可以在初始化之前在 ApplicationContext(或实现)上以编程方式执行此操作?
Is there a way I can do this programmatically on the ApplicationContext (or implementation) before initialising it?
推荐答案
我在这个问题上挣扎了很多,作为一种解决方法,我创建了一个包含以下内容的简单 spring-aspect-config.xml 文件
I struggled a lot with this one, as a workaround i created a simple spring-aspect-config.xml file with following content
<?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.xsd">
<context:spring-configured/>
并将其作为导入添加到我的应用程序上下文类
and added it as import to my Application context class
@ImportResource("classpath:spring-aspect-config.xml")
以编程方式,您可以通过指定
Programmatically you can do the same by specifying
@EnableSpringConfigured
关于您的应用程序上下文类.
on you application context class.
参考 - https://jira.springsource.org/browse/SPR-7888
这篇关于如何以编程方式启用“annotation-config"?在 Spring ApplicationContext 中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!