+ Spring MVC 版本

POM文件

<project  ns=\"http://maven.apache.org/POM/4.0.0\"  ns:xsi=\"http://www.w3.org/2001/ Schema-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.adagio</groupId>
 <artifactId>task</artifactId>
 <version>0.0.1-SNAPSHOT</version>
 <packaging>war</packaging>
 <name>task</name>
 <de ion/>
 
 <properties>
  <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  <spring.version>4.1.6.RELEASE</spring.version>
 </properties>
 
 <dependencies>
 
   <dependency>
    <groupId>org.aspectj</groupId>
    <artifactId>aspectjweaver</artifactId>
    <version>1.8.3</version>
  </dependency>
  
   <dependency>
    <groupId>org.quartz-scheduler</groupId>
    <artifactId>quartz</artifactId>
    <version>2.2.1</version>
  </dependency>
  
  <dependency>
   <groupId>javax</groupId>
   <artifactId>javaee-api</artifactId>
   <version>7.0</version>
   <scope>provided</scope>
  </dependency>
  
  <dependency>
     <groupId>junit</groupId>
     <artifactId>junit</artifactId>
     <version>4.12</version>
     <scope>test</scope>
   </dependency>

   <!--SPRING DEPENDENCY-->
   <dependency>
     <groupId>org.spring work</groupId>
     <artifactId>spring-context</artifactId>
     <version>${spring.version}</version>
   </dependency>
   <dependency>
     <groupId>org.spring work</groupId>
     <artifactId>spring-test</artifactId>
     <version>${spring.version}</version>
   </dependency>
   <dependency>
     <groupId>org.spring work</groupId>
     <artifactId>spring-context-support</artifactId>
     <version>${spring.version}</version>
   </dependency>
   <dependency>
     <groupId>org.spring work</groupId>
     <artifactId>spring-webmvc</artifactId>
     <version>${spring.version}</version>
   </dependency>
   <dependency>
     <groupId>org.spring work</groupId>
     <artifactId>spring-tx</artifactId>
     <version>${spring.version}</version>
   </dependency>



 </dependencies>
  
 <build>
   <finalName>task</finalName>
  <plugins>
   <plugin>
    <artifactId>maven-compiler-plugin</artifactId>
    <version>2.3.2</version>
    <configuration>
     <source>1.7</source>
     <target>1.7</target>
    </configuration>
   </plugin>
   <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-war-plugin</artifactId>
        <version>2.6</version>
      </plugin>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-surefire-plugin</artifactId>
        <version>2.18.1</version>
        <configuration>
          <skipTests>true</skipTests>
        </configuration>
      </plugin>
  </plugins>
 </build>
 
</project>

web.

