DB 연결에 spring jdbc를 사용하고 있습니다. 테스트 용 Java 클래스 스프링을 실행할 때마다 컨텍스트도로드 될 때마다로드됩니다. 어떠한 제안? 매번 봄 응용 프로그램 컨텍스트가로드되는 이유
내 코드
은 다음과 같습니다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>
자바 클래스 :
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
}
}
그리고 테스트 자바 클래스는 다음과 같습니다
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));
}
}
}
매번 스프링 컨텍스트를로드하는 getJdbctemplate()을 호출하고 있습니다. 무엇을하고 싶니? – jervine10
이제 내 코드 문제를 이해합니다. getJdbctemplate() 메서드를 호출 할 때마다 응용 프로그램 컨텍스트를로드 할 때이를 수행 할 수 있습니까? – jasim