java - 为什么spring应用程序上下文每次都要加载

标签 java spring spring-jdbc

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

我的代码片段如下,

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

最佳答案

您的 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>

关于java - 为什么spring应用程序上下文每次都要加载,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22763243/

相关文章:

java - BoofCV/Java - 使用高斯卷积构造图像的 Hessian 矩阵

java - Spring webServiceTemplate 连接超时属性

jdbctemplate - 无法在 QueryDslJdbcTemplate 上调用 list() 方法

java - 表示层与 Apache 扭矩一起使用

java - 我的 Spring Batch 上下文有什么问题?

java - Spring RowMapper 返回一个结果的 ArrayList

java - transient 属性值异常 : object references an unsaved transient instance - save the transient instance before flushing

java - do-while(false) 的优点是什么?

java - 线程 "main"java.awt.AWTError : BoxLayout can't be shared 中的异常

java - 使用DES在java中使用私钥而不自动生成 key