2015-02-06 130 views
0

我使用Spring框架4.1.4是否有可能与Java

混合Spring XML配置我有JBoss5XmlWebApplicationContext作为我contextClass读取XML配置。

我想添加@Configuration类以便从xml中读取。 这可能吗?怎么做?

+0

是的,这是可能的。 – Jens 2015-02-06 10:47:32

+0

请问您可以详细说明如何做到这一点? – user3750768 2015-02-06 11:00:08

回答

0

是的,可能的。我知道的一种方法是使用springboot类。我使用MySpringConfiguration类来使用@Configuration和MySpringConfig.xml为基于xml的bean配置声明bean。我可以从两个来源的上下文中加载bean。 检查下面的代码:

public static void main(String[] args) { 

     Resource res = new FileSystemResource("E:\\MySpringConfig.xml"); 
     SpringApplication app = new SpringApplication(MySpringConfiguration.class,res); 
     ApplicationContext ctx = app.run(""); 
     MyXMLBean myXMLBean = (MyXMLBean)ctx.getBean("myNewBean"); // From XML file... 
     GeData geData= (GeData)ctx.getBean("geData"); // From Java Configuration ... 
     .... 

配置类是这样

@Configuration 
public class MySpringConfiguration { 

    @Bean 
    public GeData geData(){ 
     return new GeData(); 
    } 

希望这有助于...

+0

虽然这确实回答了标题问题,但它并没有回答我提到的有关与 JBoss5XmlWebApplicationContext混合的具体情况。无论如何我会为此努力投票,谢谢。 (当我没有rep足够upvote ..大声笑) – user3750768 2015-02-09 04:45:02

+0

我相信JBoss5XmlWebApplicationContext也可以加载,你可以检查SpringApplication APIs,那里你可以检查从其他来源加载到其他来源的方法(也许加载())任何实现ApplicationContext的上下文。 – Sharman25 2015-02-09 08:56:46