SpringBoot 学习笔记
最近难得又轻松了点,也就有时间看一下一直想看的SpringBoot和Dubbo+zk,考虑了下,先从SpringBoot开始:
上官方文档:https://docs.spring.io/spring-boot/docs/2.0.0.M1/reference/htmlsingle/
这是我比较喜欢的类型
这里选择了一个比较适中的版本1.5.0.RELEASE
By default, Spring Boot 1.5.0.RELEASE requires Java 7 andSpring Framework 4.3.6.RELEASE or above. You can use Spring Boot with Java 6 with someadditional configuration. SeeSection 84.11, “How to use Java 6” for more details. Explicitbuild support is provided forMaven (3.2+), and Gradle 2 (2.9 or later) and 3.
我的IDE是MyEclipse2014,不推荐使用,感觉太重了,然后环境为:
JDK8
Tomcat8
apache-maven-3.3.9
1、首先我新建了一个项目,天生带着小红叉,但是用tomcat跑了下,正常。
2、pom中引入相关类库:
<?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.example</groupId>
<artifactId>myproject</artifactId>
<version>0.0.1-SNAPSHOT</version>
<!-- Inherit defaults from Spring Boot -->
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.0.RELEASE</version>
</parent>
<!-- Add typical dependencies for a web application -->
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
<!-- Package as an executable jar -->
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
3、官网这一步让安装 Boot CLI,应该是类似终端的东西:spring-boot-cli-1.5.0.RELEASE-bin.zip
下完之后打开闪退,按照install。txt的提示,将下载的cli配置到环境变量
4、又让安装SDKMAN,包含 Groovy and the Spring Boot CLI.不装了,骗子
===============================================================================================================================
5、随便找个博客(以下两个类抄自右边博客):http://blog.csdn.net/lxhjh/article/details/51711148
创建启动类(http://blog.csdn.net/lxhjh/article/details/51711148):
@SpringBootApplication
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
创建控制层类(http://blog.csdn.net/lxhjh/article/details/51711148):
@RestController
@EnableAutoConfiguration
public class Example {
@RequestMapping("/")
String home() {
return "Hello World!";
}
@RequestMapping("/hello/{myName}")
String index(@PathVariable String myName) {
return "Hello "+myName+"!!!";
}
}
运行
Application 类中的main方法:》》一下是启动日志:
. ____ _ __ _ _
/\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
\\/ ___)| |_)| | | | | || (_| | ) ) ) )
' |____| .__|_| |_|_| |_\__, | / / / /
=========|_|==============|___/=/_/_/_/
:: Spring Boot :: (v1.5.0.RELEASE)
2017-12-12 18:00:27.019 INFO 4036 --- [ main] Application.Application : Starting Application on PC-20161217LDRA with PID 4036 (E:\WorkspaceTest\TestBoot\target\classes started by Administrator in E:\WorkspaceTest\TestBoot)
2017-12-12 18:00:27.019 INFO 4036 --- [ main] Application.Application : No active profile set, falling back to default profiles: default
2017-12-12 18:00:27.079 INFO 4036 --- [ main] ationConfigEmbeddedWebApplicationContext : Refreshing org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext@21e360a: startup date [Tue Dec 12 18:00:27 CST 2017]; root of context hierarchy
2017-12-12 18:00:28.045 INFO 4036 --- [ 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-12-12 18:00:28.126 INFO 4036 --- [ 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-12-12 18:00:28.536 INFO 4036 --- [ main] s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat initialized with port(s): 8080 (http)
2017-12-12 18:00:28.556 INFO 4036 --- [ main] o.apache.catalina.core.StandardService : Starting service Tomcat
2017-12-12 18:00:28.556 INFO 4036 --- [ main] org.apache.catalina.core.StandardEngine : Starting Servlet Engine: Apache Tomcat/8.0.47
2017-12-12 18:00:28.838 INFO 4036 --- [ost-startStop-1] org.apache.jasper.servlet.TldScanner : At least one JAR was scanned for TLDs yet contained no TLDs. Enable debug logging for this logger for a complete list of JARs that were scanned but no TLDs were found in them. Skipping unneeded JARs during scanning can improve startup time and JSP compilation time.
2017-12-12 18:00:28.848 INFO 4036 --- [ost-startStop-1] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring embedded WebApplicationContext
2017-12-12 18:00:28.848 INFO 4036 --- [ost-startStop-1] o.s.web.context.ContextLoader : Root WebApplicationContext: initialization completed in 1769 ms
2017-12-12 18:00:29.010 INFO 4036 --- [ost-startStop-1] o.s.b.w.servlet.ServletRegistrationBean : Mapping servlet: 'dispatcherServlet' to [/]
2017-12-12 18:00:29.020 INFO 4036 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean : Mapping filter: 'characterEncodingFilter' to: [/*]
2017-12-12 18:00:29.020 INFO 4036 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean : Mapping filter: 'hiddenHttpMethodFilter' to: [/*]
2017-12-12 18:00:29.020 INFO 4036 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean : Mapping filter: 'httpPutFormContentFilter' to: [/*]
2017-12-12 18:00:29.020 INFO 4036 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean : Mapping filter: 'requestContextFilter' to: [/*]
2017-12-12 18:00:29.406 INFO 4036 --- [ main] s.w.s.m.m.a.RequestMappingHandlerAdapter : Looking for @ControllerAdvice: org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext@21e360a: startup date [Tue Dec 12 18:00:27 CST 2017]; root of context hierarchy
2017-12-12 18:00:29.477 INFO 4036 --- [ main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/hello/{myName}]}" onto java.lang.String Application.Example.index(java.lang.String)
2017-12-12 18:00:29.477 INFO 4036 --- [ main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/]}" onto java.lang.String Application.Example.home()
2017-12-12 18:00:29.487 INFO 4036 --- [ 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-12-12 18:00:29.487 INFO 4036 --- [ 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-12-12 18:00:29.527 INFO 4036 --- [ main] o.s.w.s.handler.SimpleUrlHandlerMapping : Mapped URL path [/webjars/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2017-12-12 18:00:29.527 INFO 4036 --- [ main] o.s.w.s.handler.SimpleUrlHandlerMapping : Mapped URL path [/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2017-12-12 18:00:29.567 INFO 4036 --- [ main] o.s.w.s.handler.SimpleUrlHandlerMapping : Mapped URL path [/**/favicon.ico] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2017-12-12 18:00:29.847 INFO 4036 --- [ main] o.s.j.e.a.AnnotationMBeanExporter : Registering beans for JMX exposure on startup
2017-12-12 18:00:29.967 INFO 4036 --- [ main] s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat started on port(s): 8080 (http)
2017-12-12 18:00:29.972 INFO 4036 --- [ main] Application.Application : Started Application in 3.252 seconds (JVM running for 3.703)
2017-12-12 18:01:09.472 INFO 4036 --- [nio-8080-exec-1] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring FrameworkServlet 'dispatcherServlet'
2017-12-12 18:01:09.472 INFO 4036 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet : FrameworkServlet 'dispatcherServlet': initialization started
2017-12-12 18:01:09.494 INFO 4036 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet : FrameworkServlet 'dispatcherServlet': initialization completed in 22 ms
测试:
http://localhost:8080/hello/%E4%BD%BF%E7%94%A8controller
浏览器显示:Hello 使用controller!!!
================================================================================================================================
6、好了,继续看官方文档
下面官方给出了这样一个代码,其实和上面一样的,但是合在一起就很别扭,没有上面那个人分开写更清晰:
@RestController
@EnableAutoConfiguration
public class Example2 {
@RequestMapping("/e2")
String home() {
return "Hello World!";
}
public static void main(String[] args) throws Exception {
SpringApplication.run(Example.class, args);
}
}
7、然后介绍了两个基础注解,以及集成的mvn给项目打包
8、总体看了几眼文档,看饿了,先回家吃饭