spring 多数据库 操作
TraceWebSupport.xml
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.spri ngframework.org/dtd/spring-beans.dtd"> <beans> <bean id="config" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> <property name="location"> <value>classpath:db_web.properties</value> </property> </bean> <bean id="traceWebDataSource" class="org.apache.tomcat.dbcp.dbcp.BasicDataSource" destroy-method="close"> <property name="driverClassName" value="${driverClassName}" /> <property name="url" value="${url}" /> <property name="username" value="${username}" /> <property name="password" value="${password}" /> <property name="initialSize" value="${initialSize}" /> <property name="maxActive" value="${maxActive}" /> <property name="maxIdle" value="${maxIdle}" /> <property name="maxWait" value="${maxWait}" /> <property name="validationQuery" value="${validationQuery}" /> <property name="testWhileIdle" value="${testWhileIdle}" /> <property name="timeBetweenEvictionRunsMillis" value="${timeBetweenEvictionRunsMillis}" /> <property name="minEvictableIdleTimeMillis" value="${minEvictableIdleTimeMillis}" /> <property name="testOnBorrow" value="${testOnBorrow}" /> </bean> <bean id="archiveBlobDao" class="com.sq.dao.impl.ArchiveBlobDaoImpl" autowire="byName"> <property name="dataSource" ref="traceWebDataSource" /> </bean> <bean id="archiveBlobService" class="com.sq.service.impl.ArchiveBlobServiceImpl" autowire="byName"> <property name="archiveBlobDao" ref="archiveBlobDao" /> </bean> </beans>
BeanConfig.xml
<?xml version="1.0" encoding="UTF-8"?> <!-- <!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd"> --> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:jee="http://www.springframework.org/schema/jee" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:p="http://www.springframework.org/schema/p" xmlns:util="http://www.springframework.org/schema/util" xmlns:tool="http://www.springframework.org/schema/tool" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd http://www.springframework.org/schema/tool http://www.springframework.org/schema/tool/spring-tool.xsd" > <bean id="config" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> <property name="location"> <value>classpath:db.properties</value> </property> </bean> <bean id="dataSource" class="org.apache.tomcat.dbcp.dbcp.BasicDataSource" destroy-method="close"> <property name="driverClassName" value="${driverClassName}" /> <property name="url" value="${url}" /> <property name="username" value="${username}" /> <property name="password" value="${password}" /> <property name="initialSize" value="${initialSize}" /> <property name="maxActive" value="${maxActive}" /> <property name="maxIdle" value="${maxIdle}" /> <property name="maxWait" value="${maxWait}" /> <property name="validationQuery" value="${validationQuery}" /> <property name="testWhileIdle" value="${testWhileIdle}" /> <property name="timeBetweenEvictionRunsMillis" value="${timeBetweenEvictionRunsMillis}" /> <property name="minEvictableIdleTimeMillis" value="${minEvictableIdleTimeMillis}" /> <property name="testOnBorrow" value="${testOnBorrow}" /> </bean> <bean id="userAreaRelationDao" class="com.sq.dao.impl.UserAreaRelationDaoImpl" autowire="byName"> </bean> <bean id="userDao" class="com.sq.dao.impl.UserDaoImpl" autowire="byName"> </bean> <bean id="userService" class="com.sq.service.impl.UserServiceImpl" autowire="byName"> <property name="userDao" ref="userDao"/> </bean> <bean id="appDao" class="com.sq.dao.impl.ApplicationDaoImpl" autowire="byName"> </bean> <bean id="appService" class="com.sq.service.impl.ApplicationServiceImpl" autowire="byName"> <property name="appDao" ref="appDao"/> </bean> <!-- more bean definitions go here --> <!-- Quartz 定时任务 start...--> <bean id="callUser" class="org.springframework.scheduling.quartz.JobDetailBean"> <property name="jobClass" value="com.sq.job.StaticsCallJob"/> </bean> <bean id="dotimetrigger" class="org.springframework.scheduling.quartz.CronTriggerBean"> <property name="jobDetail" ref="callUser"/> <property name="cronExpression" value="0 50 14 * * ?"/> </bean> <bean id="startQuartz" lazy-init="false" autowire="no" class="org.springframework.scheduling.quartz.SchedulerFactoryBean"> <property name="triggers"> <list> <ref bean="dotimetrigger"/> </list> </property> </bean> <!-- Quartz 定时任务 end...--> </beans>
ContextInitiateListener
package com.sq.listeners; import javax.servlet.ServletContextEvent; import javax.servlet.ServletContextListener; import org.apache.log4j.Logger; import org.quartz.Scheduler; import org.quartz.SchedulerException; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import com.sq.utilities.Constants; /** * 监听器:服务器加载项目时,启动上下文初始化监听,从而获取系统上下文,并获取quartz定时任务 * */ public class ContextInitiateListener implements ServletContextListener{ public static Logger logger = Logger.getLogger(ContextInitiateListener.class); /* * 取消定时任务 * */ @Override public void contextDestroyed(ServletContextEvent arg0) { ApplicationContext serviceContext = (ApplicationContext) arg0.getServletContext().getAttribute("serviceCtx"); Scheduler quartzSch = (Scheduler) serviceContext.getBean("startQuartz"); try { quartzSch.deleteJob("callUser", "DEFAULT"); quartzSch.shutdown(); } catch (SchedulerException e) { e.printStackTrace(); } } /* * 开始定时任务,完成相应系统参数的初始化工作 * */ @Override public void contextInitialized(ServletContextEvent paramContextEvent) { Constants.TRACEWEBSERVICECONTEXT = new ClassPathXmlApplicationContext(Constants.BEAN_XML_SUPPORT); paramContextEvent.getServletContext().setAttribute(Constants.BEAN_XML_SUPPORT, Constants.TRACEWEBSERVICECONTEXT); Constants.SERVICECONTEXT = new ClassPathXmlApplicationContext(Constants.BEAN_XML); paramContextEvent.getServletContext().setAttribute("serviceCtx", Constants.SERVICECONTEXT); } }
使用的时候:
UserService userService = (UserService) Constants.SERVICECONTEXT.getBean("userService"); ApplicationService appService = (ApplicationService) Constants.SERVICECONTEXT.getBean("appService"); ArchiveBlobService archiveBlobService = (ArchiveBlobService) Constants.TRACEWEBSERVICECONTEXT.getBean("archiveBlobService");
捐助开发者
在兴趣的驱动下,写一个免费
的东西,有欣喜,也还有汗水,希望你喜欢我的作品,同时也能支持一下。 当然,有钱捧个钱场(右上角的爱心标志,支持支付宝和PayPal捐助),没钱捧个人场,谢谢各位。
谢谢您的赞助,我会做的更好!