JavaEE微框架Spring Boot深入解讀。本站提示廣大學習愛好者:(JavaEE微框架Spring Boot深入解讀)文章只能為提供參考,不一定能成為您想要的結果。以下是JavaEE微框架Spring Boot深入解讀正文
前言
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文件夾裡
<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自動給程序進行必要的配置,等價於以默認屬性使用
@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類:
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深入解讀,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復大家的。在此也非常感謝大家對網站的支持!