spring 多数据库 操作

简介: spring  多数据库 操作       TraceWebSupport.xml classpath:db_web.properties ...

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捐助),没钱捧个人场,谢谢各位。



 
 
 谢谢您的赞助,我会做的更好!

 

 

 

 

 

 

目录
相关文章
|
JavaScript 关系型数据库 MySQL
❤Nodejs 第六章(操作本地数据库前置知识优化)
【4月更文挑战第6天】本文介绍了Node.js操作本地数据库的前置配置和优化,包括处理接口跨域的CORS中间件,以及解析请求数据的body-parser、cookie-parser和multer。还讲解了与MySQL数据库交互的两种方式:`createPool`(适用于高并发,通过连接池管理连接)和`createConnection`(适用于低负载)。
17 0
|
1月前
|
SQL 数据库连接 数据库
你不知道ADo.Net中操作数据库的步骤【超详细整理】
你不知道ADo.Net中操作数据库的步骤【超详细整理】
16 0
|
2月前
|
安全 Java 数据库
后端进阶之路——万字总结Spring Security与数据库集成实践(五)
后端进阶之路——万字总结Spring Security与数据库集成实践(五)
|
3月前
|
SQL 关系型数据库 MySQL
MySQL| 数据库的管理和操作【操作数据库和操作表】【附练习】
MySQL | 数据库的基本操作和表的基本操作【附练习】
|
3月前
|
SQL 关系型数据库 MySQL
MySQL | 数据库的管理和操作【表的增删改查】(一)
MySQL | 数据库的管理和操作【表的增删改查】
|
3月前
|
SQL 关系型数据库 MySQL
MySQL | 数据库的管理和操作【表的增删改查】(二)
MySQL | 数据库的管理和操作【表的增删改查】(二)
|
15天前
|
存储 关系型数据库 MySQL
【mybatis-plus】Springboot+AOP+自定义注解实现多数据源操作(数据源信息存在数据库)
【mybatis-plus】Springboot+AOP+自定义注解实现多数据源操作(数据源信息存在数据库)
|
1月前
|
缓存 NoSQL 数据库
[Redis]——数据一致性,先操作数据库,还是先更新缓存?
[Redis]——数据一致性,先操作数据库,还是先更新缓存?
|
1月前
|
SQL 存储 关系型数据库
【mysql】—— 数据库的操作
【mysql】—— 数据库的操作
【mysql】—— 数据库的操作
|
2月前
|
存储 SQL 数据库连接
连接并操作数据库:Python 数据库案例
数据库是一种用于存储和管理数据的工具,它以一种有组织的方式将数据存储在文件或内存中,以便于检索和处理。数据库系统通常使用 SQL(Structured Query Language)语言来进行数据的操作,包括数据的插入、查询、更新和删除等。