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>