Spring boot maven 搭建框架


Spring Boot:
目的:這個框架幫助開發者更容易地創建基於Spring的應用程序和服務,使得pring開發者能夠最快速地獲得所需要的Spring功能。
優點:完全不需要XML配置,讓spring應用從配置到運行更加快速。但並沒有增加spring額外的功能。
提供了非功能性的大型項目類特性,如(如內嵌服務器、安全、度量、健康檢查、外部化配置),
內部封裝了tomcat的一些核心jar包,將發布封裝了,因此不需要將項目(war包)發布到tomcat上。
搭建一個簡單的Spring web mvc Rest
環境:
eclipse-4.5
java version :"1.7.0_21"
maven:3.2.1
Servlet3容器(tomcat)

創建maven項目:
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.0 http://maven.apache.org/xsd/settings-1.0.0.xsd">
<!-- 倉庫的地址-->
<localRepository>I:\work\apache-maven-3.2.1\springBootMvnRes</localRepository>
<pluginGroups>

</pluginGroups>

<proxies>

</proxies>

<servers>

</servers>

<mirrors>

</mirrors>

<profiles>

</profiles>

</settings>
2:eclipse配置maven如下圖所示:

3:創建maven項目

 new-->other-->maven-->Maven Project-->next-->finsh,maven項目就建好了

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.0 http://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>

<artifactId>TestWebApp</artifactId>
<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>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>


</project>

 5:編寫測試代碼:

java代碼如下:

package com.spring.boot.TestWebApp;

public class User
{
private Long id;
private String name;

public Long getId() {
return id;
}

public void setId(Long id) {
this.id = id;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;

User user = (User) o;

if (id != null ? !id.equals(user.id) : user.id != null) return false;

return true;
}


@Override
public int hashCode() {
return id != null ? id.hashCode() : 0;
}
}

 

控制器代碼:

 

package com.spring.boot.TestWebApp;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@EnableAutoConfiguration
@RestController
@RequestMapping("/user")
public class UserController {

@RequestMapping("/{id}")
public User view(@PathVariable("id") Long id) {
User user = new User();
user.setId(id);
user.setName("qinbb");
return user;
}

public static void main(String[] args) {
SpringApplication.run(UserController.class);
}

}

這時候項目就可以運行了,在controller中run as-->java application 

控制台打印出:

此時在瀏覽器輸入:http://localhost:8080/user/1即可看到頁面效果:

上面的控制器controller:通過在UserController中加上@EnableAutoConfiguration開啟自動配置,然后通過SpringApplication.run(UserController.class);運行這個控制器;這種方式只運行一個控制器比較方便;下面介紹的這個適合多個控制器

代碼如下(這時候需要將控制器中的@EnableAutoConfiguration和main方法注釋):

package com.spring.boot.TestWebApp;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;

@Configuration
@ComponentScan
@EnableAutoConfiguration
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class);
}
}

執行main 這個方法就OK

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM