为什么每次都加载应用程序上下文

时间:2014-03-31 13:46:01

标签: java spring spring-jdbc

我使用spring jdbc进行数据库连接。每次运行我的测试java类spring时,每次都会加载context。有什么建议吗?

我的代码段在下面,

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类:

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

    }

}

测试java类是:

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));
        }
    }
}

2 个答案:

答案 0 :(得分:1)

您的DAO不应该知道应用程序上下文,也不应该明确地查找其中的内容。您可以使用the Spring documentation作为示例重写它:

public class DbDaoImpl implements DbDao {

    private JdbcTemplate jdbcTemplate;

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

您可以从应用程序上下文xml中删除jdbcTemp条目。而是让DAO Spring管理,为它创建一个条目:

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

答案 1 :(得分:0)

那是因为你每次都创建新实例:

new DbDao().getJdbctemplate();

并进一步:

new ClassPathXmlApplicationContext("appContext.xml");

这不是Spring的问题。这取决于你的设计。

我不知道如何帮助你,因为它只是正确的Java代码:无论如何,public static void main(String[] args)你必须实例化对象来处理它们。

请提供更多信息您的怀疑在哪里

相关问题