`
knight_black_bob
  • 浏览: 842728 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

spring 多数据库 操作

阅读更多

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.Net与NHibernate结合进行多数据库操作,以便在不同的数据库系统间灵活切换,提高应用程序的可移植性和可扩展性。 首先,Spring.Net的核心特性之一是依赖注入,这使得我们能够轻松地...

    spring连接数据库代码

    本文将详细讲解如何使用 Spring 连接数据库,包括创建数据库、配置 Spring、编写数据库操作代码等步骤。 创建数据库 首先,我们需要创建一个数据库。这里,我们使用 MySQL 数据库管理系统和 SQLyog 工具来创建一个...

    spring多数据库

    在Spring框架中,多数据库的支持是一项重要的功能,它允许开发者在一个应用中同时连接并操作多个数据库。这在处理跨系统数据交互、数据隔离或者不同数据源的场景下非常有用。"spring多数据库"项目就是针对这种需求而...

    spring boot 常用数据库操作例子

    本示例主要关注Spring Boot与JPA(Java Persistence API)的集成,用于数据库操作。JPA是Java EE平台的一部分,提供了一种标准的方式来管理和持久化应用程序中的对象到关系数据库。 首先,让我们详细了解一下Spring...

    spring和数据库层的结合

    在Java领域,Spring框架通过与ORM工具如Hibernate的集成,使得开发者能够在不直接编写SQL语句的情况下实现数据库操作。 **场景示例**:假设我们需要创建一个DAO(Data Access Object)接口,该接口接收一个模糊匹配...

    spring连接数据库实例

    接下来,我们讨论Spring如何进行数据库操作。Spring JDBC模块提供了模板类`JdbcTemplate`和`NamedParameterJdbcTemplate`,它们简化了编写SQL语句和处理结果集的过程。例如,使用`JdbcTemplate`执行一个简单的查询:...

    spring 数据库动态切换

    在Spring框架中,数据库动态切换是一项关键功能,它允许应用程序根据特定条件或需求在多个数据库之间灵活地切换。这在处理分布式系统、多租户应用或者数据隔离等场景时非常有用。下面我们将深入探讨这个主题。 首先...

    Spring-test做数据库操作的单元测试2-跨库访问

    综上所述,"Spring-test做数据库操作的单元测试2-跨库访问"这个主题涵盖了许多关键概念,包括Spring-test的运行机制、多数据源配置、事务管理以及测试的准备与清理。这些知识点对于开发涉及跨库操作的应用程序,确保...

    Springcloud 多数库 多数据源整合,查询动态切换数据库

    可以通过Spring Security来控制用户的数据库访问权限,防止未经授权的数据库操作。 9. **测试与监控**:在开发和运行阶段,对多数据源的测试和监控同样重要。使用像Spring Boot Actuator这样的工具,可以实时监控...

    跨多个数据库操作,同时连接多个的数据库,同时操作

    在复杂的企业级系统中,往往需要连接并操作多个数据库,以满足不同业务场景的需求。这通常涉及到跨数据库操作和分布式事务处理,确保数据的一致性和完整性。以下是一些关于这个主题的重要知识点: 1. **JTA(Java ...

    初学SpringCloud连接Oracle数据库

    SpringCloud作为一个微服务框架,提供了丰富的服务治理功能,而数据库作为数据存储的核心,与SpringBoot的集成使得我们可以轻松处理数据操作。本篇文章将详细讲解如何在SpringCloud项目中配置Oracle数据库,并从...

    spring+hibernate和spring+myBatis实现连接多个数据库,同时操作的项目

    这个项目提供了一个模板,可以帮助开发者快速搭建一个多数据源环境,无论是使用Hibernate的ORM方式还是MyBatis的SQL映射方式,都能灵活地进行数据库操作。通过这个项目,开发者可以学习到如何在实际应用中管理多数据...

    spring框架、数据库池连接等之间连接的jar包.rar

    在Spring框架中,数据库连接池是实现高效数据库操作的关键组件,它允许应用程序重复使用已建立的数据库连接,而不是每次都创建新的连接,从而提高了性能和资源利用率。 在Java中,常见的数据库连接池工具有C3P0、...

    spring操作MySQL数据库.zip

    综上所述,这个压缩包提供了一个完整的Spring与MySQL结合的示例,涵盖了Spring的数据库操作、MVC模式、事务管理等多个核心功能,是学习和理解Spring整合数据库的绝佳实践。通过这个项目,开发者可以深入理解Spring...

    spring Batch实现数据库大数据量读写

    本篇文章将深入探讨如何利用Spring Batch实现数据库大数据量的读写操作。 ### 1. Spring Batch 概述 Spring Batch 提供了一套完整的解决方案,涵盖了批量处理的生命周期管理,包括初始化、执行、监控和重试等环节...

    spring jpa操作数据库 级联数据 hibernate

    在IT行业中,数据库操作是应用程序的核心部分,Spring JPA(Java Persistence API)是Spring框架提供的一种简化ORM(对象关系映射)操作的方式。本主题主要围绕"spring jpa操作数据库 级联数据 hibernate"展开,探讨...

    在应用层透过spring解决数据库读写分离

    在应用层透过Spring解决数据库读写分离 数据库读写分离是指将数据库的读取和写入操作分开处理,以提高数据库的性能和可用性。在应用层解决数据库读写分离可以通过Spring框架来实现,本文将介绍如何使用Spring解决...

    springaop多数据库读写分离

    2. **定义切面**:创建一个Spring AOP切面,这个切面将包含一个或多个通知(advice),用于拦截特定的数据库操作。例如,我们可以创建一个`@Before`通知,检查方法的注解或方法名,以判断是读操作还是写操作。 3. *...

    Spring Framework 开发参考手册,数据库系统的JDBC驱动程序

    总之,Spring Framework的JDBC支持为数据库操作提供了强大而灵活的工具,通过其简洁的API和丰富的功能,大大提升了开发效率和代码质量。理解和掌握这些知识点,对于任何使用Spring进行数据库操作的开发者来说都至关...

Global site tag (gtag.js) - Google Analytics