首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > JAVA > J2SE开发 >

spring的JDBCTemplate与proxool一行使用时该怎样进行配置

2013-01-18 
spring的JDBCTemplate与proxool一起使用时该怎样进行配置只使用spring,连接池proxoolweb.xml,spring-servl

spring的JDBCTemplate与proxool一起使用时该怎样进行配置
只使用spring,连接池proxool
web.xml,spring-servlet.xml,proxool.xml,applicationContext.xml都放在WEB-INF下。
web.xml配置如下:
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="2.5"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee   http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
 <welcome-file-list>
  <welcome-file>index.jsp</welcome-file>    
 </welcome-file-list>       
   
 <!--  DB连接池管理 -->           
 <servlet>
  <servlet-name>ServletConfigurator</servlet-name>
  <servlet-class>
   org.logicalcobwebs.proxool.configuration.ServletConfigurator
  </servlet-class>
  <init-param>
   <param-name>xmlFile</param-name>
   <param-value>WEB-INF/proxool.xml</param-value>
  </init-param>
  <load-on-startup>1</load-on-startup>
 </servlet>
 <!--  连接线程池监控-->
 <servlet>
  <servlet-name>admin</servlet-name>
  <servlet-class>
   org.logicalcobwebs.proxool.admin.servlet.AdminServlet
  </servlet-class>
 </servlet>
 <servlet-mapping>
  <servlet-name>admin</servlet-name>
  <url-pattern>/admin</url-pattern>
 </servlet-mapping>
 
 <!-- Spring MVC配置 --> 
 <servlet>  
     <servlet-name>spring</servlet-name>  
     <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>  
     <init-param>  
         <param-name>contextConfigLocation</param-name>  
         <param-value>/WEB-INF/spring-servlet.xml</param-value>  
     </init-param>  
     <load-on-startup>1</load-on-startup>  
 </servlet>  

 <servlet-mapping>  
     <servlet-name>spring</servlet-name>  
     <url-pattern>*.do</url-pattern>  
 </servlet-mapping>  

 <!-- Spring配置 --> 
 <listener>  
     <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>  
 </listener>  

 <!-- 指定Spring Bean的配置文件所在目录。默认配置在WEB-INF目录下 --> 
<context-param>
     <param-name>contextConfigLocation</param-name>  
     <param-value>/WEB-INF/applicationContext.xml</param-value>  
 </context-param> 
 
</web-app>

spring-servlet.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:p="http://www.springframework.org/schema/p"  
       xmlns:context="http://www.springframework.org/schema/context"  
       xmlns:mvc="http://www.springframework.org/schema/mvc"  
  xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
       http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
       http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
       http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
       http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">
 <context:annotation-config />
       <!-- 把标记了@Controller注解的类转换为bean -->  
      <context:component-scan base-package="com.mvc.controller" /> 
  <mvc:annotation-driven/>    
  
     <!-- 处理了所有的上传操作方法 -->
       <bean id="multipartResolver"  
          class="org.springframework.web.multipart.commons.CommonsMultipartResolver"  
          p:defaultEncoding="utf-8" />  
    
    <!-- 启动Spring MVC的注解功能,完成请求和注解POJO的映射 -->  
    <bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">  
    <property name="messageConverters">  
        <list>  
            <ref bean="mappingJacksonHttpMessageConverter" />  
        </list>  
    </property>  
</bean>  
    <!-- 处理json数据转换 -->    
<bean id="mappingJacksonHttpMessageConverter"  
    class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter" /> 
 </beans>

proxool.xml配置如下:
<?xml version="1.0" encoding="UTF-8"?>
<something-else-entirely>
     <proxool>
    <alias>DBPool</alias>
    <driver-url>jdbc:oracle:thin:@localhost:1521:xxxx</driver-url><!-- @119.84.84.24:1521:cqonline myedu-->
    <driver-class>oracle.jdbc.driver.OracleDriver</driver-class>
    <driver-properties>
      <property name="user" value="xxxx"/>
      <property name="password" value="xxxx"/>
    </driver-properties>
    
    <minimum-connection-count>5</minimum-connection-count>
<maximum-connection-count>20</maximum-connection-count>


<maximum-connection-lifetime>
18000000
</maximum-connection-lifetime>
<maximum-active-time>480000</maximum-active-time>
  </proxool>
</something-else-entirely>

applicationContext.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:p="http://www.springframework.org/schema/p"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
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.5.xsd
       http://www.springframework.org/schema/aop 
       http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
       http://www.springframework.org/schema/tx 
       http://www.springframework.org/schema/tx/spring-tx-2.5.xsd
       http://www.springframework.org/schema/context
       http://www.springframework.org/schema/context/spring-context-2.5.xsd">
    
     <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
     <property name="driverClassName">
             <value>org.logicalcobwebs.proxool.ProxoolDriver</value>
         </property>
         <property name="url">
             <value>proxool.DBPool</value>
         </property>
</bean>
      <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
        <property name="dataSource"><ref bean="dataSource"/></property>
    </bean>
<bean id="CommonDao"
class="com.common.db.CommonDao">
<property name="jdbcTemplate">
<ref bean="jdbcTemplate" />
</property>
</bean>
 </beans> 

CommonDao类如下
package com.common.db;

import org.springframework.jdbc.core.JdbcTemplate;

public class CommonDao {
private JdbcTemplate jdbcTemplate;

public void setJdbcTemplate(JdbcTemplate jdbcTemplate)
{
    this.jdbcTemplate = jdbcTemplate;
}


public int selectWithTemp()
{
    String sql = "select count(*) from SUBMIT_RES";
    int count = jdbcTemplate.queryForInt(sql);       
    return count;
}
        public static void main(String args[]){
System.out.println(new CommonDao().selectWithTemp());
}
}

问题:
1.为什么会报Exception in thread "main" java.lang.NullPointerException
at com.common.db.CommonDao.selectWithTemp(CommonDao.java:17)
at com.common.db.CommonDao.main(CommonDao.java:21)


异常呢?找不到JdbcTemplate类么?
2.JDBCTemplate与proxool连接池一起使用还需要配置什么或写什么类吗? spring JDBCTemplate proxool
[解决办法]
你用new CommonDao()的方式当然是null不是由spring给你创建的自然就没注入jdbcTemplate了
要测试用
ApplicationContext act=new ClassPathXmlApplicationContext("applicationContext.xml");
CommonDao dao=(CommonDao)act.getBean("CommonDao");
System.out.println(dao.selectWithTemp());

热点排行