SPRINGMVC4 environment Simple construction and scheduled tasks

Source: Internet
Author: User

Before the copy and paste created a few SSM project, but back to let yourself write the time is still confused, the reason is spring unfamiliar. Just wrote a few HelloWorld. And it's written. I don't even know how many jars springmvc need to do. So, the next step is to look at the spring principle.

Recently, due to the need for scheduled tasks, a simple springmvc4 HelloWorld was built.

The IDE uses idea, and of course eclipse should be no problem, because it's all MAVEN projects.

1. Create a good MAVEN structure.

Can be created by New->project->maven. After creation, it is added to the normal javaweb structure. That is Src\main\java,src\main\resource,src\main\webapp.

2.jar Pack--pom

It's unclear how much the jar package is and what it does. Here is a version I found on the internet, very few jars, and gradually increase the time needed later on the line.

<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/maven-v4_0_0.xsd" > <modelversion >4.0.0</modelVersion> <groupId>com.test</groupId> <artifactid>spmvtest</artifactid > <packaging>war</packaging> <version>1.0-SNAPSHOT</version> <name>spmvtest Maven Webapp</name> <url>http://maven.apache.org</url> <!--set up the main warehouse and find it in the set order.    -<repositories> <!--If you have a nexus, uncomment it and point to the correct server address. <repository> <id>nexus-repos</id> <name>team Nexus repository</name> &L T;url>http://192.168.11.36:8888/nexus/content/groups/public</url> </repository> < repository> <id>oschina-repos</id> <name>oschina releases</name> <url>http ://maven.oschina.net/content/groups/public</url> </repository> <repository> <id>java-repos</id> & Lt;name>java repository</name> <url>http://download.java.net/maven/2/</url> </repository&    Gt      <repository> <id>springsource-repos</id> <name>springsource repository</name> <url>http://repo.spring.io/release/</url> </repository> <repository> <id>central -repos</id> <name>central repository</name> <url>http://repo.maven.apache.org/maven2< /url> </repository> <repository> <id>central-repos2</id> <name>central Re Pository 2</name> <url>http://repo1.maven.org/maven2/</url> </repository> </repositori    Es> <!--set up the plugin warehouse--<pluginRepositories> <!--If you have a nexus, uncomment it and point to the correct server address. <pluginRepository> &LT;ID&GT;nexus-repos</id> <name>team Nexus repository</name> <url>http://192.168.11.36:8888 /nexus/content/groups/public</url> </pluginRepository> <pluginRepository> <id>os china-repos</id> <name>oschina releases</name> <url>http://maven.oschina.net/content/gro ups/public</url> </pluginRepository> </pluginRepositories> <properties> <spring.version >4.0.8.RELEASE</spring.version> <quartz.version>2.2.1</quartz.version> <slf4j.version >1.7.7</slf4j.version> <log4j.version>1.2.17</log4j.version> </properties> < dependencies> <dependency> <groupId>junit</groupId> <artifactid>junit</artifact id> <version>3.8.1</version> <scope>test</scope> </dependency> <!--SPR  INGMVC begin--> <dependency>    <groupId>org.springframework</groupId> <artifactId>spring-core</artifactId> <vers ion>${spring.version}</version> </dependency> <dependency> <groupid>org.springframew Ork</groupid> <artifactId>spring-webmvc</artifactId> <version>${spring.version}</vers ion> </dependency> <dependency> <groupId>org.springframework</groupId> <arti    Factid>spring-beans</artifactid> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactid>spring-context</artif Actid> <version>${spring.version}</version> </dependency> <!--springmvc end--> &L t;! --quartz--> <dependency> <groupId>org.quartz-scheduler</groupId> <artifactid>quart Z</artifactid>     <version>${quartz.version}</version> </dependency> <dependency> <groupid>jst l</groupid> <artifactId>jstl</artifactId> <version>1.2</version> </dependenc       y> <dependency> <groupId>taglibs</groupId> <artifactId>standard</artifactId> <version>1.1.2</version> </dependency> <!--log Start-to-<dependency> & Lt;groupid>log4j</groupid> <artifactId>log4j</artifactId> &LT;VERSION&GT;${LOG4J.VERSION}&L t;/version> </dependency> <dependency> <groupId>org.slf4j</groupId> &LT;ARTIFAC tid>slf4j-api</artifactid> <version>${slf4j.version}</version> </dependency> &LT;DEP endency> <groupId>org.slf4j</groupId> <artifactId>slf4j-log4j12</artifactId> &lt ; version>${slf4j.versIon}</version> </dependency> <!--log End-to-</dependencies> <build> <finalna Me>spmvtest</finalname> </build></project>
3.web.xml

Set up interceptors and filters

