royjavelosa
royjavelosa

Reputation: 2068

How to simplify adding multiple beans in the xml file which uses the DataSource in Spring

Im using Spring 3.0 and JDBC I'm currently experimenting with spring for a desktop application. Each time a class would need to use the datasource declared on my spring_bean_file.xml I would need to declare each of them in the xml file and again initialize each of them in my class.
Below is my xml file

<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">       
        <property name="driverClassName" value="oracle.jdbc.driver.OracleDriver"/>
        <property name="url" value="#{T(java.lang.System).getenv(DB_URL')}"/>
        <property name="username" value="#{T(java.lang.System).getenv('DB_USER')}"/>
        <property name="password" value="#{T(java.lang.System).getenv('DB_PASS')}"/>        
    </bean>


<bean id="classA" class="com.example.ClassA">
    <property name="dataSource" ref="dataSource"/>
</bean>

<bean id="classB" class="com.example.ClassB">
    <property name="dataSource" ref="dataSource"/>
</bean>

This is my java code:

public static void main(String[] args) {        

        ApplicationContext context = new ClassPathXmlApplicationContext("spring_bean_file.xml");
            ClassA classA = (ClassA) context.getBean("classA");
            ClassB classB = (ClassB) context.getBean("classB");
            try {
                rrRpi.generateRrRpi();
                rrSpi.generateRrSpi();
            } catch (SQLException e) {

                e.printStackTrace();
            } catch (IOException e) {

                e.printStackTrace();
            }

         //close the context             
        ((ClassPathXmlApplicationContext) context).close(); 

    }

Id like to know if theres a more efficient way of doing this. Whats the best approach so I wouldnt need to add a bean for each class that would use the DataSoruce. Thanks in advance guys.

Upvotes: 0

Views: 2086

Answers (1)

Tomasz Nurkiewicz
Tomasz Nurkiewicz

Reputation: 340993

You can use:

Annotations and CLASSPATH scanning

@Service
public class ClassA {
    @Resource
    private DataSource dataSource;

    //...
}

@Service
public class ClassB {
    @Resource
    private DataSource dataSource;

    //...
}

dataSource definition is still required to be in the XML, you also need to replace classA and classB <bean/> definitions with simple:

<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:component-scan base-package="com.example" />

    <!-- ... -->
</beans

@Configuration

Or you can take advantage of very new but exciting Java configuration:

@Configuration
public class Cfg {

    @Bean
    public DataSource dataSource() {
        BasicDataSource ds = new BasicDataSource();
        ds.setDriverClassName("oracle.jdbc.driver.OracleDriver);
        //...
        return ds;
    }

    @Bean
    public ClassA classA() {
        ClassA ca = new ClassA()
        ca.setDataSource(dataSource());
        return ca;
    }

    @Bean
    public ClassB classB() {
        ClassB cb = new ClassB()
        cb.setDataSource(dataSource());
        return cb;
    }

}

The benefit of this approach is that you don't need XML, at all:

new AnnotationConfigApplicationContext(Cfg.class);

See also

Upvotes: 1

Related Questions