SSM項目搭建之配置文件


1.1  pom文件配置

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.how2java</groupId>
  <artifactId>ssm</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <packaging>war</packaging>
 
  <dependencies>
  
             <!-- https://mvnrepository.com/artifact/org.aspectj/aspectjrt -->
        <dependency>
            <groupId>org.aspectj</groupId>
            <artifactId>aspectjrt</artifactId>
            <version>1.7.4</version>
        </dependency>
         <!-- https://mvnrepository.com/artifact/org.aspectj/aspectjweaver -->
    <dependency>
        <groupId>org.aspectj</groupId>
        <artifactId>aspectjweaver</artifactId>
        <version>1.8.11</version>
    </dependency>
    <!-- https://mvnrepository.com/artifact/org.aopalliance/com.springsource.org.aopalliance -->
    <dependency>
        <groupId>org.aopalliance</groupId>
        <artifactId>com.springsource.org.aopalliance</artifactId>
        <version>1.0.0</version>
    </dependency>
    <!-- https://mvnrepository.com/artifact/org.apache.commons/com.springsource.org.apache.commons.logging -->
    <dependency>
        <groupId>org.apache.commons</groupId>
        <artifactId>com.springsource.org.apache.commons.logging</artifactId>
        <version>1.1.1</version>
    </dependency>
    <!-- https://mvnrepository.com/artifact/org.apache.log4j/com.springsource.org.apache.log4j -->
    <dependency>
        <groupId>org.apache.log4j</groupId>
        <artifactId>com.springsource.org.apache.log4j</artifactId>
        <version>1.2.15</version>
    </dependency>
    <!-- https://mvnrepository.com/artifact/com.alibaba/druid -->
    <dependency>
        <groupId>com.alibaba</groupId>
        <artifactId>druid</artifactId>
        <version>1.0.15</version>
    </dependency>
    <!-- https://mvnrepository.com/artifact/com.alibaba/fastjson -->
    <dependency>
        <groupId>com.alibaba</groupId>
        <artifactId>fastjson</artifactId>
        <version>1.2.7</version>
    </dependency>
    <!-- https://mvnrepository.com/artifact/org.freemarker/freemarker -->
    <dependency>
        <groupId>org.freemarker</groupId>
        <artifactId>freemarker</artifactId>
        <version>2.3.23</version>
    </dependency>
    <!-- https://mvnrepository.com/artifact/org.hamcrest/hamcrest-all -->
    <dependency>
        <groupId>org.hamcrest</groupId>
        <artifactId>hamcrest-all</artifactId>
        <version>1.3</version>
        <scope>test</scope>
    </dependency>
    <!-- https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-annotations -->
    <dependency>
        <groupId>com.fasterxml.jackson.core</groupId>
        <artifactId>jackson-annotations</artifactId>
        <version>2.5.0</version>
    </dependency>
    <!-- https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-core -->
    <dependency>
        <groupId>com.fasterxml.jackson.core</groupId>
        <artifactId>jackson-core</artifactId>
        <version>2.5.4</version>
    </dependency>
    <!-- https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-databind -->
    <dependency>
        <groupId>com.fasterxml.jackson.core</groupId>
        <artifactId>jackson-databind</artifactId>
        <version>2.5.4</version>
    </dependency>
    <!-- https://mvnrepository.com/artifact/com.github.jsqlparser/jsqlparser -->
    <dependency>
        <groupId>com.github.jsqlparser</groupId>
        <artifactId>jsqlparser</artifactId>
        <version>1.0</version>
    </dependency>
    <!-- https://mvnrepository.com/artifact/javax.servlet/jstl -->
    <dependency>
        <groupId>javax.servlet</groupId>
        <artifactId>jstl</artifactId>
        <version>1.2</version>
    </dependency>
    <!-- https://mvnrepository.com/artifact/junit/junit -->
    <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <version>4.4</version>
        <scope>test</scope>
    </dependency>
    <!-- https://mvnrepository.com/artifact/org.mybatis/mybatis -->
    <dependency>
        <groupId>org.mybatis</groupId>
        <artifactId>mybatis</artifactId>
        <version>3.1.1</version>
    </dependency>
    <!-- https://mvnrepository.com/artifact/org.mybatis/mybatis-spring -->
    <dependency>
        <groupId>org.mybatis</groupId>
        <artifactId>mybatis-spring</artifactId>
        <version>1.2.3</version>
    </dependency>
    <!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java -->
    <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
        <version>5.1.6</version>
    </dependency>
    <!-- https://mvnrepository.com/artifact/org.springframework/spring-context-support -->
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-context-support</artifactId>
        <version>4.0.4.RELEASE</version>
    </dependency>

    <!-- https://mvnrepository.com/artifact/com.github.pagehelper/pagehelper -->
    <dependency>
        <groupId>com.github.pagehelper</groupId>
        <artifactId>pagehelper</artifactId>
        <version>5.1.2</version>
    </dependency>
    <!-- https://mvnrepository.com/artifact/javax.servlet/javax.servlet-api -->
    <dependency>
        <groupId>javax.servlet</groupId>
        <artifactId>javax.servlet-api</artifactId>
        <version>3.1.0</version>
        <scope>provided</scope>
    </dependency>
    <!-- https://mvnrepository.com/artifact/org.springframework/spring-aop -->
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-aop</artifactId>
        <version>4.0.4.RELEASE</version>
    </dependency>
    <!-- https://mvnrepository.com/artifact/org.springframework/spring-aspects -->
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-aspects</artifactId>
        <version>4.0.4.RELEASE</version>
    </dependency>
    <!-- https://mvnrepository.com/artifact/org.springframework/spring-beans -->
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-beans</artifactId>
        <version>4.0.4.RELEASE</version>
    </dependency>
    <!-- https://mvnrepository.com/artifact/org.springframework/spring-context -->
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-context</artifactId>
        <version>4.0.4.RELEASE</version>
    </dependency>
    <!-- https://mvnrepository.com/artifact/org.springframework/spring-core -->
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-core</artifactId>
        <version>4.0.4.RELEASE</version>
    </dependency>
    <!-- https://mvnrepository.com/artifact/org.springframework/spring-expression -->
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-expression</artifactId>
        <version>4.0.4.RELEASE</version>
    </dependency>
    <!-- https://mvnrepository.com/artifact/org.springframework/spring-jdbc -->
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-jdbc</artifactId>
        <version>4.0.4.RELEASE</version>
    </dependency>
    <!-- https://mvnrepository.com/artifact/org.springframework/spring-test -->
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-test</artifactId>
        <version>4.0.4.RELEASE</version>
        <scope>test</scope>
    </dependency>
    <!-- https://mvnrepository.com/artifact/org.springframework/spring-tx -->
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-tx</artifactId>
        <version>4.0.4.RELEASE</version>
    </dependency>
    <!-- https://mvnrepository.com/artifact/org.springframework/spring-web -->
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-web</artifactId>
        <version>4.0.4.RELEASE</version>
    </dependency>
    <!-- https://mvnrepository.com/artifact/org.springframework/spring-webmvc -->
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-webmvc</artifactId>
        <version>4.0.4.RELEASE</version>
    </dependency>
 
  </dependencies>
 
  <build>
    <finalName>${project.artifactId}</finalName>
    <plugins>
      <!-- 資源文件拷貝插件 -->
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-resources-plugin</artifactId>
        <version>2.7</version>
        <configuration>
          <encoding>UTF-8</encoding>
        </configuration>
      </plugin>
      <!-- java編譯插件 -->
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-compiler-plugin</artifactId>
        <version>3.2</version>
        <configuration>
          <source>1.8</source>
          <target>1.8</target>
          <encoding>UTF-8</encoding>
        </configuration>
      </plugin>
    </plugins>
