I am using spring jdbc for DB connection.And each time i run my test java class spring,context is also loading each time. Any suggestion?

My code snippet are below,

appContext.xml:

<?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-2.0.xsd
           http://www.springframework.org/schema/context
           http://www.springframework.org/schema/beans/spring-context-2.0.xsd ">

    <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
        <property name="driverClassName" value="com.mysql.jdbc.Driver" />
        <property name="url" value="jdbc:mysql://localhost:3306/3i" />
        <property name="username" value="xxx" />
        <property name="password" value="xxx" />
    </bean>

    <bean id="jdbcTemp" class="org.springframework.jdbc.core.JdbcTemplate">
        <property name="dataSource" ref="dataSource">
        </property>
    </bean>
</beans>

Java class:

package com.pinovus.dbconnection;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.jdbc.core.JdbcTemplate;

public class DbDao {

    private static JdbcTemplate jdbctemplate;
    private static ApplicationContext appcontext;

    public JdbcTemplate getJdbctemplate() {

        ApplicationContext cx = new ClassPathXmlApplicationContext(
                "appContext.xml");
        jdbctemplate = (JdbcTemplate) cx.getBean("jdbcTemp");
        return jdbctemplate;
    }

    public void setJdbctemplate(JdbcTemplate jdbctemplate) {
        this.jdbctemplate = jdbctemplate;
    }

    /**
     * @param args
     */
    public static void main(String[] args) {
        // TODO Auto-generated method stub

    }

}

And test java class is :

package com.pinovus.dbconnection;

import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.support.rowset.SqlRowSet;

public class test {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        JdbcTemplate jt = new DbDao().getJdbctemplate();
        String qry = "select role from accounts";
        SqlRowSet rs = jt.queryForRowSet(qry);
        while (rs.next()) {
            System.out.println(rs.getString(1));
        }
    }
}
有帮助吗?

解决方案

Your DAO shouldn't know about the application context and shouldn't be explicitly looking up things in it. You could rewrite it using the Spring documentation as an example:

public class DbDaoImpl implements DbDao {

    private JdbcTemplate jdbcTemplate;

    public void setDataSource(DataSource dataSource) {
        this.jdbcTemplate = new JdbcTemplate(dataSource);
    }    
    ...
}

and you can delete the jdbcTemp entry from the application context xml. Instead make the DAO Spring-managed, creating an entry for it like this:

<bean id="dbDao" class="DbDaoImpl">
    <property name="dataSource" ref="dataSource"/>
</bean>

其他提示

That's because you create new instance each time:

new DbDao().getJdbctemplate();

and further:

new ClassPathXmlApplicationContext("appContext.xml");

It isn't an issue of Spring. It's up to your design.

And I don't know how to help you as it is just correct Java code: anyway in case of public static void main(String[] args) you have to instantiate objects to get deal with them.

Please, provide more info where are your doubts

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top