Spring常见问题

如何配置定时任务

Spring 3中涉及定时任务的主要是TaskScheduler接口,利用spring的task命名空间,可以很简单地配置一个定时任务,以下是配置一个简单的定时任务(每隔5秒执行一次beanA.methodA方法)

1
2
3
4
5
6
7
8
9
<beans xmlns:task="http://www.springframework.org/schema/task"
......>
<task:scheduler id="myScheduler" pool-size="10"/>
<task:scheduled-tasks scheduler="myScheduler">
<task:scheduled ref="beanA" method="methodA" fixed-rate="5000"/>
</task:scheduled-tasks>
......
</beans>

如何使用spring4.x配置RESTFUL API

pom.xml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>4.3.2.RELEASE</version>
</dependency>
<!--
jackson 2的包在classpath时,spring会使用该包进行json转换,详见spring官方文档的
“Enabling the MVC Java Config or the MVC XML Namespace”
-->
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.8.3</version>
</dependency>
</dependencies>

一个简单的Controller

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
@RestController
@RequestMapping("/users")
public class RestfulController {
/**
* 方式1
*
* @param id
* @return 返回一个ResponseEntity对象
*/
@RequestMapping(value="/type1/{id}", method = RequestMethod.GET)
public ResponseEntity<User> getUserById1(@PathVariable int id) {
return ResponseEntity.ok(new User(id, "type1 user"));
}
/**
* 方式2
*
* @param id
* @return 返回一个POJO
*/
@RequestMapping(value="/type2/{id}", method = RequestMethod.GET)
public User getUserById2(@PathVariable String id) {
return new User(1, "type2 user");
}
}

POJO对象

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
public class User {
private final int id;
private final String name;
public User(int id, String name) {
this.id = id;
this.name = name;
}
public int getId() {
return id;
}
public String getName() {
return name;
}
}

web.xml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
<?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"
metadata-complete="true">
<servlet>
<servlet-name>springmvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring-mvc.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>springmvc</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>
</web-app>

spring-mvc.xml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
<?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.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd">
<context:component-scan base-package="io.jasonlu.springrestful.controller"/>
<mvc:annotation-driven/>
</beans>