<?  version=\"1.0\" encoding=\"UTF-8\"?>
<web-app  ns:xsi=\"http://www.w3.org/2001/ Schema-instance\"  ns=\"http:// ns.jcp.org/ /ns/javaee\" xsi:schemaLocation=\"http:// ns.jcp.org/ /ns/javaee http:// ns.jcp.org/ /ns/javaee/web-app_3_1.xsd\" id=\"WebApp_ID\" version=\"3.1\">
 <context-param>
  <param-name>contextConfigLocation</param-name>
  <param-value>
      classpath*:/spring/spring-core. 
    </param-value>
 </context-param>
 <listener>
  <listener-class>org.spring work.web.context.ContextLoaderListener</listener-class>
 </listener>
 <listener>
  <listener-class>org.spring work.web.context.request.RequestContextListener</listener-class>
 </listener>
 <filter>
  <filter-name>CharacterEncodingFilter</filter-name>
  <filter-class>org.spring work.web.filter.CharacterEncodingFilter</filter-class>
  <init-param>
   <param-name>encoding</param-name>
   <param-value>utf-8</param-value>
  </init-param>
 </filter>
 <filter-mapping>
  <filter-name>CharacterEncodingFilter</filter-name>
  <url-pattern>/*</url-pattern>
 </filter-mapping>
 <servlet>
  <servlet-name>dispatcher</servlet-name>
  <servlet-class>org.spring work.web.servlet.DispatcherServlet</servlet-class>
  <init-param>
   <param-name>contextConfigLocation</param-name>
   <param-value>classpath*:/spring/spring-mvc. </param-value>
  </init-param>
  <load-on-startup>1</load-on-startup>
 </servlet>
 <servlet-mapping>
  <servlet-name>dispatcher</servlet-name>
  <url-pattern>/</url-pattern>
 </servlet-mapping>
 
 <display-name>task</display-name>
 <welcome-file-list>
  <welcome-file>index.jsp</welcome-file>
 </welcome-file-list>
 <welcome-file-list>
  <welcome-file>index.html</welcome-file>
  <welcome-file>index.htm</welcome-file>
  <welcome-file>index.jsp</welcome-file>
  <welcome-file>default.html</welcome-file>
  <welcome-file>default.htm</welcome-file>
  <welcome-file>default.jsp</welcome-file>
 </welcome-file-list>
</web-app>

spring.

<?  version=\"1.0\" encoding=\"UTF-8\"?>
<beans  ns=\"http://www.spring work.org/schema/beans\"
   ns:xsi=\"http://www.w3.org/2001/ Schema-instance\"
   ns:p=\"http://www.spring work.org/schema/p\"
   ns:tx=\"http://www.spring work.org/schema/tx\"
   ns:context=\"http://www.spring work.org/schema/context\"
   ns:aop=\"http://www.spring work.org/schema/aop\"
   ns:task=\"http://www.spring work.org/schema/task\"
  xsi:schemaLocation=\"http://www.spring work.org/schema/aop http://www.spring work.org/schema/aop/spring-aop-4.1.xsd
    http://www.spring work.org/schema/beans http://www.spring work.org/schema/beans/spring-beans-4.1.xsd
    http://www.spring work.org/schema/tx http://www.spring work.org/schema/tx/spring-tx-4.1.xsd
    http://www.spring work.org/schema/context http://www.spring work.org/schema/context/spring-context-4.1.xsd
    http://www.spring work.org/schema/task 
    http://www.spring work.org/schema/task/spring-task-4.0.xsd \"
    >

  
  <context:component-scan  -package=\"com.adagio\">
    <context:exclude-filter type=\"annotation\"  =\"org.spring work.stereotype.Controller\"/>
  </context:component-scan>
  
  <import resource=\"spring-quartz. \" />
  
</beans>

spring-mvc.

<?  version=\"1.0\" encoding=\"UTF-8\"?>
<beans
   ns=\"http://www.spring work.org/schema/beans\"
   ns:xsi=\"http://www.w3.org/2001/ Schema-instance\"
   ns:p=\"http://www.spring work.org/schema/p\"
   ns:mvc=\"http://www.spring work.org/schema/mvc\"
   ns:context=\"http://www.spring work.org/schema/context\"
   ns:aop=\"http://www.spring work.org/schema/aop\"
  xsi:schemaLocation=\"http://www.spring work.org/schema/beans 
    http://www.spring work.org/schema/beans/spring-beans-3.1.xsd
    http://www.spring work.org/schema/mvc 
    http://www.spring work.org/schema/mvc/spring-mvc-3.1.xsd
    http://www.spring work.org/schema/aop
    http://www.spring work.org/schema/aop/spring-aop-4.1.xsd
    http://www.spring work.org/schema/context 
    http://www.spring work.org/schema/context/spring-context-3.1.xsd\">
  <!--扫描Controller-->
  <context:component-scan  -package=\"com.adagio\">
    <context:include-filter type=\"annotation\"  =\"org.spring work.stereotype.Controller\" />
  </context:component-scan>
  
  <aop:aspectj-autoproxy proxy-target-class=\"true\"/>
  
  <mvc:annotation-driven>
    <mvc:message-converters>
      <bean class=\"org.spring work.http.converter.StringHttpMessageConverter\">
        <constructor-arg name=\"defaultCharset\" value=\"UTF-8\" />   
        <property name=\"supportedMediaTypes\">   
          <list>
            <value>text/plain;charset=UTF-8</value>   
            <value>text/html;charset=UTF-8</value>   
          </list>   
        </property>   
      </bean>   
    </mvc:message-converters>
  </mvc:annotation-driven>


  <!--定义视图解析-->
  <bean class=\"org.spring work.web.servlet.view.InternalResourceViewResolver\">
    <property name=\"prefix\" value=\"/WEB-INF/views/\" />
    <property name=\"suffix\" value=\".jsp\" />
  </bean>
  
  <!--对静态资源的处理-->
  <!-- <mvc:default-servlet-handler /> -->
  <!-- Handles HTTP GET requests for /resources/** by efficiently serving up static resources in the ${webappRoot}/resources directory -->
  <mvc:resources mapping=\"/static/**\" location=\"/static/\" />
  

</beans>

spring-quartz.

<?  version=\"1.0\" encoding=\"UTF-8\"?>
<beans  ns=\"http://www.spring work.org/schema/beans\"
   ns:xsi=\"http://www.w3.org/2001/ Schema-instance\"
  xsi:schemaLocation=\"http://www.spring work.org/schema/beans http://www.spring work.org/schema/beans/spring-beans.xsd\">
  

  <!-- ======================== 调度工厂 ======================== -->
  <bean id=\"SpringJobSchedulerFactoryBean\" class=\"org.spring work.scheduling.quartz.SchedulerFactoryBean\">
    <property name=\"triggers\">
      <list>
        <!-- 调度触发器 -->
        <ref bean=\"helloTrigger\"/>
      </list>
    </property>
  </bean> 

   <!-- ======================== 调度触发器 ======================== -->
   <bean id=\"helloTrigger\" class=\"org.spring work.scheduling.quartz.CronTriggerFactoryBean\">
    <!-- 任务调度类 -->
    <property name=\"jobDetail\" ref=\"helloJob\"></property>
    <property name=\"cron \" value=\"0/5 * * * * ?\"></property>
  </bean>
  
   <!-- 任务调度类 -->
  <bean id=\"helloJob\" class=\"org.spring work.scheduling.quartz.MethodInvokingJobDetailFactoryBean\">
    <!-- 任务执行类 -->
    <property name=\"target \">
      <ref bean=\"helloTask\"/>
    </property>
    <property name=\"targetMethod\"> 
      <value>excute</value><!-- 要执行的方法名称 -->
    </property>
  </bean>
</beans>

HelloTask

package com.adagio;

import org.spring work.stereotype.Component;

@Component
public class HelloTask {
  
  public void excute() {
    System.out.println(\"excute...22222>>>>>>>>\");
  }
}

configuration + Spring MVC

和上面类似,只是 spring-quartz. 转成configuration

package com.adagio;

import org.spring work.context.annotation.Bean;
import org.spring work.context.annotation.Configuration;
import org.spring work.scheduling.quartz.CronTriggerFactoryBean;
import org.spring work.scheduling.quartz.MethodInvokingJobDetailFactoryBean;
import org.spring work.scheduling.quartz.SchedulerFactoryBean;

@Configuration
public class QuartzConfig {

  @Bean
  public SchedulerFactoryBean factoryBean() {
    SchedulerFactoryBean factoryBean = new SchedulerFactoryBean();
    
    factoryBean.setTriggers(cronTriggerFactoryBean().get ());
    return factoryBean;
  }
  
  @Bean
  public CronTriggerFactoryBean cronTriggerFactoryBean() {
    CronTriggerFactoryBean cronTriggerFactoryBean = new CronTriggerFactoryBean();
    cronTriggerFactoryBean.setJobDetail(methodInvokingJobDetailFactoryBean().get ());
    cronTriggerFactoryBean.setCron (\"0/5 * * * * ?\");
    
    return cronTriggerFactoryBean;
  }
  
  @Bean
  public MethodInvokingJobDetailFactoryBean methodInvokingJobDetailFactoryBean() {
    MethodInvokingJobDetailFactoryBean m = new MethodInvokingJobDetailFactoryBean();
    m.setTarget (helloTask());
    m.setTargetMethod(\"excute\");
    return m;
  }
  
  @Bean
  public HelloTask helloTask() {
    return new HelloTask();
  }
  
}

Configuration + Spring Boot

QuartzConfig 和 HelloTask 与上面一样

启动方式不同

package com.adagio;

import org.spring work.boot.SpringApplication;
import org.spring work.boot.autoconfigure.SpringBootApplication;
import org.spring work.context.annotation.ComponentScan;

@ComponentScan( Packages= {\"com.adagio\"})
@SpringBootApplication
public class TimerTaskApplication {

  public static void main(String[] args) {
    SpringApplication.run(TimerTaskApplication.class, args);
  }
}

总结

  • 对 的方式比较熟悉,所有先配置好
  • confugration如果不是Spring Boot的项目的话,还是用的比较少

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

收藏 打印