spring4+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:p="http://www.springframework.org/schema/p"  
    xmlns:aop="http://www.springframework.org/schema/aop"   
    xmlns:context="http://www.springframework.org/schema/context"  
    xmlns:jee="http://www.springframework.org/schema/jee"  
    xmlns:tx="http://www.springframework.org/schema/tx"  
    xmlns:task="http://www.springframework.org/schema/task"
    xsi:schemaLocation="    
        http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd  
        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd  
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd  
        http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-4.0.xsd  
        http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
        http://www.springframework.org/schema/task  
        http://www.springframework.org/schema/task/spring-task-3.1.xsd">
        
    <!-- JNDI方式配置数据源 -->
    <!-- <bean id="dataSource" class="org.springframework.jndi.JndiObjectFactoryBean"> <property name="jndiName" value="${jndiName}"></property> </bean> -->

    <!-- 引入属性文件 -->
    <context:property-placeholder location="classpath:config.properties" ignore-resource-not-found="true" ignore-unresolvable="true"/>

    <bean id="springUtils" class="com.xxdc.core.util.SpringUtils"/>
    
    <!-- 自动扫描dao和service包(自动注入) -->
    <context:component-scan base-package="com.xxdc.*" />
    
    <!-- 配置数据源 -->
     <bean name="dataSource1" class="com.mchange.v2.c3p0.ComboPooledDataSource"  destroy-method="close">
        <property name="driverClass" value="${jdbc.driver}"/>
        <property name="jdbcUrl" value="${jdbc.url}" />
        <property name="user" value="${jdbc.username}" />
        <property name="password" value="${jdbc.password}" />
        <property name="initialPoolSize" value="${connection_pools.initial_pool_size}"/>
        <property name="minPoolSize" value="${connection_pools.min_pool_size}"/>
        <property name="maxPoolSize" value="${connection_pools.max_pool_size}"/>
        <property name="maxIdleTime" value="${connection_pools.max_idle_time}"/>
        <property name="acquireIncrement" value="${connection_pools.acquire_increment}"/>
        <property name="checkoutTimeout" value="${connection_pools.checkout_timeout}"/>
    </bean> 
                                  
    <!-- 配置hibernate session工厂 -->
    <bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
         <!-- 依赖注入数据源,正是上文定义的dataSource -->
        <property name="dataSource" ref="dataSource1" />
        <property name="hibernateProperties">
            <props>
                <prop key="hibernate.hbm2ddl.auto">${hibernate.hbm2ddl.auto}</prop>
                <prop key="hibernate.dialect">${hibernate.dialect}</prop>
                <prop key="hibernate.show_sql">${hibernate.show_sql}</prop>
                <prop key="hibernate.format_sql">${hibernate.format_sql}</prop>
                <prop key="javax.persistence.validation.mode">none</prop>
                <!-- <prop key="hibernate.current_session_context_class">org.springframework.orm.hibernate4.SpringSessionContext</prop> -->
            </props>
        </property>

        <!-- 自动扫描注解方式配置的hibernate类文件    -->
        <property name="packagesToScan" value="com.xxdc.core.entity"/>
        
    </bean>
    
    <bean id="validator" class="org.springframework.validation.beanvalidation.LocalValidatorFactoryBean">
      <property name="providerClass" value="org.hibernate.validator.HibernateValidator"/>
    </bean>
    
    <!--国际化 start -->
    <bean id="messageSource" class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
        <property name="cacheSeconds" value="${message.cache_seconds}" />
        <property name="useCodeAsDefaultMessage" value="true" />
        <property name="fileEncodings" value="utf-8" />
        <property name="basenames">
            <list>
                <value>${message.common_path}</value>
            </list>
        </property>
    </bean>
    
    <bean id="localeResolver" class="org.springframework.web.servlet.i18n.FixedLocaleResolver">
        <property name="defaultLocale" value="${locale}" />
    </bean>
    <!--国际化 end -->
    <!-- 配置事务管理器 指定其作用的sessionFactory把事务交给Spring去处理 -->
    <!-- 配置事务管理器 -->
    <bean name="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager">
        <property name="sessionFactory" ref="sessionFactory"></property>
    </bean>

    <!-- 配置事务的传播特性 -->
    <!-- 拦截器方式配置事务 -->
    <tx:advice id="transactionAdvice" transaction-manager="transactionManager">
        <tx:attributes>
            <tx:method name="add*" />
            <tx:method name="save*" />
            <tx:method name="update*" />
            <tx:method name="modify*" />
            <tx:method name="edit*" />
            <tx:method name="delete*" />
            <tx:method name="remove*" />
            <tx:method name="refresh*" />
            <tx:method name="repair" />
            <tx:method name="execute*"/>
            <!-- 如果当前没有事务,就新建一个事务,如果已经存在一个事务中,加入到这个事务中。这是最常见的选择。 -->
            <!--  propagation="REQUIRED" -->
            <!-- 支持当前事务,如果当前没有事务,就以非事务方式执行。 -->
            <tx:method name="get*" propagation="SUPPORTS" />
            <tx:method name="find*" propagation="SUPPORTS" />
            <tx:method name="load*" propagation="SUPPORTS" />
            <tx:method name="search*" propagation="SUPPORTS" />
            <tx:method name="datagrid*" propagation="SUPPORTS" />
            <tx:method name="*" propagation="SUPPORTS" />
        </tx:attributes>
    </tx:advice>
    
    <!-- 那些类的哪些方法参与事务 -->
    <aop:config>
        <aop:pointcut id="transactionPointcut" expression="execution(* com.xxdc.core.service..*Impl.*(..))" />
        <aop:advisor pointcut-ref="transactionPointcut" advice-ref="transactionAdvice" />
    </aop:config>
    
    
     <!-- 申明JavaMailSenderImpl对象 -->  
    <bean id="mailSender" class="org.springframework.mail.javamail.JavaMailSenderImpl">  
        <property name="defaultEncoding" value="UTF-8" />  
        <property name="port" value="25" />  
        <property name="host" value="smtp.163.com" />  
        <property name="username" value="" />  
        <property name="password" value="" />  
        <property name="javaMailProperties">  
            <props>  
                <!-- 设置认证开关 -->  
                <prop key="mail.smtp.auth">true</prop>  
                <!-- 启动调试开关 -->  
                <prop key="mail.debug">true</prop>  
            </props>  
        </property>  
    </bean>  
    
    <!--异步线程执行器 -->  
    <bean id="taskExecutor" class="org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor">  
        <property name="corePoolSize" value="10" />  
        <property name="maxPoolSize" value="30" />  
    </bean>  
    
    <task:annotation-driven/> 
    
</beans>

调用:

@Component   
public class MyTestServiceImpl  implements IMyTestService {  
      @Scheduled(cron="0/5 * *  * * ? ")   //每5秒执行一次  
      @Override  
      public void myTest(){  
            System.out.println("进入测试");  
      }  
}  
public interface IMyTestService {  
    public void myTest();  
}

jar:quartz-2.2.1.jar

原文地址:https://www.cnblogs.com/jinjixia/p/4815642.html