f-angi
7/5/2017 - 7:54 AM

Spring Context: from xml to @Configuration

Spring Context: from xml to @Configuration

<?xml version="1.0"?>

<beans 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
		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:property-placeholder
            location="file:${portlet.conf.path}/ols-mass/ols-mass-commons.properties"/>

</beans>
@Configuration
@PropertySource(value = "file:${portlet.conf.path}/ols-mass/ols-mass-commons.properties")
public class SpringContextConfiguration {
}
@Configuration
@ComponentScan(basePackages = "it.intesys.arag.mass.commons")
public class SpringContextConfiguration {
}
<?xml version="1.0"?>

<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:tx="http://www.springframework.org/schema/tx"
       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
        http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd">

    <context:property-placeholder
            location="file:${portlet.conf.path}/ols-mass/ols-mass-commons.properties"/>

    <tx:annotation-driven transaction-manager="txManager"/>
    <bean id="txManager"
          class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="aragWebDataSource"/>
    </bean>

    <bean id="aragWebDataSource" class="org.springframework.jndi.JndiObjectFactoryBean">
        <property name="jndiName"
                  value="java:comp/env/jdbc/${dao.arag-web-data-source}"/>
    </bean>

</beans>
<?xml version="1.0"?>

<beans 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
		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:component-scan base-package="it.intesys.arag.mass.commons"/>

</beans>
@Configuration
@EnableTransactionManagement
@PropertySource(value = "file:${portlet.conf.path}/ols-mass/ols-mass-commons.properties")
/**
 * http://docs.spring.io/spring-framework/docs/4.3.8.RELEASE/javadoc-api/org/springframework/transaction/annotation/EnableTransactionManagement.html
 * /
public class SpringContextConfiguration {

    @Value("${dao.arag-web-data-source}")
    String aragWebDataSource;

    @Bean(name = "aragWebDataSource")
    public DataSource dataSource()
        throws NamingException {

        JndiTemplate jndi = new JndiTemplate();
        return jndi.lookup("java:comp/env/jdbc/" + aragWebDataSource, DataSource.class);
    }

    @Bean
    public PlatformTransactionManager txManager()
                    throws NamingException {

        return new DataSourceTransactionManager(dataSource());
    }

}