发展史
从Spring3.x开始提供了Java配置方式,使用Java配置方式可以更好的理解你配置的Bean,现在我们就处于这个时代,并且Spring4.x和Spring boot都推荐使用java配置的方式。可以完全替代xml配置。
Springboot介绍
它是全新的框架,主要就是为了简化spring的搭建及应用程序的开发的。
springboot特性:
springboot-HelloWorld实现
创建Maven项目(打包方式是war),项目结构:
pom.xml中的依赖:
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion> <groupId>com.test</groupId> <artifactId>springbootTest</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>war</packaging> <build/> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.5.6.RELEASE</version> </parent> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <dependency> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-resources-plugin</artifactId> <version>2.4.3</version> </dependency> </dependencies> </project>使用的JDK是JDK7。使用的是springboot内置的tomcat
Controller类:
package test1;
import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.ResponseBody; public class Controller1 { @RequestMapping(value="/hello") @ResponseBody public String hello(){ return "hello world!"; } }主类:
package test1;
import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication public class HelloApplication { public static void main(String[] args) { SpringApplication.run(HelloApplication.class, args); } }注意:主类默认会扫描同包下或者子包下的类,所以建立的类放置的位置要注意。
解释用到的注解:
(1)Controller中使用的注解比较熟悉
(2)主类中使用的注解:@SpringBootApplication 可以启动一个类
访问url:http://localhost:8080/hello
注意:
springboot有自己使用的tomcat,端口是8080
springboot访问Controller添加后缀
前面测试的例子中Controller中没有添加后缀,访问URL也没有添加后缀。
现在添加访问URL的后缀,做法:
(1)在src/main/java中添加application.properties文件,在文件中添加:
server.servlet-path=*.html
(2)Controller不变
@RequestMapping(value="/hello")
@ResponseBody public String hello(){ return "a"; }(3)访问的URL:http://localhost:8080/hello.html中添加访问的后缀
springboot添加拦截器
创建一个拦截器类,注意还是之前说的,springboot默认扫描的是与主类同包下的类或者子包下的类,所以这个拦截器类要放在与主类同包下:
package test1;
import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.springframework.context.annotation.Configuration; import org.springframework.web.servlet.HandlerInterceptor; import org.springframework.web.servlet.ModelAndView; import org.springframework.web.servlet.config.annotation.InterceptorRegistry; import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter; @Configuration //申明这是一个配置 public class MySrpingMVCConfig extends WebMvcConfigurerAdapter{ // 自定义拦截器 @Override public void addInterceptors(InterceptorRegistry registry) { HandlerInterceptor handlerInterceptor = new HandlerInterceptor() { @Override public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception { System.out.println("自定义拦截器............"); return true; } @Override public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception { } @Override public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception { } }; registry.addInterceptor(handlerInterceptor).addPathPatterns("/**"); } }springboot返回Json格式数据返回json格式的数据有两种方式:
(1)传统springmvc的做法
package test1;
import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.ResponseBody;@Controller public class Controller1 { @RequestMapping(value="/hello") @ResponseBody public String hello(){ return "a"; } }(2)springboot提供的返回Json格式的@RestController注解
package test1;
import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.ResponseBody; import org.springframework.web.bind.annotation.RestController;@RestController public class Controller1 { @RequestMapping(value="/hello") public String hello(){ return "a"; } }使用这个注解,这个Controller中的所有方法返回的都是json格式的数据。
springBoot项目属性配置
springboot使用的配置文件是application.properties中的,这个文件放在src/main/resources下。他就是相当于我们的web项目的web.xml文件,但是这里的是更加强大的。
1.application.properties默认配置
访问的是8080端口,路径是/,没有后缀
2.配置application.properties
(1)修改URL访问的端口:添加server.port=8888
访问的URL:http://localhost:8888/hello
(2)修改URL访问的后缀:添加server.servlet-path=*.html
Controller中的@RequestMapping(value="/hello")不变,访问的URL:localhost:8888/hello.html
(3)添加访问Controller的路径:
第一种方式:直接在Controller中添加:@RequestMapping(value="/context/hello")
第二种方式:在application.properties中添加server.context-path=/context,Controller中还是原来的@RequestMapping(value="/hello")
springboot自定义属性
先看一下pom.xml中使用的springboot版本:
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion> <groupId>com.test</groupId> <artifactId>springbootTest</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>war</packaging> <build/> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.5.6.RELEASE</version> </parent> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <dependency> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-resources-plugin</artifactId> <version>2.4.3</version> </dependency> </dependencies> </project>我使用的是JDK7,使用JDK8会报错。
当我们想要使用key-value的时候,有两种方式:
(1)可以把属性放在application.properties中
普通key-value的使用:
比如在application.properties中添加一个key-value:test=spring boot \u4F60\u5927\u7237(自动编码的)(spring boot 你大爷)。注意这个文件application.properties的编码不是utf-8,不要改变这个。
在Controller中的使用:
package test1;
import org.springframework.beans.factory.annotation.Value; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; @RestController public class Controller1 { @Value("${test}") private String test; @RequestMapping(value="/hello") public String hello(){ return "a:"+test; } }带有前缀的key-value的使用:
比如在application.properties中添加一个key-value:mysql.name=hello。
在Controller中的使用:
package test1;
import org.springframework.beans.factory.annotation.Value; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; @RestController public class Controller1 { @Value("${ mysql.name}") private String test; @RequestMapping(value="/hello") public String hello(){ return "a:"+test; } }(2)也可以放在一个配置类(ConfigrationProperties属性)中,实际上这个类被扫描之后,里面的key-value还是被放在application.properties中的
创建配置类(注意放在src/main/java下,因为springboot默认扫描的是与主类同包或子包下):
package test1;
import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.stereotype.Component; @Component @ConfigurationProperties(prefix="mysql")//属性前缀 public class MysqlProperties { private String name="myname"; private String age="myage"; public String getName() { return name; } public void setName(String name) { this.name = name; } public String getAge() { return age; } public void setAge(String age) { this.age = age; } }Controller使用:package test1;
import javax.annotation.Resource; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; @RestController public class Controller1 { @Resource private MysqlProperties test; @RequestMapping(value="/hello") public String hello(){ return "name:"+test.getName()+"</br>"+"age:"+test.getAge(); } } application.properties配置文件中不用做什么配置。springboot的freemark支持(模板)
(1)pom.xml中添加freemarker支持
<dependency>
<groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-freemarker</artifactId> </dependency>(2)Controller使用
package test1;
import javax.annotation.Resource; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import org.springframework.web.servlet.ModelAndView; @RestController public class Controller1 { @RequestMapping(value="/hello") public ModelAndView hello(){ ModelAndView mode = new ModelAndView(); mode.addObject("message","spring boot 你大爷"); mode.setViewName("a");//页面的名字 return mode; } } (3)application.properties文件server.context-path=/context(不变,这里是URL访问Controller的路径的配置,与freemarker无关)
(4)页面
在src/main/resources下创建templates文件夹(springboot可以识别这个名字的文件夹)
首先创建一个a.html,里面添加的内容是:
然后将a.html文件改名为a.ftl。
(5)访问的URL:http://localhost:8080/context/hello