Spring Boot 之Spring MVC
搭建一个web应用
引入Maven依赖
<!--
这里使用官方最新的版本,官方推荐建议继承默认的springboot配置
继承后,项目的编译级别会自动变为jdk1.6版本。
-->
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.3.3.RELEASE</version>
</parent>
<!--
添加一个web应用所需要的依赖,它会自动引入tomcat内置容器、log4j、jackson等通用的依赖
-->
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
我们可以通过 mvn dependency:tree 来查看项目的依赖结构树
Controller
package com.panlingxiao.springboot.web.controller;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
/**
* RestController是Spring 4.0推出的新特性,
* 使用其标注表示当前类为一个@Controller,并且
* 使用@RequestMapping所标注的方法的返回值默认会被认为使用
* 了@ResponseBody进行标注,因此不再使用视图解析的方式进行处理,
* 而是将内容通过HTTP响应体返回给客户端。
*/
@RestController
public class HelloController {
/**
* spring boot会自动读取application.properties,
* 并且将其作为系统参数进行注入,用户也可以在启动应用的时候
* 通过-Dname=xxx来手动注入,手动注入会覆盖配置文件中的参数
* 如果没有指定值,那么name的默认值就是World。
*/
@Value("${name:World}")
private String name;
/**
* 由于使用了@RestControlelr,因此无需在使用@ResponseBody来标注返回的结果
*/
@RequestMapping("/hello")
public String sayHello(){
return String.format("hello:%s", name);
}
}
@RestController,可以直接将值response出去,不需要@ResponseBody
Spring Boot启动类
package com.panlingxiao.springboot.web;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
//如果用了Spring Boot,一定要加这个
@SpringBootApplication
public class WebApp{
/*
* 直接用Main方法运行,就能启动Spring Boot
*/
public static void main(String[] args) {
SpringApplication.run(WebApp.class, args);
}
}
启动后结果
启动后,默认端口是8080,没有设置上下文的情况下应该请求下面URL
http://127.0.0.1:8080/hello
理解上面启动类中的注释:@SpringBootApplication
该注解其实又是由其他3个注解来标识,它们分别是@Configuration、@EnableAutoConfiguration、@ComponentScan。
@Configuration:
表示该类是配置文件,可以定义Bean,相当于xml的配置
/**
* 使用@Configuration标识
* 在当前类中通过@Bean标注的方式
* 之后都由Spring的AppplicationContext来进行调用
* 从而生成Bean对象。
*/
@Configuration
public class AppConfig {
@Bean
public User getUser(){
User user = new User();
return user;
}
}
public class App {
public static void main(String[] args) {
/*
* 将@Configuraion所标识的类通过Spring的上下问进行加载,从而实现Bean的创建。
*/
AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext();
ctx.register(AppConfig.class);
ctx.refresh();
User user = ctx.getBean("user", User.class);
String value = user.sayHello();
}
}
@EnableAutoConfiguration:
该注解是让Spring根据当前classpath中所存在的类,自动推断出当前运行的环境(判断是否一个Web应用还是一个普通的Java应用),然后根据具体的环境去创建对应的bean,将其添加的Spring的ApplicationContext中。
比如:我们在classpath中添加了tomcat-embedded.jar,因此它会去自动帮助我们自动地创建一个EmbeddedServletContainerFactory。如果没有这个对象,那么我们的Tomcat是无法启动的。我们将WebApp改成如下结果:
//不再设置EnableAutoConfiguration会导致应用启动失败
@Configuration
@ComponentScan
public class WebApp {
public static void main(String[] args) {
SpringApplication.run(WebApp.class, args);
}
}
@ComponentScan:
该注解就是让Spring容器自动根据指定的包以及子包中进行组件的查找然后创建,如果没有指定具体到哪个包中进行查找,那么就以当前使用该注解的类所在的包作为查找的根,进行查找。即在WebApp这个类所在的包及其子包中进行查找。
注意:这里我将HelloController定义在了WebApp所在的包的子包中,如果它们所在的包不具有上面所描述的查找关系,那么Controller是不会被创建的,即使应用可以启动,但是最终响应的结果将是404。
我们在main方法中使用了SpringApplication这个类完成了应用的启动,该类是一个Spring应用启动的引导工具类,它底层维护着Spring容器。
参考:
http://www.jianshu.com/u/dc9be3b69337