springmvc4环境简单搭建和定时任务

简介: 之前复制粘贴创建了几个ssm的项目,然而回头让自己写的时候还是一头雾水,究其原因是spring的陌生。仅仅是写过几个helloworld而已。而且是照着写。我都不知道springmvc到底需要多少jar,都用来干嘛。

之前复制粘贴创建了几个ssm的项目,然而回头让自己写的时候还是一头雾水,究其原因是spring的陌生。仅仅是写过几个helloworld而已。而且是照着写。我都不知道springmvc到底需要多少jar,都用来干嘛。所以,接下来要用心看spring原理了。

最近由于有定时任务的需求,简单搭建了一个springmvc4的helloworld。

ide采用的是idea,当然eclipse应该也是没问题的,因为都是maven项目。

1.创建好maven结构。

可以通过new->project->maven来创建。创建完后补充成正常的javaweb结构。即src\main\java,src\main\resource,src\main\webapp.

2.jar包--pom

关于jar包有多少,干啥用,还不清楚。这里是我在网上找的一个版本,很少jar,以后需要的时候逐渐增加就行了。

<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>
  <!-- 设定主仓库,按设定顺序进行查找。 -->
  <repositories>

    <!-- 如有Nexus私服, 取消注释并指向正确的服务器地址.
    <repository>
        <id>nexus-repos</id>
        <name>Team Nexus Repository</name>
        <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>
      <name>Java Repository</name>
      <url>http://download.java.net/maven/2/</url>
    </repository>

    <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 Repository 2</name>
      <url>http://repo1.maven.org/maven2/</url>
    </repository>

  </repositories>

  <!-- 设定插件仓库 -->
  <pluginRepositories>

    <!-- 如有Nexus私服, 取消注释并指向正确的服务器地址.
    <pluginRepository>
        <id>nexus-repos</id>
        <name>Team Nexus Repository</name>
        <url>http://192.168.11.36:8888/nexus/content/groups/public</url>
    </pluginRepository> -->

    <pluginRepository>
      <id>oschina-repos</id>
      <name>Oschina Releases</name>
      <url>http://maven.oschina.net/content/groups/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</artifactId>
      <version>3.8.1</version>
      <scope>test</scope>
    </dependency>

    <!--springmvc begin-->
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-core</artifactId>
      <version>${spring.version}</version>
    </dependency>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-webmvc</artifactId>
      <version>${spring.version}</version>
    </dependency>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-beans</artifactId>
      <version>${spring.version}</version>
    </dependency>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-context</artifactId>
      <version>${spring.version}</version>
    </dependency>
    <!--springmvc end-->

    <!--quartz-->
    <dependency>
      <groupId>org.quartz-scheduler</groupId>
      <artifactId>quartz</artifactId>
      <version>${quartz.version}</version>
    </dependency>

    <dependency>
      <groupId>jstl</groupId>
      <artifactId>jstl</artifactId>
      <version>1.2</version>
    </dependency>
    <dependency>
      <groupId>taglibs</groupId>
      <artifactId>standard</artifactId>
      <version>1.1.2</version>
    </dependency>


    <!-- log start -->
    <dependency>
      <groupId>log4j</groupId>
      <artifactId>log4j</artifactId>
      <version>${log4j.version}</version>
    </dependency>
    <dependency>
      <groupId>org.slf4j</groupId>
      <artifactId>slf4j-api</artifactId>
      <version>${slf4j.version}</version>
    </dependency>
    <dependency>
      <groupId>org.slf4j</groupId>
      <artifactId>slf4j-log4j12</artifactId>
      <version>${slf4j.version}</version>
    </dependency>
    <!-- log end -->
  </dependencies>
  <build>
    <finalName>spmvtest</finalName>
  </build>
</project>

  3.web.xml

设置拦截器和过滤器

