/java/

Mixed Spring configuration.

2015-02-23 21:09:03

Sometimes when you want to migrate from one approach to other, you have to use both for some time. That is why, it is good to know, how to use xml configuration with java configuration and opposite way.

Example 1 - Java Configuration with Xml Extension

Application.java

package pl.btbw;

import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;

public class Application {

    public static void main(String[] args) throws InterruptedException {

        ApplicationContext context = new AnnotationConfigApplicationContext(AppConfig.class);
        BeanA beanA = context.getBean("beanA", BeanA.class);

    }
}

AppConfig.java

package pl.btbw;

import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.ImportResource;

@Configuration
@ComponentScan("pl.btbw")
@ImportResource("application-context.xml")
public class AppConfig {
}

Important part: @ImportResource application-context.xml

<?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?>

<beans beans="" http:="" schema="" spring-beans.xsd&quot;="" www.springframework.org="" xmlns='"http://www.springframework.org/schema/beans"' xmlns:xsi='"http://www.w3.org/2001/XMLSchema-instance"' xsi:schemalocation='"http://www.springframework.org/schema/beans'>
<bean class='"pl.btbw.BeanC"/' id='"beanC"'>
</bean></beans>

Example 2 - Xml configuration with Java Extension

Application.java

package pl.btbw;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class Application {

    public static void main(String[] args) throws InterruptedException {

        ApplicationContext context = new ClassPathXmlApplicationContext("application-context.xml");
        BeanA beanA = context.getBean("beanA", BeanA.class);

    }
}

AppConfig.java

package pl.btbw;

import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;

@Configuration
@ComponentScan("pl.btbw")
public class AppConfig {
}

application-context.xml

<?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?>

<beans beans="" context="" http:="" schema="" spring-beans.xsd="" spring-context-4.0.xsd&quot;="" www.springframework.org="" xmlns='"http://www.springframework.org/schema/beans"' xmlns:context='"http://www.springframework.org/schema/context"' xmlns:xsi='"http://www.w3.org/2001/XMLSchema-instance"' xsi:schemalocation='"http://www.springframework.org/schema/beans'>
<context:annotation-config></context:annotation-config>
<bean class='"pl.btbw.AppConfig"/'>
<bean class='"pl.btbw.BeanC"/' id='"beanC"'>
</bean></bean></beans>

Important part: context:annotation & bean class="pl.btbw.AppConfig"

Other file

package pl.btbw;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Component;

@Component
@Lazy
public class BeanA {

    @Autowired
    private BeanB beanB;

    private final static Logger LOG = Logger.getLogger(BeanA.class);

    public BeanA() {
        LOG.info("initialization");
    }

}
package pl.btbw;

import org.apache.log4j.Logger;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Component;

@Component
@Lazy
public class BeanB {

    private final static Logger LOG = Logger.getLogger(BeanB.class);

    public BeanB() {
        LOG.info("initialization");
    }
}
package pl.btbw;

import org.apache.log4j.Logger;

public class BeanC {

    private final static Logger LOG = Logger.getLogger(BeanC.class);

    public BeanC() {
        LOG.info("initialization");
    }
}
<?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?>

<project http:="" maven-4.0.0.xsd&quot;="" maven.apache.org="" xmlns='"http://maven.apache.org/POM/4.0.0"' xmlns:xsi='"http://www.w3.org/2001/XMLSchema-instance"' xsd="" xsi:schemalocation='"http://maven.apache.org/POM/4.0.0'>
<modelversion>4.0.0</modelversion>
<groupid>pl.btbw</groupid>
<artifactid>spring-bean-demo</artifactid>
<version>1.0-SNAPSHOT</version>
<dependencies>
<dependency>
<groupid>org.springframework</groupid>
<artifactid>spring-core</artifactid>
<version>4.0.4.RELEASE</version>
</dependency>
<dependency>
<groupid>org.springframework</groupid>
<artifactid>spring-context</artifactid>
<version>4.0.4.RELEASE</version>
</dependency>
<dependency>
<groupid>commons-logging</groupid>
<artifactid>commons-logging</artifactid>
<version>1.1.3</version>
</dependency>
<dependency>
<groupid>log4j</groupid>
<artifactid>log4j</artifactid>
<version>1.2.17</version>
</dependency>
</dependencies>
</project>