结合spring配置JNDI的测试用例
2015-11-09 18:04
375 查看
最近用到JNDI,并且在spring中进行配置,可是由于各种条件限制,需要在测试各种资料后发现想要在spring中实现并不太容易,参考网上的资料总结出两种方式,为自己做备份,也为他人做参考,少走弯路:
1. 通过InitialContext,context.lookup("test")实现,缺点不能代码全部自己写,几乎不能用spring
2. 与spring完美结合,但是配置文件DataSource和spring相关配置分开写,配置文件参考文章最后
附:testJNDI.xml
<bean id="propertyConfigurer"
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<list>
<value>classpath:ds.properties</value>
</list>
</property>
</bean>
<bean id="abstractDS" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
<property name="driverClassName">
<value>oracle.jdbc.OracleDriver</value>
</property>
<property name="maxActive"><value>${maxActive}</value></property>
<property name="maxIdle"><value>${maxIdle}</value></property>
<property name="minIdle"><value>${minIdle}</value></property>
<property name="initialSize"><value>${initialSize}</value></property>
</bean>
<bean id="testDs1" parent="abstractDS">
<property name="url">
<value>your database url</value>
</property>
<property name="username">
<value>test</value>
</property>
<property name="password">
<value>test</value>
</property>
</bean>
testSpring.xml
<bean id="dao" class="com.db.dao.impl.BaseDAOImpl">
<property name="nameJdbcTemplate">
<ref bean="nameJdbcTemplate"/>
</property>
</bean>
<bean id="nameJdbcTemplate" class="org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate">
<constructor-arg>
<ref bean="testDataSource" />
</constructor-arg>
</bean>
<bean id="testDataSource" class="org.springframework.jndi.JndiObjectFactoryBean">
<property name="jndiName">
<value>test1</value> <!-- 代码中对应的字符 builder.bind("test1", testDs1);-->
</property>
</bean>
1. 通过InitialContext,context.lookup("test")实现,缺点不能代码全部自己写,几乎不能用spring
String[] paths = {"conf/testJNDI.xml" };//testJNDI.xml参考文章后边
ClassPathXmlApplicationContext app =new ClassPathXmlApplicationContext(paths);
DataSource testDs =(DataSource) app.getBean("testDs1");
SimpleNamingContextBuilder builder =new SimpleNamingContextBuilder();
builder.bind("test1", testDs);
try {
builder.activate();
} catch (NamingException e) {
e.printStackTrace();
}
JSONObject reqJson = new JSONObject();
Long startDate = System.currentTimeMillis();
Context context = new InitialContext();
DataSource ds = (DataSource)context.lookup("test");
NamedParameterJdbcTemplate namedParameterJdbcTemplate = new NamedParameterJdbcTemplate(ds);
BaseDAOImpl baseDao = new BaseDAOImpl();
baseDao.setNameJdbcTemplate(namedParameterJdbcTemplate);
List<Map<String, Object>> tradeList = baseDao.querySql("select * from trade");
2. 与spring完美结合,但是配置文件DataSource和spring相关配置分开写,配置文件参考文章最后
String[] paths = {"conf/testJNDI.xml" };
ClassPathXmlApplicationContext app = new ClassPathXmlApplicationContext(paths);
DataSource testDs1 =(DataSource) app.getBean("testDs1");
SimpleNamingContextBuilder builder = new SimpleNamingContextBuilder();
builder.bind("test1", testDs1);
try {
builder.activate();
} catch (NamingException e) {
e.printStackTrace();
}
String[] servicePath = {"conf/testSpring.xml" };
ClassPathXmlApplicationContext testContext = new ClassPathXmlApplicationContext(servicePath);
Long startDate = System.currentTimeMillis();
TradeInfo tradeInfo = (TradeInfo)testContext.getBean("tradeInfo");
try {
Map map = tradeInfo.getMainTradeInfo("1115031212567112");
System.out.println(System.currentTimeMillis() - startDate);
logger.debug("================>"+map.toString());
} catch (Exception e) {
e.printStackTrace();
}
附:testJNDI.xml
<bean id="propertyConfigurer"
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<list>
<value>classpath:ds.properties</value>
</list>
</property>
</bean>
<bean id="abstractDS" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
<property name="driverClassName">
<value>oracle.jdbc.OracleDriver</value>
</property>
<property name="maxActive"><value>${maxActive}</value></property>
<property name="maxIdle"><value>${maxIdle}</value></property>
<property name="minIdle"><value>${minIdle}</value></property>
<property name="initialSize"><value>${initialSize}</value></property>
</bean>
<bean id="testDs1" parent="abstractDS">
<property name="url">
<value>your database url</value>
</property>
<property name="username">
<value>test</value>
</property>
<property name="password">
<value>test</value>
</property>
</bean>
testSpring.xml
<bean id="dao" class="com.db.dao.impl.BaseDAOImpl">
<property name="nameJdbcTemplate">
<ref bean="nameJdbcTemplate"/>
</property>
</bean>
<bean id="nameJdbcTemplate" class="org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate">
<constructor-arg>
<ref bean="testDataSource" />
</constructor-arg>
</bean>
<bean id="testDataSource" class="org.springframework.jndi.JndiObjectFactoryBean">
<property name="jndiName">
<value>test1</value> <!-- 代码中对应的字符 builder.bind("test1", testDs1);-->
</property>
</bean>
相关文章推荐
- 基于Spring测试的jndi配置问题
- Junit+spring创建JNDI运行测试用例
- spring、junit测试用例配置与写法
- Junit+spring创建JNDI运行测试用例
- junit测试用例加载spring配置文件
- Spring配置JNDI及通过JNDI获取DataSource
- spring hsqldb 测试潜入数据库 配置程序
- Spring结合Hibernate声明式事务配置
- spring cloud在编写测试用例时报错:Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: N
- 基于spring-test/junit4的测试用例
- Spring 配置JNDI连接池
- Spring整合JUnit4测试,使用注解引入一个或多个配置文件
- Spring结合c3p0配置
- fastJson与springmvc结合,在spring-mv be9e c.xml中的配置,解决415报错
- Junit4 如何实现并发测试用例,及 Selenium Grid2 与 Junit4 结合进行并发测试。
- Atitit.mybatis的测试 以及spring与mybatis在本项目中的集成配置说明
- linux下junit测试用例编译与运行配置
- Restlet实战(十一)结合源代码修改Restlet-Spring配置文件
- Spring结合java Quartz配置实例代码
- spring结合Junit测试使用小结