结合spring配置JNDI的测试用例

最近用到JNDI,并且在spring中进行配置,可是由于各种条件限制,需要在测试各种资料后发现想要在spring中实现并不太容易,参考网上的资料总结出两种方式,为自己做备份,也为他人做参考,少走弯路:
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>

猜你喜欢

转载自201310154330.iteye.com/blog/2255767
今日推荐