第一個spring boot工程
阿新 • • 發佈:2018-01-04
lan 1.0 figure app 通過 分享 package plugins stc
參考。
1. 碰到的問題:
-出現bind:address already in use是因為當前項目正在運行,停掉當前項目即可。cmd中命令 netstat -nao 查看所有占用的端口及PID號,在任務管理器中將相應PID進程停掉。
-@SpringBootApplication註解
官方的代碼中controller和main函數寫在一個類中,因此可以不需要SpringBootApplication這個註解,寫在不同的類中就需要這個註解,否則掃描不到controller。通過源碼可以知道@SpringBootApplication = @SpringBootConfiguration + @EnableAutoConfiguration + @ComponentScan,它具有ComponentScan的作用,因此不需要像spring程序那樣顯式指定包掃描. 參考
2.工程結構:
3. 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.pkyou.Sample</groupId> <artifactId>HelloBoco</artifactId> <version>1.0.0</version> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.5.8.RELEASE</version> </parent> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>
4. main:
package com.pkyou.Sample.Controller; import org.springframework.boot.SpringApplication; import org.springframework.boot.SpringBootConfiguration; import org.springframework.boot.autoconfigure.EnableAutoConfiguration; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication public class Main { public static void main(String[] args) throws Exception { SpringApplication.run(Main.class, args); } }
5. controller
package com.pkyou.Sample.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; @RestController public class HelloWorldController { @RequestMapping("/hello") public String hello() { return "Hello the whole world"; } }
6. 作為JAVA應用程序運行,啟動tomcat,默認使用localhost的8080端口
第一個spring boot工程