初学 Spring Boot,顺便记录下学习中碰到的问题,只针对初学入门需求者。
环境:
- IDE环境:Spring Tool Suite(Version: 3.7.3.RELEASE)
- Java环境:jdk1.8.0_172(x64)
- 依赖环境:Maven(apache-maven-3.3.9)
一、开发环境配置
配置开发环境部分,可以度娘搜索,或者直接参考本博客文章《Maven 的初识、安装和配置》、《单独搭建一套 WEB 开发环境》等文章,这里不再赘述。
二、新建一个Maven项目
File -》 New -》 Other;
找到 Maven 下 Maven Project,一路 Next 按钮。。。
填上 Group Id、Artifact Id,选择 Version,修改 Package,直接 Finish。
三、查看代码目录结构
3.1 自动生成目录结构如下:
3.2 自动生成代码如下:
App.java:
- package com.menglanglang.test.springboot;
- /**
- * Hello world!
- *
- */
- public class App
- {
- public static void main( String[] args )
- {
- System.out.println( "Hello World!" );
- }
- }
AppTest.java 为 junit 自动生成的测试代码,无关紧要。
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.menglanglang</groupId>
- <artifactId>test-springboot</artifactId>
- <version>0.0.1-SNAPSHOT</version>
- <packaging>jar</packaging>
- <name>test-springboot</name>
- <url>http://maven.apache.org</url>
- <properties>
- <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
- </properties>
- <dependencies>
- <dependency>
- <groupId>junit</groupId>
- <artifactId>junit</artifactId>
- <version>3.8.1</version>
- <scope>test</scope>
- </dependency>
- </dependencies>
- </project>
四、补全代码目录结构
由于在项目开发中,有很多静态资源,一般都放在 resources 目录下,所以这里补充两个资源目录。
方法:项目 -》 右键 -》New -》 Source Folder,在 Folder Name 中输入 src/main/resources 和 src/test/resources,补全后代码目录结构如下:
如果此时运行代码:选中 App.java -》 右键 -》 Java Application。
结果为:Hello World!
五、修改代码
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.menglanglang</groupId>
- <artifactId>test-springboot</artifactId>
- <version>0.0.1-SNAPSHOT</version>
- <packaging>jar</packaging>
- <name>test-springboot</name>
- <url>http://blog.csdn.net/tzhuwb</url>
- <properties>
- <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
- </properties>
- <dependencies>
- <dependency>
- <groupId>junit</groupId>
- <artifactId>junit</artifactId>
- <version>3.8.1</version>
- <scope>test</scope>
- </dependency>
- <!-- 该依赖包提供了MVC、AOP等的依赖包 -->
- <dependency>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-starter-web</artifactId>
- <version>1.4.2.RELEASE</version>
- </dependency>
- </dependencies>
- <!-- Add Spring repositories -->
- <!-- (you don't need this if you are using a .RELEASE version) -->
- <repositories>
- <repository>
- <id>spring-snapshots</id>
- <url>http://repo.spring.io/snapshot</url>
- <snapshots>
- <enabled>true</enabled>
- </snapshots>
- </repository>
- <repository>
- <id>spring-milestones</id>
- <url>http://repo.spring.io/milestone</url>
- </repository>
- </repositories>
- <pluginRepositories>
- <pluginRepository>
- <id>spring-snapshots</id>
- <url>http://repo.spring.io/snapshot</url>
- </pluginRepository>
- <pluginRepository>
- <id>spring-milestones</id>
- <url>http://repo.spring.io/milestone</url>
- </pluginRepository>
- </pluginRepositories>
- </project>
App.java:
- package com.menglanglang.test.springboot;
- import org.springframework.boot.SpringApplication;
- import org.springframework.boot.autoconfigure.SpringBootApplication;
- /**
- * @desc 应用入口
- *
- * @author 孟郎郎
- * @blog http://blog.csdn.net/tzhuwb
- * @version 1.0
- * @date 2017年11月2日下午6:48:19
- */
- @SpringBootApplication
- public class App {
- public static void main(String[] args) {
- SpringApplication.run(App.class, args);
- System.out.println("Hello World!");
- }
- }
如果此时运行代码:选中 App.java -》 右键 -》 Java Application(或者 Spring Boot App)。
结果为:
- . ____ _ __ _ _
- /\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
- ( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
- \\/ ___)| |_)| | | | | || (_| | ) ) ) )
- ' |____| .__|_| |_|_| |_\__, | / / / /
- =========|_|==============|___/=/_/_/_/
- :: Spring Boot :: (v1.4.2.RELEASE)
- 2018-05-24 21:11:59.093 INFO 13028 --- [ restartedMain] com.menglanglang.test.springboot.App : Starting App on LangLang-PC with PID 13028 (D:\STS\sts-bundle\workspace\test-springboot\target\classes started by LangLang in D:\STS\sts-bundle\workspace\test-springboot)
- 2018-05-24 21:11:59.095 INFO 13028 --- [ restartedMain] com.menglanglang.test.springboot.App : No active profile set, falling back to default profiles: default
- 2018-05-24 21:11:59.141 INFO 13028 --- [ restartedMain] ationConfigEmbeddedWebApplicationContext : Refreshing org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext@62128c3a: startup date [Thu May 24 21:11:59 CST 2018]; root of context hierarchy
- 2018-05-24 21:12:00.653 INFO 13028 --- [ restartedMain] s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat initialized with port(s): 8080 (http)
- 2018-05-24 21:12:00.668 INFO 13028 --- [ restartedMain] o.apache.catalina.core.StandardService : Starting service Tomcat
- 2018-05-24 21:12:00.669 INFO 13028 --- [ restartedMain] org.apache.catalina.core.StandardEngine : Starting Servlet Engine: Apache Tomcat/8.5.6
- 2018-05-24 21:12:00.747 INFO 13028 --- [ost-startStop-1] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring embedded WebApplicationContext
- 2018-05-24 21:12:00.747 INFO 13028 --- [ost-startStop-1] o.s.web.context.ContextLoader : Root WebApplicationContext: initialization completed in 1610 ms
- 2018-05-24 21:12:00.884 INFO 13028 --- [ost-startStop-1] o.s.b.w.servlet.ServletRegistrationBean : Mapping servlet: 'dispatcherServlet' to [/]
- 2018-05-24 21:12:00.887 INFO 13028 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean : Mapping filter: 'characterEncodingFilter' to: [/*]
- 2018-05-24 21:12:00.888 INFO 13028 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean : Mapping filter: 'hiddenHttpMethodFilter' to: [/*]
- 2018-05-24 21:12:00.888 INFO 13028 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean : Mapping filter: 'httpPutFormContentFilter' to: [/*]
- 2018-05-24 21:12:00.888 INFO 13028 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean : Mapping filter: 'requestContextFilter' to: [/*]
- 2018-05-24 21:12:01.129 INFO 13028 --- [ restartedMain] s.w.s.m.m.a.RequestMappingHandlerAdapter : Looking for @ControllerAdvice: org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext@62128c3a: startup date [Thu May 24 21:11:59 CST 2018]; root of context hierarchy
- 2018-05-24 21:12:01.182 INFO 13028 --- [ restartedMain] 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)
- 2018-05-24 21:12:01.183 INFO 13028 --- [ restartedMain] 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)
- 2018-05-24 21:12:01.207 INFO 13028 --- [ restartedMain] o.s.w.s.handler.SimpleUrlHandlerMapping : Mapped URL path [/webjars/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
- 2018-05-24 21:12:01.208 INFO 13028 --- [ restartedMain] o.s.w.s.handler.SimpleUrlHandlerMapping : Mapped URL path [/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
- 2018-05-24 21:12:01.238 INFO 13028 --- [ restartedMain] o.s.w.s.handler.SimpleUrlHandlerMapping : Mapped URL path [/**/favicon.ico] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
- 2018-05-24 21:12:01.363 INFO 13028 --- [ restartedMain] o.s.b.d.a.OptionalLiveReloadServer : LiveReload server is running on port 35729
- 2018-05-24 21:12:01.426 INFO 13028 --- [ restartedMain] o.s.j.e.a.AnnotationMBeanExporter : Registering beans for JMX exposure on startup
- 2018-05-24 21:12:01.473 INFO 13028 --- [ restartedMain] s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat started on port(s): 8080 (http)
- 2018-05-24 21:12:01.479 INFO 13028 --- [ restartedMain] com.menglanglang.test.springboot.App : Started App in 2.81 seconds (JVM running for 3.238)
- Hello World!
到此,恭喜你,你的第一个 Spring Boot 程序已开发完毕。