有过spring的学习经历的同学来说,各种配置让人眼花缭乱实在不知道怎么办,为什么这么配置就更不知道了,还有日志的配置(logback等),稍微不注意就错了,各种google,百度,想死的心都有;就算熟练使用,有多年开发经验,大多数人离开资料也会一样不知道怎么配置。
好在现在有了springboot,糟糕的配置可以放一边了,我们完全可以全心体验spring高效开发带来的好处。
简单的描述
引用http://projects.spring.io/spring-boot对spring-boot的描述:
Spring Boot makes it easy to create stand-alone, production-grade Spring based Applications that you can "just run". We take an opinionated view of the Spring platform and third-party libraries so you can get started with minimum fuss. Most Spring Boot applications need very little Spring configuration.
spring-boot能很轻松提供创建独立、生产级的spring应用程序,能让程序"just run",仅仅是只需要1点点配置。
再引用http://projects.spring.io/spring-boot对spring-boot特性的描述:
- Create stand-alone Spring applications
- Embed Tomcat, Jetty or Undertow directly (no need to deploy WAR files)
- Provide opinionated 'starter' POMs to simplify your Maven configuration
- Automatically configure Spring whenever possible
- Provide production-ready features such as metrics, health checks and externalized configuration
- Absolutely no code generation and no requirement for XML configuration
大意是:
创建独立的Spring applications
能够使用内嵌的Tomcat, Jetty or Undertow,不需要部署war
提供starter pom来简化maven配置
自动配置Spring
提供一些生产环境的特性,比如metrics, health checks and externalized configuration
初识
一个简单的demo:
pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<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.zxp.spring.boot</groupId>
<artifactId>springBoot</artifactId>
<version>1.0-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<version>1.4.1.RELEASE</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<version>1.4.1.RELEASE</version>
</plugin>
</plugins>
</build>
</project>
程序:
package com.zxp.spring.boot; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; /** * Created by lucene on 2017/7/1. */ @SpringBootApplication @RestController public class SpringBootDemoApplication { @RequestMapping("/springBootDemo") public String greeting() { return "Hello World!"; } public static void main(String[] args) { SpringApplication.run(SpringBootDemoApplication.class, args); } }
程序启动信息
C:\Java\jdk1.7.0_75\bin\java -Didea.launcher.port=7532 "-Didea.launcher.bin.path=D:\IntelliJ IDEA 2016.1.3\bin" -Dfile.encoding=UTF-8 -classpath "C:\Java\jdk1.7.0_75\jre\lib\charsets.jar;C:\Java\jdk1.7.0_75\jre\lib\deploy.jar;C:\Java\jdk1.7.0_75\jre\lib\ext\access-bridge-64.jar;C:\Java\jdk1.7.0_75\jre\lib\ext\dnsns.jar;C:\Java\jdk1.7.0_75\jre\lib\ext\jaccess.jar;C:\Java\jdk1.7.0_75\jre\lib\ext\localedata.jar;C:\Java\jdk1.7.0_75\jre\lib\ext\sunec.jar;C:\Java\jdk1.7.0_75\jre\lib\ext\sunjce_provider.jar;C:\Java\jdk1.7.0_75\jre\lib\ext\sunmscapi.jar;C:\Java\jdk1.7.0_75\jre\lib\ext\zipfs.jar;C:\Java\jdk1.7.0_75\jre\lib\javaws.jar;C:\Java\jdk1.7.0_75\jre\lib\jce.jar;C:\Java\jdk1.7.0_75\jre\lib\jfr.jar;C:\Java\jdk1.7.0_75\jre\lib\jfxrt.jar;C:\Java\jdk1.7.0_75\jre\lib\jsse.jar;C:\Java\jdk1.7.0_75\jre\lib\management-agent.jar;C:\Java\jdk1.7.0_75\jre\lib\plugin.jar;C:\Java\jdk1.7.0_75\jre\lib\resources.jar;C:\Java\jdk1.7.0_75\jre\lib\rt.jar;D:\SpringWorkSpace\springBootDemo\target\classes;C:\Users\lucene\.m2\repository\org\springframework\boot\spring-boot-starter-web\1.4.1.RELEASE\spring-boot-starter-web-1.4.1.RELEASE.jar;C:\Users\lucene\.m2\repository\org\springframework\boot\spring-boot-starter\1.4.1.RELEASE\spring-boot-starter-1.4.1.RELEASE.jar;C:\Users\lucene\.m2\repository\org\springframework\boot\spring-boot\1.4.1.RELEASE\spring-boot-1.4.1.RELEASE.jar;C:\Users\lucene\.m2\repository\org\springframework\boot\spring-boot-autoconfigure\1.4.1.RELEASE\spring-boot-autoconfigure-1.4.1.RELEASE.jar;C:\Users\lucene\.m2\repository\org\springframework\boot\spring-boot-starter-logging\1.4.1.RELEASE\spring-boot-starter-logging-1.4.1.RELEASE.jar;C:\Users\lucene\.m2\repository\ch\qos\logback\logback-classic\1.1.7\logback-classic-1.1.7.jar;C:\Users\lucene\.m2\repository\ch\qos\logback\logback-core\1.1.7\logback-core-1.1.7.jar;C:\Users\lucene\.m2\repository\org\slf4j\slf4j-api\1.7.20\slf4j-api-1.7.20.jar;C:\Users\lucene\.m2\repository\org\slf4j\jcl-over-slf4j\1.7.21\jcl-over-slf4j-1.7.21.jar;C:\Users\lucene\.m2\repository\org\slf4j\jul-to-slf4j\1.7.21\jul-to-slf4j-1.7.21.jar;C:\Users\lucene\.m2\repository\org\slf4j\log4j-over-slf4j\1.7.21\log4j-over-slf4j-1.7.21.jar;C:\Users\lucene\.m2\repository\org\springframework\spring-core\4.3.3.RELEASE\spring-core-4.3.3.RELEASE.jar;C:\Users\lucene\.m2\repository\org\yaml\snakeyaml\1.17\snakeyaml-1.17.jar;C:\Users\lucene\.m2\repository\org\springframework\boot\spring-boot-starter-tomcat\1.4.1.RELEASE\spring-boot-starter-tomcat-1.4.1.RELEASE.jar;C:\Users\lucene\.m2\repository\org\apache\tomcat\embed\tomcat-embed-core\8.5.5\tomcat-embed-core-8.5.5.jar;C:\Users\lucene\.m2\repository\org\apache\tomcat\embed\tomcat-embed-el\8.5.5\tomcat-embed-el-8.5.5.jar;C:\Users\lucene\.m2\repository\org\apache\tomcat\embed\tomcat-embed-websocket\8.5.5\tomcat-embed-websocket-8.5.5.jar;C:\Users\lucene\.m2\repository\org\hibernate\hibernate-validator\5.2.4.Final\hibernate-validator-5.2.4.Final.jar;C:\Users\lucene\.m2\repository\javax\validation\validation-api\1.1.0.Final\validation-api-1.1.0.Final.jar;C:\Users\lucene\.m2\repository\org\jboss\logging\jboss-logging\3.2.1.Final\jboss-logging-3.2.1.Final.jar;C:\Users\lucene\.m2\repository\com\fasterxml\classmate\1.1.0\classmate-1.1.0.jar;C:\Users\lucene\.m2\repository\com\fasterxml\jackson\core\jackson-databind\2.8.3\jackson-databind-2.8.3.jar;C:\Users\lucene\.m2\repository\com\fasterxml\jackson\core\jackson-annotations\2.8.0\jackson-annotations-2.8.0.jar;C:\Users\lucene\.m2\repository\com\fasterxml\jackson\core\jackson-core\2.8.3\jackson-core-2.8.3.jar;C:\Users\lucene\.m2\repository\org\springframework\spring-web\4.3.3.RELEASE\spring-web-4.3.3.RELEASE.jar;C:\Users\lucene\.m2\repository\org\springframework\spring-aop\4.3.3.RELEASE\spring-aop-4.3.3.RELEASE.jar;C:\Users\lucene\.m2\repository\org\springframework\spring-beans\4.3.3.RELEASE\spring-beans-4.3.3.RELEASE.jar;C:\Users\lucene\.m2\repository\org\springframework\spring-context\4.3.3.RELEASE\spring-context-4.3.3.RELEASE.jar;C:\Users\lucene\.m2\repository\org\springframework\spring-webmvc\4.3.3.RELEASE\spring-webmvc-4.3.3.RELEASE.jar;C:\Users\lucene\.m2\repository\org\springframework\spring-expression\4.3.3.RELEASE\spring-expression-4.3.3.RELEASE.jar;D:\IntelliJ IDEA 2016.1.3\lib\idea_rt.jar" com.intellij.rt.execution.application.AppMain com.zxp.spring.boot.SpringBootDemoApplication
. ____ _ __ _ _
/\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
\\/ ___)| |_)| | | | | || (_| | ) ) ) )
' |____| .__|_| |_|_| |_\__, | / / / /
=========|_|==============|___/=/_/_/_/
:: Spring Boot :: (v1.4.1.RELEASE)
2017-07-01 18:36:25.877 INFO 17268 --- [ main] c.z.s.boot.SpringBootDemoApplication : Starting SpringBootDemoApplication on zxp with PID 17268 (D:\SpringWorkSpace\springBootDemo\target\classes started by lucene in D:\SpringWorkSpace\springBootDemo)
2017-07-01 18:36:25.886 INFO 17268 --- [ main] c.z.s.boot.SpringBootDemoApplication : No active profile set, falling back to default profiles: default
2017-07-01 18:36:26.048 INFO 17268 --- [ main] ationConfigEmbeddedWebApplicationContext : Refreshing org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext@5a2a7673: startup date [Sat Jul 01 18:36:26 CST 2017]; root of context hierarchy
2017-07-01 18:36:28.826 INFO 17268 --- [ main] s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat initialized with port(s): 8080 (http)
2017-07-01 18:36:28.855 INFO 17268 --- [ main] o.apache.catalina.core.StandardService : Starting service Tomcat
2017-07-01 18:36:28.857 INFO 17268 --- [ main] org.apache.catalina.core.StandardEngine : Starting Servlet Engine: Apache Tomcat/8.5.5
2017-07-01 18:36:29.015 INFO 17268 --- [ost-startStop-1] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring embedded WebApplicationContext
2017-07-01 18:36:29.015 INFO 17268 --- [ost-startStop-1] o.s.web.context.ContextLoader : Root WebApplicationContext: initialization completed in 2972 ms
2017-07-01 18:36:29.274 INFO 17268 --- [ost-startStop-1] o.s.b.w.servlet.ServletRegistrationBean : Mapping servlet: 'dispatcherServlet' to [/]
2017-07-01 18:36:29.280 INFO 17268 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean : Mapping filter: 'characterEncodingFilter' to: [/*]
2017-07-01 18:36:29.280 INFO 17268 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean : Mapping filter: 'hiddenHttpMethodFilter' to: [/*]
2017-07-01 18:36:29.281 INFO 17268 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean : Mapping filter: 'httpPutFormContentFilter' to: [/*]
2017-07-01 18:36:29.281 INFO 17268 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean : Mapping filter: 'requestContextFilter' to: [/*]
2017-07-01 18:36:29.769 INFO 17268 --- [ main] s.w.s.m.m.a.RequestMappingHandlerAdapter : Looking for @ControllerAdvice: org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext@5a2a7673: startup date [Sat Jul 01 18:36:26 CST 2017]; root of context hierarchy
2017-07-01 18:36:29.883 INFO 17268 --- [ main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/]}" onto public java.lang.String com.zxp.spring.boot.SpringBootDemoApplication.greeting()
2017-07-01 18:36:29.888 INFO 17268 --- [ main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/error]}" onto public org.springframework.http.ResponseEntity<java.util.Map<java.lang.String, java.lang.Object>> org.springframework.boot.autoconfigure.web.BasicErrorController.error(javax.servlet.http.HttpServletRequest)
2017-07-01 18:36:29.888 INFO 17268 --- [ main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/error],produces=[text/html]}" onto public org.springframework.web.servlet.ModelAndView org.springframework.boot.autoconfigure.web.BasicErrorController.errorHtml(javax.servlet.http.HttpServletRequest,javax.servlet.http.HttpServletResponse)
2017-07-01 18:36:29.948 INFO 17268 --- [ main] o.s.w.s.handler.SimpleUrlHandlerMapping : Mapped URL path [/webjars/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2017-07-01 18:36:29.948 INFO 17268 --- [ main] o.s.w.s.handler.SimpleUrlHandlerMapping : Mapped URL path [/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2017-07-01 18:36:30.013 INFO 17268 --- [ main] o.s.w.s.handler.SimpleUrlHandlerMapping : Mapped URL path [/**/favicon.ico] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2017-07-01 18:36:30.394 INFO 17268 --- [ main] o.s.j.e.a.AnnotationMBeanExporter : Registering beans for JMX exposure on startup
2017-07-01 18:36:30.561 INFO 17268 --- [ main] s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat started on port(s): 8080 (http)
2017-07-01 18:36:30.569 INFO 17268 --- [ main] c.z.s.boot.SpringBootDemoApplication : Started SpringBootDemoApplication in 6.292 seconds (JVM running for 7.138)
2017-07-01 18:37:45.375 INFO 17268 --- [nio-8080-exec-1] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring FrameworkServlet 'dispatcherServlet'
2017-07-01 18:37:45.375 INFO 17268 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet : FrameworkServlet 'dispatcherServlet': initialization started
2017-07-01 18:37:45.396 INFO 17268 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet : FrameworkServlet 'dispatcherServlet': initialization completed in 21 ms
浏览器访问结果:http://localhost:8080/springBootDemo
返回结果:Hello World!
稍稍深入
@RestController: is a stereotype annotation that combines @ResponseBody and @Controller(其实相当于@ResponseBody + @Controller)
@SpringBootApplication:
The @SpringBootApplication annotation is equivalent to using @Configuration, @EnableAutoConfiguration and @ComponentScan with
their default attributes
@RequestMapping:和spring mvc中意义一致,如果标识资源的URI
@ConfigurationProperties:读取配置文件(个人觉得还是比较麻烦的,使用@Value更为方便)
。。。
如:配置文件common.properties,内容如下:
--------------------------------------------------------------------------------------
password.name=lucene password.password=1222222
--------------------------------------------------------------------------------------
package com.zxp.spring.boot.properties;
import org.springframework.boot.context.properties.ConfigurationProperties;
/**
* Created by lucene on 2017/7/2.
*/
@ConfigurationProperties(prefix = "password",locations = "classpath:common.properties")
public class Password {
private String name;
private String password;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}
package com.zxp.spring.boot;
import com.fasterxml.jackson.core.json.UTF8DataInputJsonParser;
import com.zxp.spring.boot.properties.Password;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
/**
* Created by lucene on 2017/7/1.
*/
@SpringBootApplication
@RestController
@EnableConfigurationProperties({Password.class})
public class SpringBootDemoApplication {
@Autowired
Password password;
@RequestMapping("/springBootDemo")
public String greeting() {
return "Hello World!---" + password.getName();
}
public static void main(String[] args) {
SpringApplication.run(SpringBootDemoApplication.class, args);
}
}
浏览器访问http://localhost:8080/springBootDemo结果:Hello World!---lucene
搭建一个应用程序
目的:搭建一个访问数据库并集成velocity的web程序
todo