java web spring 注解,[Spring4.x]基于spring4.x纯注解的Web工程搭建

在前文中已经说明了如何基于 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、@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 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中指定相关配置

/p>

"-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"

"http://java.sun.com/dtd/web-app_2_3.dtd" >

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 就可以正常使用服务了

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值