因爲Spring官方推薦使用Maven來構建項目,進行依賴管理(jar包的管理)所以第一步首先去Maven官網下載maven包 地址:maven下載地址
第一步:把下載好的文件解壓到你喜歡的磁盤目錄中,例如我的 D:\apache-maven-3.5.0 解壓出來後目錄有 bin(maven的一些執行命令的腳本 如mvn命令),boot(它是一個類加載器的框架,相對於JDK中的類加載器,它提供了更豐富的語法以方便配置,Maven使用該框架加載自己的類庫),conf(maven的配置文件目錄),lib(maven允許所以來的一些jar包)
第二步: 因爲這是解壓包如果不配置環境變量是無法使用的 所以給maven配置環境變量 (如何配置請自己到網上搜索)
第三步:在myeclipse中去添加自己的maven(mp中有自帶的maven但是不穩定) 步驟:(1)windows——preferencesht——Maven——Installations——add——Installations home中填寫maven安裝目錄,在勾選剛纔添加的maven 點擊apply。 (2)User Settings——User Settings填寫maven的setting.xml所在目錄。
第四步:創建maven項目 點擊new-other-maven project ——(maven-archetype-webapp)——Group Id(項目包名)ArtifactID(項目名稱)——finish即可
在pom.xml中添加依賴:
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.6.RELEASE</version>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
在項目根目錄創建一個springboot的啓動類
@SpringBootApplication
//@SpringBootApplication註解默認是掃描本包及子包中的註解,要想自定義可以使用@ComponentScan()來指定掃描路徑
//繼承SpringBootServletInitializer就可以像普通web項目一樣加在自己tomcat中運行就可以了
public class SampleController extends SpringBootServletInitializer{
public static void main(String[] args) {
SpringApplication.run(SampleController.class, args);
}
}
在建一個
controller類來測試
@RestController
public class Boot {
@Autowired
private User user;
//在瀏覽器中應爲springboot使用的是內置tomcat所以不需要加項目名例如:http://localhost:8080/boot訪問即可
@RequestMapping("/boot")
public String ss(){
user.setName("張三");
user.setSex("男");
user.setAddre("北京");
String data="{\"name\":\"張三\"}";//用於拼接json格式字符串
return data;
}
點擊run as ——spring app來運行
瀏覽器輸入localhost:8080/boot即可瀏覽到數據