在前文中已经说明了如何基于 Spring4.x+ 版本开发纯注解的非web项目,链接如下:
https://my.oschina.net/morpheusWB/blog/2985600
本文则主要说明,如何在Web项目中,"基于spring纯注解方式如何开发?",且看下文:
下面基于Eclipse Oxygen版本,开发一个普通的Java Web工程,演示如下:
1. 基于 maven-archetype-webapp 骨架 创建一个普通的Maven工程:
2. 在pom.xml中加入spring、logback、servlet及常用jar包的依赖:
3. 在src/main/resources下创建 application.properties、logback.xml、assembly.xml 等配置文件
4. 在Java 代码中创建 @Configuration、@ComponentScan类用于替代xml配置
代替springmvc.xml 配置的类定义
package com.morpheus.web.config;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.ComponentScan.Filter;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Controller;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
@Configuration
@EnableWebMvc
@ComponentScan(basePackages = "com.morpheus.web.controller", includeFilters = @Filter(classes = Controller.class), useDefaultFilters = false)
public class WebMVCConfig {
}
代替applicationContext.xml 的类定义
package com.morpheus.web.config;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.ComponentScan.Filter;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Controller;
@Configuration
@ComponentScan(basePackages = { "com.morpheus.web" }, excludeFilters = { @Filter(classes = Controller.class) })
public class ApplicationConfig {
}
5. 在Java 代码中编写 @Component、@Service、@Controller、@RestController 等基本组件类
package com.morpheus.web.controller;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import com.alibaba.fastjson.JSONObject;
import com.morpheus.web.util.JSONUtil;
@RestController
public class TimeController {
private static final Logger LOGGER = LoggerFactory.getLogger(TimeController.class);
public TimeController() {
}
@RequestMapping(value = "/time", method = { RequestMethod.GET })
public ResponseEntity<String> getTime() {
LOGGER.info("TimeController.getTime() begin...");
DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss:SSS");
String time = dateFormat.format(Calendar.getInstance().getTime());
JSONObject resJSON = JSONUtil.createJSON(true, HttpStatus.OK.value(), time);
return ResponseEntity.ok().contentType(MediaType.APPLICATION_JSON_UTF8).body(resJSON.toJSONString());
}
}
6. 在Java 代码中使用 @Autowired、@Resource 等注入依赖Bean
7. 在Java 代码中使用 @Value 等注入配置值
8. 使用 maven-war-plugin 打包项目为标准的war包,以便部署到servlet容器中
9. 在web.xml中指定相关配置
<!DOCTYPE web-app PUBLIC
"-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/dtd/web-app_2_3.dtd" >
<web-app>
<display-name>WebMVC Application</display-name>
<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>
<init-param>
<param-name>forceRequestEncoding</param-name>
<param-value>false</param-value>
</init-param>
<init-param>
<param-name>forceResponseEncoding</param-name>
<param-value>true</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>characterEncodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<servlet>
<servlet-name>SpringServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextClass</param-name>
<param-value>org.springframework.web.context.support.AnnotationConfigWebApplicationContext</param-value>
</init-param>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>com.morpheus.web.config.WebMVCConfig</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>SpringServlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
当然,步骤9中如果是基于 servlet 3.x 规范,也可以通过类定义方式取代 web.xml 配置;
完成以上步骤开发后,使用 mvn clean package 打包部署到 tomcat 就可以正常使用服务了