前言
spring框架作為JavaEE框架領域的一款重要的開源框架,在企業應用開發中有著很重要的作用,同時Spring框架及其子框架很多,所以知識量很廣。
Spring Boot:一款Spring框架的子框架,也可以叫微框架,是2014年推出的一款使Spring框架開發變得容易的框架。學過Spring框架的都知識,Spring框架難以避免地需要配置不少XMl,而使用Spring Boot框架的話,就可以使用注解開發,極大地簡化基于Spring框架的開發。
Spring Boot充分利用了JavaConfig的配置模式以及“約定優于配置”的理念,能夠極大的簡化基于Spring MVC的Web應用和REST服務開發。
然后本博客介紹基于IDEA編輯器的Spring Boot項目創建和部署。
Spring Boot項目創建
1.創建Maven項目
2.在pom.xml加入Spring Boot的jar
如果只是測試一個字符串輸出的話,只要加入spring-boot-starter(核心模塊)和spring-boot-starter-web(因為這個一個Web項目),可以參考我的配置,這里使用了Spring Boot熱部署,需要去github上搜索jar:springloaded-1.2.4.RELEASE.jar,然后下載放在項目的lib文件夾里
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
|
<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/maven-v4_0_0.xsd" > <modelVersion> 4.0 . 0 </modelVersion> <groupId>com.example</groupId> <artifactId>demo</artifactId> <packaging>war</packaging> <version> 1.0 -SNAPSHOT</version> <name>demo Maven Webapp</name> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version> 1.5 . 1 .RELEASE</version> <relativePath/> </parent> <properties> <project.build.sourceEncoding>UTF- 8 </project.build.sourceEncoding> <java.version> 1.8 </java.version> <spring-boot-admin.version> 1.4 . 5 </spring-boot-admin.version> </properties> <dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version> 3.8 . 1 </version> <scope>test</scope> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <dependency> <groupId>de.codecentric</groupId> <artifactId>spring-boot-admin-starter-client</artifactId> <version>${spring-boot-admin.version}</version> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter</artifactId> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin </artifactId> <dependencies> <!--springloaded hot deploy --> <dependency> <groupId>org.springframework</groupId> <artifactId>springloaded</artifactId> <systemPath>${basedir}/src/main/webapp/WEB-INF/lib/springloaded- 1.2 . 5 .RELEASE.jar</systemPath> </dependency> </dependencies> <executions> <execution> <goals> <goal>repackage</goal> </goals> <configuration> <classifier>exec</classifier> </configuration> </execution> </executions> </plugin> </plugins> </build> </project> |
刷新,下載jar到maven項目里
3.編寫程序,項目結構如圖
寫個啟動類Application.Java:
啟動類設置端口為8087,因為默認端口是8080,而有很多應用都是8080端口,避免重復,最好自己改端口
其中@SpringBootApplication申明讓spring boot自動給程序進行必要的配置,等價于以默認屬性使用
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
|
@Configuration , @EnableAutoConfiguration 和 @ComponentScan package com; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.builder.SpringApplicationBuilder; import org.springframework.boot.context.embedded.ConfigurableEmbeddedServletContainer; import org.springframework.boot.context.embedded.EmbeddedServletContainerCustomizer; import org.springframework.boot.web.support.SpringBootServletInitializer; import org.springframework.scheduling.annotation.EnableAsync; @SpringBootApplication @EnableAsync public class Application implements EmbeddedServletContainerCustomizer { public static void main(String[] args) { SpringApplication.run(Application. class , args); } @Override public void customize(ConfigurableEmbeddedServletContainer configurableEmbeddedServletContainer) { configurableEmbeddedServletContainer.setPort( 8087 ); } } |
寫個Controller類:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
package com.example; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; /** * Created by Administrator on 2017/4/24. */ @RestController @RequestMapping ( "/" ) public class DemoController { @RequestMapping ( "/demo" ) private String demo() { return "this is spring boot demo!!!" ; } } |
導入不想自己寫demo,可以通過http://start.spring.io/ ,在平臺自動生成一個demo代碼,然后打開項目就好
Spring Boot部署
添加個Spring Boot配置服務器
訪問:
以上所述是小編給大家介紹的JavaEE微框架Spring Boot深入解讀,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復大家的。在此也非常感謝大家對服務器之家網站的支持!
原文鏈接:http://blog.csdn.net/u014427391/article/details/70655332