1. 开箱即用
整合常用框架,内嵌tomcat ,直接通过main函数启动. 可打包可执行jar.
我是官网链接
可以看到间接引入的依赖
2. 约定大于配置
基于此省去了 web.xml , spring-config.xml 等繁琐的配置.
2.1 静态文件
如 js代码, 图片等, 默认放在与源文件目录src
同级的static/
下即可. 如hello.html放在projectDir/static/
下, 对应的URL就是http://ip:port/hello.html
.
2.2 日志配置
可以写到src/main/resources/log4j.properties
中.
3. websocket功能
基础的websocket类编写可参见 websocket (html5新规范)
在spring boot中使用,需要以下3步.
- 需要额外引入依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-websocket</artifactId>
<version>${spring-boot.version}</version>
</dependency>
- 在main()函数所在类上添加
@EnableWebSocket
注解. - 新写一个websocket的配置类
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.socket.server.standard.ServerEndpointExporter;
@Configuration
public class EndpointConfig {
@Bean
//MyWebSocket 为自己的WebSocket类.
public MyWebSocket serverendpoint() {
return new MyWebSocket();
}
@Bean
public ServerEndpointExporter endpointExporter() {
return new ServerEndpointExporter();
}
}
4.filter功能
与main() 函数所在类同级的配置类.
@Configuration
public class AConfig {
@Bean
public CrossDomainFilter crossDomainFilter() {
return new CrossDomainFilter();
}
}
filter 实现见下:
@WebFilter(urlPatterns = "/2017", filterName = "crossDomainFilter")
public class CrossDomainFilter implements Filter {...}
5. 启动日志
贴一段启动日志感受一下.
启动后, 访问localhost:8080
即可出现”Hello World!” 报文.
. ____ _ __ _ _
/\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
\\/ ___)| |_)| | | | | || (_| | ) ) ) )
' |____| .__|_| |_|_| |_\__, | / / / /
=========|_|==============|___/=/_/_/_/
:: Spring Boot :: (v1.5.1.RELEASE)
2017-02-17 14:02:15.891 INFO 5172 --- [ main] c.y.springbootdemo.SampleController : Starting SampleController on MININT-R287J3I with PID 5172 (C:\Users\yichu.dyc\workspace\csdn\SpringBootDemo\target\classes started by yichu.dyc in C:\Users\yichu.dyc\workspace\csdn\SpringBootDemo)
2017-02-17 14:02:15.899 INFO 5172 --- [ main] c.y.springbootdemo.SampleController : No active profile set, falling back to default profiles: default
2017-02-17 14:02:15.976 INFO 5172 --- [ main] ationConfigEmbeddedWebApplicationContext : Refreshing org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext@43d7741f: startup date [Fri Feb 17 14:02:15 CST 2017]; root of context hierarchy
2017-02-17 14:02:17.546 INFO 5172 --- [ main] trationDelegate$BeanPostProcessorChecker : Bean 'org.springframework.boot.autoconfigure.validation.ValidationAutoConfiguration' of type [class org.springframework.boot.autoconfigure.validation.ValidationAutoConfiguration] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying)
2017-02-17 14:02:17.661 INFO 5172 --- [ main] trationDelegate$BeanPostProcessorChecker : Bean 'validator' of type [class org.springframework.validation.beanvalidation.LocalValidatorFactoryBean] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying)
2017-02-17 14:02:18.602 INFO 5172 --- [ main] s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat initialized with port(s): 8080 (http)
2017-02-17 14:02:18.631 INFO 5172 --- [ main] o.apache.catalina.core.StandardService : Starting service Tomcat
2017-02-17 14:02:18.633 INFO 5172 --- [ main] org.apache.catalina.core.StandardEngine : Starting Servlet Engine: Apache Tomcat/8.5.11
2017-02-17 14:02:18.810 INFO 5172 --- [ost-startStop-1] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring embedded WebApplicationContext
2017-02-17 14:02:18.810 INFO 5172 --- [ost-startStop-1] o.s.web.context.ContextLoader : Root WebApplicationContext: initialization completed in 2840 ms
2017-02-17 14:02:19.082 INFO 5172 --- [ost-startStop-1] o.s.b.w.servlet.ServletRegistrationBean : Mapping servlet: 'dispatcherServlet' to [/]
2017-02-17 14:02:19.088 INFO 5172 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean : Mapping filter: 'characterEncodingFilter' to: [/*]
2017-02-17 14:02:19.088 INFO 5172 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean : Mapping filter: 'hiddenHttpMethodFilter' to: [/*]
2017-02-17 14:02:19.089 INFO 5172 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean : Mapping filter: 'httpPutFormContentFilter' to: [/*]
2017-02-17 14:02:19.089 INFO 5172 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean : Mapping filter: 'requestContextFilter' to: [/*]
2017-02-17 14:02:19.506 INFO 5172 --- [ main] s.w.s.m.m.a.RequestMappingHandlerAdapter : Looking for @ControllerAdvice: org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext@43d7741f: startup date [Fri Feb 17 14:02:15 CST 2017]; root of context hierarchy
2017-02-17 14:02:19.609 INFO 5172 --- [ main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/]}" onto java.lang.String com.yichudu.springbootdemo.SampleController.home()
2017-02-17 14:02:19.613 INFO 5172 --- [ 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-02-17 14:02:19.615 INFO 5172 --- [ 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-02-17 14:02:19.654 INFO 5172 --- [ main] o.s.w.s.handler.SimpleUrlHandlerMapping : Mapped URL path [/webjars/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2017-02-17 14:02:19.654 INFO 5172 --- [ main] o.s.w.s.handler.SimpleUrlHandlerMapping : Mapped URL path [/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2017-02-17 14:02:19.708 INFO 5172 --- [ main] o.s.w.s.handler.SimpleUrlHandlerMapping : Mapped URL path [/**/favicon.ico] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2017-02-17 14:02:19.909 INFO 5172 --- [ main] o.s.j.e.a.AnnotationMBeanExporter : Registering beans for JMX exposure on startup
2017-02-17 14:02:20.030 INFO 5172 --- [ main] s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat started on port(s): 8080 (http)
2017-02-17 14:02:20.037 INFO 5172 --- [ main] c.y.springbootdemo.SampleController : Started SampleController in 4.639 seconds (JVM running for 5.304)
2017-02-17 14:06:17.386 INFO 5172 --- [nio-8080-exec-1] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring FrameworkServlet 'dispatcherServlet'
2017-02-17 14:06:17.387 INFO 5172 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet : FrameworkServlet 'dispatcherServlet': initialization started
2017-02-17 14:06:17.434 INFO 5172 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet : FrameworkServlet 'dispatcherServlet': initialization completed in 47 ms