<?xml version= "1.0" encoding= "UTF-8"? ><web-app xmlns= "Http://java.sun.com/xml/ns/javaee" xmlns:xsi= "http: Www.w3.org/2001/XMLSchema-instance "xsi:schemalocation=" Http://java.sun.com/xml/ns/javaee http://java.sun.com/ Xml/ns/javaee/web-app_3_0.xsd "version=" 3.0 "> <!--Load Spring container--<context-param> <param-na Me>contextconfiglocation</param-name> <param-value>classpath:spring/spring-*.xml</param-value > </context-param> <listener> <listener-class> Org.springframework.web.context.contextloaderlistener</listener-class> </listener> <!-- SPRINGMVC Front Controller, rest configuration-<servlet> <servlet-name>springmvc_rest</servlet-name> <servlet-c Lass>org.springframework.web.servlet.dispatcherservlet</servlet-class> <!-- Contextconfiglocation Configuring SPRINGMVC loaded configuration files (configuring processor mapper, adapters, and so on) if Contextconfiglocation is not configured, the default load is/web-inf/ Servlet name-serlvet.xml (springmvc-seRvlet.xml)-<init-param> <param-name>contextConfigLocation</param-name> <param-val Ue>classpath:spring/spring-mvc.xml</param-value> </init-param> </servlet> < Servlet-mapping> <servlet-name>springmvc_rest</servlet-name> <url-pattern>/</url-pattern > </servlet-mapping> <!--post garbled filters--<filter> <filter-name>CharacterEncodingFilter< /filter-name> <filter-class>org.springframework.web.filter.characterencodingfilter</filter-class > <init-param> <param-name>encoding</param-name> <param-value>utf-8</param-valu e> </init-param> </filter> <filter-mapping> <filter-name>characterencodingfilter</fi Lter-name> <url-pattern>/*</url-pattern> </filter-mapping> <!--direct access to static resources-< Servlet-mapping> <servlet-name>default</servlet-name> <url-pattern>*.css</url-pattern> </servlet-mapping> <servlet-mapping> <servlet-nam E>default</servlet-name> <url-pattern>*.ico</url-pattern> </servlet-mapping> < Servlet-mapping> <servlet-name>default</servlet-name> <url-pattern>*.png</url-pattern > </servlet-mapping> <servlet-mapping> <servlet-name>default</servlet-name> <url-pat tern>*.jpg</url-pattern> </servlet-mapping> <servlet-mapping> <servlet-name>default </servlet-name> <url-pattern>*.js</url-pattern> </servlet-mapping> <servlet-mapping > <servlet-name>default</servlet-name> <url-pattern>*.html</url-pattern> </ Servlet-mapping></web-app>
  4.spring Container configuration file Spring/spring-contex.xml
<?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:task= "Http://www.springframework.org/schema/task" 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/ Task Http://www.springframework.org/schema/task/spring-task.xsd Http://www.springframework.org/schema/context Http://www.springframework.org/schema/context/spring-context.xsd "> <context:component-scan base-package=" Com.test "/> <!--Scheduled task configuration with @Service @Lazy (false) Label class with @scheduled (cron =" 0 0 2 * *? ")    Labeling Method--<task:executor id= "executor" pool-size= "/> <task:scheduler id=" Scheduler "pool-size="/> <task:annotation-driven scheduler= "Scheduler" executor= "executor" Proxy-target-claSs= "true"/></beans> 
  5.dispatcherServlet configuration file Spring/spring-mvc.xml
<?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:context= "Http://www.springframework.org/schema/context" Xmlns:mvc= "Http://www.springframework.org/schema/mvc" xsi:schemalocation= "Http://www.springframework.org/schema /beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/ Context Http://www.springframework.org/schema/context/spring-context-4.1.xsd http://www.springframework.org/ Schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.1.xsd "><context:component-scan Base-package= "Com.test"/><mvc:annotation-driven/><bean id= "Viewresolver" class= " Org.springframework.web.servlet.view.UrlBasedViewResolver "><property name=" Viewclass "value=" Org.springframework.web.servlet.view.JstlView "/><property name=" prefix "value="/web-inf/views/"/>< Property name= "suffix" VAlue= ". JSP"/></bean></beans> 
6.helloController Test
Package Com.test.controller;import Org.springframework.stereotype.controller;import Org.springframework.ui.modelmap;import org.springframework.web.bind.annotation.requestmapping;/** * Created by Administrator on 2016/4/14 0014. */@Controllerpublic class Hellocontroller {    @RequestMapping ("/index") public    String Index (Modelmap modelmap) {        Modelmap.put ("username", "Ryan Miao");        Return "index";}    }
7. View:/web-inf/views/index.jsp
8. Scheduled Tasks task
Package Com.test.task;import Org.springframework.context.annotation.lazy;import Org.springframework.scheduling.annotation.scheduled;import org.springframework.stereotype.service;/** * Created by Administrator on 2016/4/14 0014. */@Service @lazy (false) public class MyTask implements ITask {    private static int i = 0;    private static int y = 0;    @Scheduled (cron= "0/5 * * * * *  ?")   Public    void Testcyc () {        System.out.println ("Task test, number of executions:" +i++) executed once every 5 seconds;    }    /**     * Heartbeat update. Executes once at startup, and executes once every 2 seconds     */    @Scheduled (fixedrate = 1000*2) public    void print () {        System.out.println (" Heartbeat execution: "+y++);}    }

9. Test code: GitHub

SPRINGMVC4 environment Simple construction and scheduled tasks

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.