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捐助),没钱捧个人场,谢谢各位。
谢谢您的赞助,我会做的更好!
相关推荐
Spring.Net+Nhibernate多数据库操作示例代码
spring事务与数据库操作
该demo包含spring boot 下使用jpa规范操作数据库的例子,包含同个项目多个数据库实例,存储过程操作,原始sql操作返回结果转换成DTO等例子。
本源码以简单易懂的方式,讲述了spring动态切换数据库的原理和做法,内附有指导文件,按照说明操作,即可实现spring的数据库动态切换
spring boot mysql数据库操作
基于Spring简化数据库操作的研究及应用,李涛,徐宁,随着Internet的发展,基于B/S架构开发的J2EE项目需要频繁的进行数据库操作。然而java提供的JDBC API过于复杂,代码过于冗余。本文主要是针
spring对多个数据库进行事务管理,配置分布式事务
struts2+spring+hibernate(实现XML和Annotation两种方式操作数据库) 项目描述: 框架及版本:struts2 + spring3.0 + hibernate3.3 所实现的功能说明 把两个对象存入到数据库中 1、一种方法采用XML文件映射的方式 2...
NULL 博文链接:https://chenjingbo.iteye.com/blog/916577
struts2 spring jpa操作数据库 级联数据 hibernate
Hibernate是一款优秀的ORM框架,能够连接并操作数据库,包括保存和修改数据。Spring MVC是Java的web框架,能够将Hibernate集成进去,完成数据的CRUD。Hibernate使用方便,配置响应的XML文件即可。由于spring3.x,基于...
Spring Data JPA数据库操作教程.zip
一、适合人群 1、具备一定Java编程基础,初级开发者 2、对springboot,mybatis,mysql有基本认识 3、对spring aop认识模糊的,不清楚如何实现Java 自定义注解的 ...4、spring boot,mybatis,druid,spring aop的使用
项目中我们经常会遇到多数据源的问题,尤其是数据同步或定时任务等项目更是如此。多数据源让人最头痛的,不是配置多个数据源,而是如何能灵活动态的切换数据源。 此项目就是为了解决这个问题。
本项目为spring操作MySQL数据库的一个小例子,实现了数据库增删查改,外部jar包和数据库齐全,下载导入到eclipse运行即可
spring-data-mongodb增强工具包,简化 CRUD 操作,提供类mybatis plus的数据库操作。传统关系型数据库及围绕它们构建的orm在项目开发中有很多难用的痛点,而mongodb这种文档性数据库的出现,完美的解决了sql数据库在...
使用spring jdbc template简化jdbc数据库操作实例代码,简化操作,包括详细测试例子。
spring famework 操作数据库使用jdbc示例代码
本篇文章主要介绍了Spring Boot Mysql 数据库操作示例,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
主要介绍了Spring boot 数据库连接断线重连问题,需要的朋友可以参考下