Spring Boot maven框架搭建教程圖解
摘要:讓Spring應(yīng)用從配置到運行更加快速,演示DIY Spring Boot框架時,如何配置端口號,如何添加日志。
Spring Boot 框架幫助開發(fā)者更容易地創(chuàng)建基于Spring的應(yīng)用程序和服務(wù),使得開發(fā)者能夠快速地獲得所需要的Spring功能。提供了非功能性的大型項目類特性,如(如內(nèi)嵌服務(wù)器、安全、度量、健康檢查、外部化配置),內(nèi)部封裝了tomcat的一些核心jar包,將發(fā)布封裝了,因此不需要將項目(war包)發(fā)布到外部tomcat上。
可以在Spring Boot官網(wǎng) https://start.spring.io/ 快速構(gòu)建項目,這個簡單易用,而且會自動生成啟動類。本文重點介紹如何使用Eclipse構(gòu)建。
搭建一個簡單的、基于Restfull 風(fēng)格的Spring web mvc 項目,結(jié)構(gòu)如下:
環(huán)境:
eclipse:Oxygen Release (4.7.0);java version :"1.8.0_77";
maven:3.5.4;Servlet3容器(tomcat)
1. 配置maven的settings.xml
配置文件中加入了阿里巴巴的鏡像。
<?xml version="1.0" encoding="UTF-8"?> <settings xmlns="http://maven.apache.org/SETTINGS/1.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.0.0http://maven.apache.org/xsd/settings-1.0.0.xsd"> <!-- 倉庫的地址--> <localRepository>E:/MyLibs</localRepository> <pluginGroups> </pluginGroups> <proxies> </proxies> <servers> </servers> <mirrors> <mirror> <id>alimaven-central</id> <mirrorOf>central</mirrorOf> <name>aliyun maven</name> <url>http://maven.aliyun.com/nexus/content/repositories/central/</url> </mirror> <mirror> <id>jboss-public-repository-group</id> <mirrorOf>central</mirrorOf> <name>JBoss Public Repository Group</name> <url>http://repository.jboss.org/nexus/content/groups/public</url> </mirror> </mirrors> <profiles> </profiles> </settings>
2. Eclipse配置Maven
Maven配置如下圖所示,如果已經(jīng)配置,可以忽略此步。
3. 創(chuàng)建maven項目
new-->other-->maven-->Maven Project-->next-->finsh,maven項目就建好了。
包名和項目名根據(jù)需求自定義。
4. 配置pom文件
<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.0http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.4.0.RELEASE</version> </parent> <groupId>com.spring.boot</groupId> <artifactId>TestWebApp</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>jar</packaging> <name>TestWebApp</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> <scope>test</scope> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> <exclusions> <exclusion> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-logging</artifactId> </exclusion> </exclusions> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-log4j2</artifactId> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>
5. 添加日志
項目使用了log4j2打印日志。首先,在src/main目錄下新增文件夾resources,然后,在resources下創(chuàng)建log4j2.xml。這個日志配置比較簡單,有待優(yōu)化。
<?xml version="1.0" encoding="UTF-8"?> <configuration> <appenders> <Console name="Console" target="SYSTEM_OUT"> <ThresholdFilter level="trace" onMatch="ACCEPT" onMismatch="DENY" /> <PatternLayout pattern="%d{HH:mm:ss.SSS} %-5level %class{36} %L %M - %msg%xEx%n" /> </Console> <File name="log" fileName="log/webApp.log" append="false"> <PatternLayout pattern="%d{HH:mm:ss.SSS} %-5level %class{36} %L %M - %msg%xEx%n" /> </File> <RollingFile name="RollingFile" fileName="log/webAppRoll.log" filePattern="log/$${date:yyyy-MM}/app-%d{MM-dd-yyyy}-%i.log"> <PatternLayout pattern="%d{yyyy-MM-dd 'at' HH:mm:ss z} %-5level %class{36} %L %M - %msg%xEx%n" /> <SizeBasedTriggeringPolicy size="50MB" /> </RollingFile> </appenders> <loggers> <root level="INFO"> <appender-ref ref="RollingFile" /> <appender-ref ref="Console" /> </root> </loggers> </configuration>
6. 設(shè)置端口號
在resources下創(chuàng)建application.properties。
1. 編寫測試代碼
javaBean定義如下,包括用戶ID和用戶名。
import java.io.Serializable; public class User implements Serializable { private static final long serialVersionUID = 7797704227043955944L; private Long id; private String name; // getter/setter omitted @Override public String toString() { return "User [id=" + id + ", name=" + name + "]"; } }
控制器代碼:
import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; @RestController @RequestMapping("/user") public class UserController { private static Logger logger = LoggerFactory.getLogger(UserController.class); /** * @Title view<br/> * @Description 示例地址 http://localhost:8080/user/100 <br/> * @param id * @return * @Author 樓蘭的胡楊<br/> * @Time 2018-08-26 11:47<br/> */ @RequestMapping("/{id}") public User view(@PathVariable("id") Long id) { logger.info("接收的請求參數(shù) begin --- id = {}", id); User user = new User(); user.setId(id); user.setName("Spring Boot"); return user; } }
通過在UserController中加上@RequestMapping 配置請求路徑。通過在main方法中運行SpringApplication.run()來啟動項目:
import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication public class Application { public static void main(String[] args) { SpringApplication.run(Application.class); } }
這時候項目就可以運行了,在Application 中run as-->java application ??刂婆_打印結(jié)果:
截圖中紅色方框圈中的文字說明了系統(tǒng)啟動成功,而且,端口號是8080。此時在瀏覽器輸入http://localhost:8080/user/100即可看到頁面效果:
控制臺打印結(jié)果:
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
詳解分別用Kotlin和java寫RecyclerView的示例
本篇文章主要介紹了詳解分別用Kotlin和java寫RecyclerView的示例,詳解分別用Kotlin和java寫RecyclerView的示例2017-12-12Java調(diào)用Zookeeper的實現(xiàn)步驟
本文主要介紹了Java調(diào)用Zookeeper的實現(xiàn)步驟,文中通過示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下2021-08-08Oracle + Mybatis實現(xiàn)批量插入、更新和刪除示例代碼
利用MyBatis動態(tài)SQL的特性,我們可以做一些批量的操作,下面這篇文章主要給大家介紹了關(guān)于Oracle + Mybatis實現(xiàn)批量插入、更新和刪除的相關(guān)資料,文中通過示例代碼介紹的非常詳細,需要的朋友可以參考借鑒,下面來一起看看吧。2018-01-01