springマルチデータベース操作
スプリング マルチデータベース操作
Trace WebSupport.xml
BenConfig.xml
Conttext InitiateListener
使う時:
Trace WebSupport.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>
BenConfig.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>
Conttext InitiateListener
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");