Spring整合C3P0和quartz
项目截图
annomvc-servlet.xml配置
springMvc注解模式下,对项目中的包进行自动扫描扫描
<?xmlversion="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" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<!-- 1.对包中的所有类进行扫描,完成Bean的创建和自动依赖注入 --> <context:component-scanbase-package="com.cloud.ctrl"/> <!-- 映射文件列表 --> <context:component-scanbase-package="com.cloud.impl"> <context:include-filtertype="annotation"expression="org.springframework.stereotype.Service"/> </context:component-scan> <!-- 2.使用springMVC的注解,完成请求和注解的POJO映射--> <beanclass="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter"/> </beans> |
applicationContext.xml配置
配置hibernate工程,C3P0连接池,和数据库的链接信息。
<?xmlversion="1.0"encoding="UTF-8"?> <beansxmlns="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:jee="http://www.springframework.org/schema/jee"xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-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/jee http://www.springframework.org/schema/jee/spring-jee-3.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd">
<!-- 配置Service拉取数据 --> <!-- <bean id="getMenuData" class="com.cloud.impl.GetMenuData"> <property name="jdbcTemplate"ref="jdbcTemplate"/> <property name="hibernateTemplate"ref="hibernateTemplate"/> </bean> -->
<!-- 设置C3P0连接池的常用属性 --> <beanid="c3p0DataSource"abstract="true"> <!--初始化时获取的连接数,取值应在minPoolSize与maxPoolSize之间。Default: 3 --> <propertyname="initialPoolSize"value="10"/> <!--连接池中保留的最小连接数。--> <propertyname="minPoolSize"value="3"/> <!--连接池中保留的最大连接数。Default: 15 --> <propertyname="maxPoolSize"value="150"/> <!--最大空闲时间,60秒内未使用则连接被丢弃。若为0则永不丢弃。Default: 0 --> <propertyname="maxIdleTime"value="60"/> <!--当连接池中的连接耗尽的时候c3p0一次同时获取的连接数。Default: 3 --> <propertyname="acquireIncrement"value="5"/> <!--每60秒检查所有连接池中的空闲连接。Default: 0 --> <propertyname="idleConnectionTestPeriod"value="60"/> </bean> <!-- 配置C3P0连接池 --> <beanid="dataSource"class="com.mchange.v2.c3p0.ComboPooledDataSource"destroy-method="close"parent="c3p0DataSource"> <propertyname="driverClass"value="oracle.jdbc.driver.OracleDriver"/> <propertyname="jdbcUrl"value="jdbc:oracle:thin:user/password@192.192.192.192:192:F1"/> <propertyname="user"value="app"/> <propertyname="password"value="app"/> </bean>
<!-- HIBERNATE相关配置 --> <beanid="jdbcTemplate"class="org.springframework.jdbc.core.JdbcTemplate" abstract="false"lazy-init="false"autowire="default"> <propertyname="dataSource"> <refbean="dataSource"/> </property> </bean> <beanid="hibernateTemplate"class="org.springframework.orm.hibernate3.HibernateTemplate"> <propertyname="sessionFactory"> <refbean="sessionFactory"/> </property> </bean> <!-- 配置HIBERNATE 工厂 --> <beanid="sessionFactory"class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean"> <propertyname="dataSource"ref="dataSource"/> <propertyname="hibernateProperties"> <props> <prop key="hibernate.dialect">org.hibernate.dialect.Oracle10gDialect</prop> <prop key="hibernate.show_sql">true</prop> <prop key="hibernate.format_sql">true</prop> <prop key="hibernate.hbm2ddl.auto">none</prop> <prop key="show_sql">true</prop> </props> </property> <propertyname="packagesToScan"> <list><value>com.cloud.pojo</value></list> </property> </bean> </beans> |
quartz.xml配置
定时器的配置,在项目启动的时候,执行特定方法,多用来执行监控报表的功能。
<?xmlversion="1.0"encoding="UTF-8"?> <beansxmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd"> <!--配置一个定时器 --> <!-- 定时器:获取数据第一步 --> <beanid="getDataQuart"class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean"> <!--配置获取数据的service --> <propertyname="targetObject"ref="getMenuDataService"/> <!--配置执行的method --> <propertyname="targetMethod"value="getMenuList"/> <!--设置作业不并发调度 --> <propertyname="concurrent"value="false"/> </bean> <!-- 定时器:设置定时启动时间 --> <beanid="getDataTrigger"class="org.springframework.scheduling.quartz.CronTriggerBean"> <propertyname="jobDetail"ref="getDataQuart"/> <!--每天凌晨开始,每30分钟拉取一次数据 --> <propertyname="cronExpression"> <value>0 0-59/1 0-23 * * ?</value> </property> </bean> <!-- 定时器:启动定时器 --> <beanclass="org.springframework.scheduling.quartz.SchedulerFactoryBean"> <propertyname="triggers"> <list> <!-- 这里可以设置多个要启动的定时器 --> <ref bean="getDataTrigger"/> </list> </property> </bean> </beans> |
web.xml配置
对配置文件的加载,项目的启动核心配置文件。
<?xmlversion="1.0"encoding="UTF-8"?> <web-appversion="3.0" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"> <display-name></display-name>
<!-- 配置Spring文件加载 --> <context-param> <param-name>contextConfigLocation</param-name> <param-value>/WEB-INF/annomvc-servlet.xml,classpath:applicationContext.xml,classpath:quartz.xml</param-value> </context-param> <!-- 配置监听 --> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener>
<!-- 配置springMvc的注解开发 --> <servlet> <servlet-name>annomvc</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet </servlet-class> <load-on-startup>2</load-on-startup> <!-- <init-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:annomvc.xml</param-value> </init-param> --> </servlet> <servlet-mapping> <servlet-name>annomvc</servlet-name> <url-pattern>*.do</url-pattern> </servlet-mapping>
<welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> </web-app> |
数据库连接封装
package com.cloud.common; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.jdbc.core.JdbcTemplate; import org.springframework.orm.hibernate3.HibernateTemplate; publicclass BaseDaoSupport { @Autowired @Qualifier("hibernateTemplate") private HibernateTemplatehibernateTemplate; @Autowired @Qualifier("jdbcTemplate") private JdbcTemplatejdbcTemplate; public HibernateTemplate getHibernateTemplate() { returnhibernateTemplate; } publicvoid setHibernateTemplate(HibernateTemplate hibernateTemplate) { this.hibernateTemplate = hibernateTemplate; } public JdbcTemplate getJdbcTemplate() { returnjdbcTemplate; } publicvoid setJdbcTemplate(JdbcTemplate jdbcTemplate) { this.jdbcTemplate = jdbcTemplate; } } |
MVC模式开发
控制层代码
package com.cloud.ctrl; import java.util.List; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import com.cloud.inte.GetDataInte; @Controller publicclass GetDataCtrl { @Autowired @Qualifier("getMenuDataService") private GetDataIntegetMenuDataService; @SuppressWarnings("rawtypes") @RequestMapping("/getData/getReport.do") publicvoid getReport(HttpServletRequest request,HttpServletResponse response)throws Exception{ System.out.println("获取报表数据:ctrl层"); List list = getMenuDataService.getReportList(); System.out.println("--"+list); } } |
接口代码
package com.cloud.inte; import java.util.List; publicinterface GetDataInte { @SuppressWarnings("rawtypes") public List getReportList()throws Exception; } |
实现层代码
package com.cloud.impl; import java.util.Date; import java.util.List; import org.springframework.stereotype.Service; import com.cloud.common.BaseDaoSupport; import com.cloud.inte.GetDataInte; @Service("getMenuDataService") publicclass GetMenuData extends BaseDaoSupportimplements GetDataInte{ /** *项目启动时,测试定时器的方法 */ @SuppressWarnings("rawtypes") publicvoid getMenuList(){ String sql = "select * from AP_MENU"; List list = this.getJdbcTemplate().queryForList(sql); System.out.println(list+(new Date().toString())); } /** * MVC模式开发测试代码 */ @SuppressWarnings("rawtypes") @Override public List getReportList()throws Exception { String sql = "select ae.take_express_id,ae.receive_province,ae.lan,ae.qu,ae.percent,ae.create_date from AP_TAKE_EXPRESS ae where ae.create_date>sysdate-1"; returnthis.getJdbcTemplate().queryForList(sql); } } |