<?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">
  <!-- 加载spring容器 -->
  <context-param>
    <param-name>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前端控制器,rest配置 -->
  <servlet>
    <servlet-name>springmvc_rest</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <!-- contextConfigLocation配置springmvc加载的配置文件(配置处理器映射器、适配器等等) 如果不配置contextConfigLocation,默认加载的是/WEB-INF/servlet名称-serlvet.xml(springmvc-servlet.xml) -->
    <init-param>
      <param-name>contextConfigLocation</param-name>
      <param-value>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乱码过虑器 -->
  <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-value>
    </init-param>
  </filter>
  <filter-mapping>
    <filter-name>CharacterEncodingFilter</filter-name>
    <url-pattern>/*</url-pattern>
  </filter-mapping>

  <!--静态资源直接访问-->
  <servlet-mapping>
    <servlet-name>default</servlet-name>
    <url-pattern>*.css</url-pattern>
  </servlet-mapping>
  <servlet-mapping>
    <servlet-name>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-pattern>*.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容器配置文件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"/>

    <!-- 计划任务配置,用 @Service @Lazy(false)标注类,用@Scheduled(cron = "0 0 2 * * ?")标注方法 -->
    <task:executor id="executor" pool-size="10"/> <task:scheduler id="scheduler" pool-size="10"/>
    <task:annotation-driven scheduler="scheduler" executor="executor" proxy-target-class="true"/>

</beans>

  5.dispatcherServlet配置文件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测试

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.
 */
@Controller
public class HelloController {

    @RequestMapping("/index")
    public String index(ModelMap modelMap){
        modelMap.put("username", "Ryan miao");
        return "index";
    }
}

  7.视图:/web-inf/views/index.jsp

<html>
<body>
<h2>Hello World!</h2>
hello ${username}
</body>
</html>

  8.定时任务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 * *  * * ?")   //每5秒执行一次
    public void testCyc() {
        System.out.println("task test,执行次数:"+i++);
    }

    /**
     * 心跳更新。启动时执行一次,之后每隔2秒执行一次
     */
    @Scheduled(fixedRate = 1000*2)
    public void print(){
        System.out.println("心跳执行次数:"+y++);
    }
}

  9.测试代码:github





唯有不断学习方能改变! -- Ryan Miao
目录
相关文章
|
7月前
|
Java 调度 Spring
SpringBoot实现多线程定时任务动态定时任务配置文件配置定时任务
SpringBoot实现多线程定时任务动态定时任务配置文件配置定时任务
669 0
|
7月前
|
Java Shell 测试技术
SpringBoot指定多套配置文件环境
SpringBoot指定多套配置文件环境
128 1
|
7月前
|
Java BI 调度
Springboot项目中定时任务的四种实现方式
Springboot项目中定时任务的四种实现方式
320 4
|
Java
自动执行任务:SpringBoot启动期间的最佳实践
自动执行任务:SpringBoot启动期间的最佳实践
70 0
|
7月前
|
Java Linux
SpringBoot项目快速启动停止脚本
SpringBoot项目快速启动停止脚本
53 0
|
Java 关系型数据库 MySQL
Springboot定时任务灵活配置
Springboot定时任务灵活配置
186 0
|
Java 应用服务中间件 Spring
Jeesite中部署定时任务,定时任务tomcat部署详细,定时任务在spring项目中部署
Jeesite中部署定时任务,定时任务tomcat部署详细,定时任务在spring项目中部署
298 0
|
Java Spring
Spring Boot 实现定时任务动态管理,太爽了!
Spring Boot 实现定时任务动态管理,太爽了!
|
Java Linux 调度
springboot整合quarzt实现动态定时任务
springboot整合quarzt实现动态定时任务
264 0
springboot整合quarzt实现动态定时任务
|
Java 数据安全/隐私保护 Spring
18、SpringBoot2.0中项目启动时启动资源(十八)
在我们实际工作中,总会遇到这样需求,在项目启动的时候需要做一些初始化的操作,比如初始化线程池,提前加载好加密证书等。今天就给大家介绍一个 Spring Boot 神器,专门帮助大家解决项目启动初始化资源操作。
102 0