如何以编程方式启用"annotation-config"在春季ApplicationContext [英] How do I programmatically enable "annotation-config" in spring ApplicationContext
问题描述
通常在春季启用@Autowired之类的注释是通过将其包含在春季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/>
并将其作为导入添加到我的Application上下文类中
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"在春季ApplicationContext的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!