16年開始就在寫Spring Boot相關的文章了,以前一直是在本身猿天地的博客上發佈,今年開始維護公衆號,大部分時間都在寫新的文章。java
一週能保持一篇原創的文章就已經很不錯了,畢竟精力有限,在沒有出新文章的時候就想着把以前寫的文章分享出來,給正在入門學習Spring Boot的朋友。web
Spring Boot做爲微服務框架,從最根本上來說,Spring Boot就是一些庫的集合,集成了各類Spring的子項目,它可以被任意項目的構建系統所使用。面試
解決了平時開發中搭建項目框架的難度,很是方便。spring
搭建的步驟也是很簡單的哈,首先建立個maven工程,而後配置pom文件數據庫
文件中配置Spring Boot版本信息以及成jar包的插件。瀏覽器
properties中的java.version是指定jdk的版本。tomcat
關於Spring Boot的版本你們能夠用最新的,目前最新的版本已經到了2.0了。微信
<!-- Inherit defaults from Spring Boot --> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.4.1.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> <properties> <java.version>1.8</java.version> </properties>
還須要別的什麼配置嗎,之前都要配置一些什麼junit啊,各類spring的包啊,commons包啊之類的。多線程
如今徹底不須要了,是否是很簡單,很方便。app
貼一個官網給的列子:
@RestController @EnableAutoConfiguration public class Example { @RequestMapping("/") String home() { return "Hello World!"; } public static void main(String[] args) throws Exception { SpringApplication.run(Example.class, args); } }
咱們能夠看到全部的配置都被註解代替了,啓動程序也能夠直接用main方法啓動了,框架裏有內置的web容器,咱們不在須要把程序丟到tomcat裏面去部署了,否則怎麼體現爲服務框架的魅力。
接下來咱們運行這個main方法能夠看到輸出了一大片日誌信息。最有趣的還要屬這個圖形了。
. ____ _ __ _ _ /\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \ ( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \ \\/ ___)| |_)| | | | | || (_| | ) ) ) ) ' |____| .__|_| |_|_| |_\__, | / / / / =========|_|==============|___/=/_/_/_/ :: Spring Boot :: (v1.4.1.RELEASE)
咱們在瀏覽器中訪問http://localhost:8080就能看到輸出的Hello World!
下面講下配置信息
配置的話默認的配置信息都在application.properties中,本身建個application.properties文件放在classpath中
好比這邊默認啓動的端口是8080, 咱們改爲80的配置以下:
#tomcat port server.port=80
還有就是項目的context path默認是/, 也是能夠改的
#the context path, defaults to '/' server.context-path=/spring-boot/
改爲上面的配置信息後咱們在訪問剛剛的地址就變成了http://localhost/spring-boot
上面官方給的列子咱們能夠看到只是啓動Example這一個類而已,每每開發中咱們確定加載的是多個類
下面給出一個簡單的列子,以學生來講明
咱們先按照這個層次建下包
com +- example +- spring-boot +- Application.java | +- domain | +- Student.java | +- service | +- StudentService.java | +- StudentServiceImpl.java | +- web +- StudentController.java
咱們的Application類是程序的入口,負責加載全部信息
經過ComponentScan掃描類,最簡單的是隻加@SpringBootApplication註解就能夠了,@SpringBootApplication中已經包含了下面的註解。
@Configuration @EnableAutoConfiguration @ComponentScan public class Application { public static void main(String[] args) { SpringApplication.run(Application.class, args); } }
業務類,這邊先不涉及數據庫,就在業務層模擬了一條數據返回。
public interface StudentService { List<Student> queryStudents(); } @Service public class StudentServiceImpl implements StudentService { @Override public List<Student> queryStudents() { return Arrays.asList(new Student("1001", "yinjihuan")); } }
實體類
public class Student { private String id; private String name; public Student() { super(); } public Student(String id, String name) { super(); this.id = id; this.name = name; } public String getId() { return id; } public void setId(String id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } }
控制類
@RestController @EnableAutoConfiguration public class StudentController { @Autowired private StudentService studentService; @RequestMapping("/students") Object queryStudents() { return studentService.queryStudents(); } }
咱們在Application中執行main方法啓動程序,而後訪問咱們的students資源
http://localhost/spring-boot/students
能夠看到輸出的結果
[{"id":"1001","name":"yinjihuan"}]
推薦相關閱讀: