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

之前复制粘贴创建了几个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

本文参与腾讯云自媒体分享计划,欢迎正在阅读的你也加入,一起分享。

发表于

我来说两句

0 条评论
登录 后参与评论

相关文章

来自专栏实战docker

Docker下,极速体验pinpoint1.6.3

pinpoint在分布式系统中的广泛应用,今天来体验pinpoint所提供的监控、调用链跟踪等服务,本次实战只关心体验服务,至于环境如何搭建、服务如何配置,留待...

24110
来自专栏云计算教程系列

如何使用killall和kill命令来停止进程

killall是一个基于名称终止系统上运行进程的工具。kill则是终止基于进程ID号(PID)的进程。kill和killall还可以向进程发送特定的系统信号。

1393
来自专栏腾讯云Elasticsearch Service

logstash input插件开发

logstash作为一个数据管道中间件,支持对各种类型数据的采集与转换,并将数据发送到各种类型的存储库,比如实现消费kafka数据并且写入到Elasticsea...

3004
来自专栏xingoo, 一个梦想做发明家的程序员

Spark监控官方文档学习笔记

任务的监控和使用 有几种方式监控spark应用:Web UI,指标和外部方法 Web接口 每个SparkContext都会启动一个web UI,默认是40...

2429
来自专栏写写代码吃吃瓜

Tornado中sqlalchemy使用

2156
来自专栏康怀帅的专栏

Redis 备份与恢复

本文介绍 Redis 持久化。 RDB 该方式为默认方式。 RDB 方式的持久化是通过快照(snapshotting)完成的,当符合一定条件时 Redis 会自...

2824
来自专栏Java编程技术

Springboot模块分类

springboot可以依据classpath里面的依赖的内容来自动配置bean到IOC容器,但是要开启这个自动配置功能需要添加@EnableAutoConfi...

621
来自专栏编程坑太多

springboot (八) 文件上传下载

1083
来自专栏行者常至

Hibernate之HelloWorld

452
来自专栏菩提树下的杨过

log4j2 与 spring mvc整合

log4j2不仅仅是log4j的简单升级,而是整个项目的重构,官网地址:http://logging.apache.org/log4j/2.x/,大家可以从官网...

411

扫码关注云+社区