首页 > 代码库 > spring对mytatis的xml配置以及quartz

spring对mytatis的xml配置以及quartz

前言:使用spring+mybatis+mysql的框架构建Java代码已经有段时间了,但是对于applicationContext.xml文件中的配置项并没有很明确的概念性知识,经过阅读《spring实战3》,结合项目中运用的知识,对本文件的配置做一些说明,同时,非常迫切的希望有这方面经验的朋友对datasource的配置项提出一些指导性的建议。

Java项目

applicationContext.xml配置项中包含了 引入jdbc配置文件、创建jdbc数据源、quartz等,见以下内容

<?xml version="1.0" encoding="UTF-8"?>

<beans
	xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns:aop="http://www.springframework.org/schema/aop"
	xmlns:tx="http://www.springframework.org/schema/tx"
	xmlns:jdbc="http://www.springframework.org/schema/jdbc"
	xmlns:context="http://www.springframework.org/schema/context"
	xmlns:task="http://www.springframework.org/schema/task"
	xsi:schemaLocation="
     http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
     http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
     http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.0.xsd
     http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
     http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
     http://www.springframework.org/schema/task  
     http://www.springframework.org/schema/task/spring-task-3.1.xsd ">

	<!-- 引入jdbc配置文件 -->
	<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
        <property name="locations">
            <value>conf/jdbc.properties</value>
        </property>
    </bean>
	
	<!--创建jdbc数据源 -->
	<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
		<property name="driverClassName" value=http://www.mamicode.com/"${driver}">>上面我把注意点都通过注释写了出来,但是对于jdbc连接这块的参数值,并没有很确定值,下面是定时quartzConfig.xml任务的配置,见以下内容
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:tx="http://www.springframework.org/schema/tx" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns:context="http://www.springframework.org/schema/context"
	xmlns:aop="http://www.springframework.org/schema/aop" xmlns:p="http://www.springframework.org/schema/p"
	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/context 
       http://www.springframework.org/schema/context/spring-context.xsd
       http://www.springframework.org/schema/aop
       http://www.springframework.org/schema/aop/spring-aop.xsd">
    <!-- 定时任务Quartz配置 -->
 <!-- 开盘 -->
    <bean id="open" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
    	<property name="targetObject">
    	    <ref bean="systemService"/>
    	</property>
    	<property name="targetMethod"> 
    	    <value>open</value>
    	</property>
    	<property name="concurrent">
            <value>false</value>
        </property>
    </bean>
    
    <bean id="openTime" class="org.springframework.scheduling.quartz.CronTriggerBean">
        <property name="jobDetail">
           <ref bean="open"/>
        </property>
        <property name="cronExpression">
             <value>0 0 19 * * ?</value>
        </property>
    </bean>
 <bean id="startQuertz" lazy-init="false" autowire="no" class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
        <property name="triggers">
            <list>
                <ref bean="openTime"/>
         </list>
        </property>
    </bean>
</beans>
以上内容我就不加注释了,比较容易理解,然后是对应Java类执行方法
	// 开盘
	public void open() {
		logger.info("--------------------开盘------------------------");
	}

Java web项目

只有一个点不相同,我只把他列出来
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
	xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx"
	xsi:schemaLocation="  
    http://www.springframework.org/schema/beans   
    http://www.springframework.org/schema/beans/spring-beans-3.0.xsd  
    http://www.springframework.org/schema/tx   
    http://www.springframework.org/schema/tx/spring-tx-3.0.xsd   
    http://www.springframework.org/schema/context   
    http://www.springframework.org/schema/context/spring-context-3.0.xsd">

	<!-- 引入jdbc配置文件 -->
	<context:property-placeholder location="classpath:jdbc.properties" />
导入的jdbc文件方式不一样,这种是spring最基本的外部文件导入方式,通过在类路径的src根目录找出jdbc配置。

总结:这些配置不是经常需要变动,一个项目只需要配置一次就够了。






spring对mytatis的xml配置以及quartz