在前文中已经说明了如何基于 Spring4.x+ 版本开发纯注解的非web项目,链接如下:
本文则主要说明,如何在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、、@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;@RestControllerpublic class TimeController { private static final Logger LOGGER = LoggerFactory.getLogger(TimeController.class); public TimeController() { } @RequestMapping(value = "/time", method = { RequestMethod.GET }) public ResponseEntitygetTime() { 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中指定相关配置
WebMVC Application characterEncodingFilter org.springframework.web.filter.CharacterEncodingFilter encoding UTF-8 forceRequestEncoding false forceResponseEncoding true characterEncodingFilter /* SpringServlet org.springframework.web.servlet.DispatcherServlet contextClass org.springframework.web.context.support.AnnotationConfigWebApplicationContext contextConfigLocation com.morpheus.web.config.WebMVCConfig 1 SpringServlet / index.html index.htm index.jsp
当然,步骤9中如果是基于 servlet 3.x 规范,也可以通过类定义方式取代 web.xml 配置;
完成以上步骤开发后,使用 mvn clean package 打包部署到 tomcat 就可以正常使用服务了