<!--     <pluginManagement>
      <plugins>
        配置Tomcat插件
        <plugin>
          <groupId>org.apache.tomcat.maven</groupId>
          <artifactId>tomcat7-maven-plugin</artifactId>
          <version>2.2</version>
        </plugin>
      </plugins>
    </pluginManagement> -->
 
    <resources>
      <resource>
        <directory>src/main/resources</directory>
        <includes>
          <include>**/*.properties</include>
          <include>**/*.xml</include>
          <include>**/*.tld</include>
        </includes>
        <filtering>false</filtering>
      </resource>
      <resource>
        <directory>src/main/java</directory>
        <includes>
          <include>**/*.properties</include>
          <include>**/*.xml</include>
        </includes>
        <filtering>false</filtering>
      </resource>
    </resources>
 
  </build>
 
</project>

1.2 web.xml文件配置

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xmlns="http://java.sun.com/xml/ns/javaee"
         xmlns:web="http://java.sun.com/xml/ns/javaee"
         xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" version="2.5">
     
    <!-- spring的配置文件-->
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath:applicationContext.xml</param-value>
    </context-param>
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
     
    <!-- spring mvc核心:分發servlet -->
    <servlet>
        <servlet-name>mvc-dispatcher</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <!-- spring mvc的配置文件 -->
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>classpath:springMVC.xml</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>mvc-dispatcher</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>
     
</web-app>

1.3  applicationContext.xml 和 springMVC.xml 配置

 1.3.1 applicationContext.xml (位置:ssm/src/main/resources)

<?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:mvc="http://www.springframework.org/schema/mvc"
    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/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd">
     
     <!--1. 通過注解,將Service的生命周期納入Spring的管理  -->
     
     <!-- 表示告訴Spring要用注解的方式進行配置 -->
   <context:annotation-config />
       <!-- 其作用是告訴Spring,Spring能管理bean都放在com.how2java.service這個包下 -->
   <context:component-scan base-package="com.how2java.service" />
   
     <!-- 2. 配置數據源   -->
     
     <!--方式一 : 說明:DriverManagerDataSource建立連接的方式是只要有連接就新建一個connection,故沒有連接池的作用。  -->
    <!-- <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">  
          <property name="driverClassName">  
              <value>com.mysql.jdbc.Driver</value>  
          </property>  
          <property name="url">  
              <value>jdbc:mysql://localhost:3306/how2java?characterEncoding=UTF-8</value>  
          </property>  
          <property name="username">  
              <value>root</value>  
          </property>  
          <property name="password">  
              <value>root</value>  
          </property>     
        </bean> -->
    <!-- 方式二:連接池方式    詳見 https://github.com/alibaba/druid -->
        <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource" init-method="init" destroy-method="close">
        <!-- 基本屬性 url、user、password -->
        <property name="url" value="jdbc:mysql://localhost:3306/how2java?characterEncoding=UTF-8" />
        <property name="username" value="root" />
        <property name="password" value="root" />
        <property name="driverClassName" value="com.mysql.jdbc.Driver" />
 
        <!-- 配置初始化大小、最小、最大 -->
        <property name="initialSize" value="3" />
        <property name="minIdle" value="3" />
        <property name="maxActive" value="20" />
 
        <!-- 配置獲取連接等待超時的時間 -->
        <property name="maxWait" value="60000" />
 
        <!-- 配置間隔多久才進行一次檢測,檢測需要關閉的空閑連接,單位是毫秒 -->
        <property name="timeBetweenEvictionRunsMillis" value="60000" />
 
        <!-- 配置一個連接在池中最小生存的時間,單位是毫秒 -->
        <property name="minEvictableIdleTimeMillis" value="300000" />
 
        <property name="validationQuery" value="SELECT 1" />
        <property name="testWhileIdle" value="true" />
        <property name="testOnBorrow" value="false" />
        <property name="testOnReturn" value="false" />
 
        <!-- 打開PSCache,並且指定每個連接上PSCache的大小 -->
        <property name="poolPreparedStatements" value="true" />
        <property name="maxPoolPreparedStatementPerConnectionSize" value="20" />
    </bean>
     <!-- 3. 掃描存放SQL語句的Category.xml -->
    <bean id="sqlSession" class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="typeAliasesPackage" value="com.how2java.pojo" />
        <property name="dataSource" ref="dataSource"/>
        <property name="mapperLocations" value="classpath:com/how2java/mapper/*.xml"/>
        <!-- PageHelper插件配置 -->
        <property name="plugins">
            <array>
              <bean class="com.github.pagehelper.PageInterceptor">
                <property name="properties">
                  <!--使用下面的方式配置參數,一行配置一個 -->
                  <value> </value>
                </property>
              </bean>
            </array>
         </property>  
    </bean>
    <!-- 4. 掃描Mapper,並將其生命周期納入Spring的管理 -->
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <property name="basePackage" value="com.how2java.mapper"/>
    </bean>
    
    <!-- 5.事務管理配置 -->
    
    <!-- 方式一    使用注解方式配置,添加事務管理器和事務注解掃描器 -->
<!--     <tx:annotation-driven transaction-manager="transactionManager"/>
    <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource" />
    </bean> -->
    
    <!-- 方式二   使用XML配置方式,需要支持AOP方式 -->
    <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource" />
    </bean>
     
    <tx:advice id="txadvice" transaction-manager="transactionManager">  
        <tx:attributes>  
            <tx:method name="add*" propagation="REQUIRED" rollback-for="Exception" />  
            <tx:method name="del*" propagation="REQUIRED" rollback-for="Exception"/>
            <tx:method name="edit*" propagation="REQUIRED" rollback-for="Exception" />
            <tx:method name="update*" propagation="REQUIRED" rollback-for="Exception"/>  
            <tx:method name="list*" propagation="REQUIRED" rollback-for="Exception"/>  
        </tx:attributes>  
    </tx:advice>  
        
    <aop:config>  
        <aop:pointcut id="serviceMethod" expression="execution(* com.how2java.service.*.*(..))"/>  
        <aop:advisor pointcut-ref="serviceMethod" advice-ref="txadvice"/>  
    </aop:config> 
    <!-- quartz配置 -->  <import resource="classpath:quartz.xml"/> </beans>

 1.3.2 springMVC.xml(位置:ssm/src/main/resources)

<?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:mvc="http://www.springframework.org/schema/mvc"
    xsi:schemaLocation="http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-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/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-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/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd">
     <!-- 1. 掃描Controller,並將其生命周期納入Spring管理 -->
      
      <!-- context:annotation-config表示告訴Spring要用注解的方式進行配置。
         context:annotation-config配置項作用:
        1)隱式地向Spring容器中注冊AutowiredAnnotationBeanPostProcessor、CommonAnnotationBeanPostProcessor、PersistenceAnnotationBeanPostProcessor 及 equiredAnnotationBeanPostProcessor 這 4 個 BeanPostProcessor。
        在配置文件中使用<context:annotationconfig/>之前,必須在 <beans> 元素中聲明 context 命名空間<context:component-scan/>。
        2)是對包進行掃描,實現注釋驅動Bean定義,同時將bean自動注入容器中使用。即解決了@Controller標識的類的bean的注入和使用。 
     --> 
    
    <context:annotation-config/>
    
      <!-- context:component-scan的作用是告訴Spring,Spring能管理bean都放在com.how2java.controller這個包下
       context:component-scan配置項不但啟用了對類包進行掃描以實施注釋驅動 Bean 定義的功能,同時還啟用了注釋驅動自動注入的功能(即還隱式地在內部注冊了 AutowiredAnnotationBeanPostProcessor 和 CommonAnnotationBeanPostProcessor),
          因此當使用 <context:component-scan/> 后,除非需要使用PersistenceAnnotationBeanPostProcessor和equiredAnnotationBeanPostProcessor兩個Processor的功能(例如JPA等)否則就可以將 <context:annotation-config/> 移除了。
       -->
    <context:component-scan base-package="com.how2java.controller">
          <!-- 只對@Controller進行掃描就可  參考:https://www.cnblogs.com/haoke/p/4604883.html-->
          <context:include-filter type="annotation"
          expression="org.springframework.stereotype.Controller"/>
    </context:component-scan>
     <!-- 2. 注解驅動,以使得訪問路徑與方法的匹配可以通過注解配置 -->
    <mvc:annotation-driven />
    
     <!--
      3. 靜態頁面,如html,css,js,images可以訪問。
         servlet在找頁面時,走的是dispatcherServlet路線。找不到的時候會報404
          加上這個默認的servlet時候,servlet在找不到的時候會去找靜態的內容。
        如果沒有試圖定位, 這個標簽起作用的前提就是需要我們把靜態資源放在/webapp下,而非/WEB-INF下。
      -->
    <mvc:default-servlet-handler />
 
    <!-- 4. 視圖定位到/WEB/INF/jsp 這個目錄下,如果沒有試圖定位到/WEB/INF/jsp文件夾,靜態資源默認加載為在/webapp下的靜態文件,而非/WEB-INF下。-->
    
    <!-- 視圖解析-jsp -->
    <bean
        class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="viewClass"
            value="org.springframework.web.servlet.view.JstlView" />
        <property name="prefix" value="/WEB-INF/jsp/" />
        <property name="suffix" value=".jsp" />
         <property name="order" value="2" />
    </bean>
    <!-- 視圖解析-html -->
   <bean id="freemarkerConfig" class="org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer">
         <property name="templateLoaderPath" value="" />  
         <property name="defaultEncoding" value="utf-8" />  
         <property name="freemarkerSettings">  
             <props>  
                 <prop key="classic_compatible">true</prop> 
                  <prop key="whitespace_stripping">true</prop>
                  <prop key="template_update_delay">0</prop> 
                  <prop key="datetime_format">yyyy-MM-dd HH:mm:ss</prop>
                  <prop key="time_format">HH:mm:ss</prop>
                  <prop key="date_format">yyyy-MM-dd</prop>
                  <prop key="default_encoding">UTF-8</prop>
                  <prop key="number_format">#.</prop>  
             </props>  
        </property>  
    </bean>
    <bean id="freemarkerViewResolver" class="org.springframework.web.servlet.view.freemarker.FreeMarkerViewResolver">
        <property name="contentType" value="text/html;charset=UTF-8" />
         <property name="order" value="1" />
    </bean>
    <mvc:interceptors>    
        <mvc:interceptor>    
            <mvc:mapping path="/view/*"/>  
            <!-- 定義在mvc:interceptor下面的表示是對特定的請求才進行攔截的 --> 
            <bean class="com.how2java.interceptor.IndexInterceptor"/>      
        </mvc:interceptor>  
        <!-- 當設置多個攔截器時,先按順序調用preHandle方法,然后逆序調用每個攔截器的postHandle和afterCompletion方法 --> 
        <mvc:interceptor>    
             <!--  
                /**的意思是所有文件夾及里面的子文件夾 
                /*是所有文件夾,不含子文件夾 
                /是web項目的根目錄
              --> 
            <mvc:mapping path="/view/**"/>  
            <mvc:exclude-mapping path="/view/listCategoryInJSP"/> 
            <!-- 定義在mvc:interceptor下面的表示是對特定的請求才進行攔截的 --> 
            <bean class="com.how2java.interceptor.IndexInterceptor2"/>      
        </mvc:interceptor>  
    </mvc:interceptors> 
      <!-- 注解方式實現AOP -->
  
      <!-- 激活自動代理功能   --> 
      <!-- 代理方式1:采用JDK代理 -->
        <aop:aspectj-autoproxy/> 
        <!--     代理方式2:cglib代理 -->
        <!--     <aop:aspectj-autoproxy proxy-target-class="true"/>   -->
        <!-- 找到被注解了的切面類,進行切面配置 -->
        <context:component-scan base-package="com.how2java.aspect"/>
        <context:component-scan base-package="com.how2java.controller"/>
</beans>

 1.4 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:mvc="http://www.springframework.org/schema/mvc"
    xsi:schemaLocation="http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-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/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-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/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd">
    
<!-- 配置quartz -->  
    <!-- 任務實例化 -->     
    <!-- 第一個定時器 -->  
    <bean id="myJob"  
        class="org.springframework.scheduling.quartz.JobDetailFactoryBean">  
          <property name="Durability" value="true"/>  <!-- 如果一個job是非持久的,當沒有活躍的trigger與之關聯的時候,會被自動地從scheduler中刪除。也就是說,非持久的job的生命期是由trigger的存在與否決定的 -->
        <property name="jobClass" value="com.how2java.timer.SimpleTriggerJob" />  
    </bean>  
    <bean id="myTrigger" class="org.springframework.scheduling.quartz.SimpleTriggerFactoryBean">  
        <property name="jobDetail" ref="myJob"/>  
        <property name="startDelay" value="5000"/>  <!-- 調度工廠實例化后,經過5秒開始執行調度 -->  
        <property name="repeatInterval" value="2000"/>  <!-- 每隔2秒執行一次 -->
    </bean>  
    
    <!-- 第二個定時器 -->  
    <bean id="cronJob"  
        class="org.springframework.scheduling.quartz.JobDetailFactoryBean">  
          <property name="Durability" value="true"/>  
        <property name="jobClass" value="com.how2java.timer.CronTriggerJob" />  
    </bean>  
    <!-- -這個定時器是到達指定的時間去執行 -->  
    <bean id="cronTrigger" class="org.springframework.scheduling.quartz.CronTriggerFactoryBean">  
        <!-- 這個屬性是為了解決這個的報錯:  SchedulerException: Jobs added with no trigger must be durable. -->  
        <property name="jobDetail" ref="cronJob"/>  
          
        <!-- cron表達式的解釋自己去百度。。。。 -->  
        <property name="cronExpression" value="0 35,36,37,38 * * * ?"/> <!-- 在35分、36分、37分,38分執行一次 --> 
    </bean>  

      
    <!-- 觸發定時器 -->  
    <bean class="org.springframework.scheduling.quartz.SchedulerFactoryBean">  
        <property name="triggers">  
            <list>  
            <!-- 把定時器加入列表 -->  
                <ref bean="myTrigger"/>  
                <ref bean="cronTrigger"/>  
            </list>  
        </property>  
    </bean>  
    <!-- 配置quartz -->  
</beans> 
package com.how2java.timer;
import java.text.SimpleDateFormat;
import java.util.Date;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.springframework.scheduling.quartz.QuartzJobBean;

public class SimpleTriggerJob extends QuartzJobBean{

    
    /**
     *   這個類是每隔多少時間來執行的業務類。把你要執行的業務寫在這里
     * 
     */
    protected void executeInternal(JobExecutionContext context) throws JobExecutionException {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); 
        System.out.println("SimpleTriggerJob 定時器開始-------------------"+sdf.format(new Date()));
    }

}

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM