javascript
Springboot搭建web项目
姓名:黃于霞 ? ? 班級:軟件151
1、pom配置
首先,建立一個maven項目,修改pom.xml文件,添加parent依賴。
<parent>
???<groupId>org.springframework.boot</groupId>
??? <artifactId>spring-boot-starter-parent</artifactId>
???<version>1.4.2.RELEASE</version>
</parent>
spring-boot-starter-parent會自動為我們引入spring相關的依賴。
再看dependencies節點:
-
我們需要引入starter-web,這是開發web項目必須的依賴,springboot默認集成了tomcat服務器,在這里排除了tomcat,引入了NIO服務器undertow。
-
springboot默認服務器端口8080,可以自行修改,后面會介紹。
-
?視圖引擎選擇velocity,引入starter-velocity即可,具體配置后面介紹。
-
?引入maven插件:
????????<plugin>
???????????? <groupId>org.springframework.boot</groupId>
???????????? <artifactId>spring-boot-maven-plugin</artifactId>
????????</plugin>
?
2、程序入口
????在一級包路徑下,比如com.xxx,新建一個Application.java。
解釋一下注解:
-
@Configuration:指出該類是 Bean 配置的信息源,相當于XML中的<beans></beans>,一般加在主類上。
-
@EnableAutoConfiguration:讓 SpringBoot 根據應用所聲明的依賴來對 Spring 框架進行自動配置,由于 spring-boot-starter-web 添加了Tomcat和Spring MVC,所以auto-configuration將假定你正在開發一個web應用并相應地對Spring進行設置
-
@ ComponentScan:表示將該類自動發現(掃描)并注冊為Bean,可以自動收集所有的Spring組件(@Component , @Service , @Repository , @Controller 等),包括@Configuration類。
-
@SpringBootApplication: @EnableAutoConfiguration、@ComponentScan和@Configuration的合集。
-
@ EnableTransactionManagement:啟用注解式事務。
?
3、配置
????在項目resources目錄下新建application.properties文件,springboot默認會讀取該配置文件,當然你也可以創建一個名為application.yml文件。
4、控制器
????控制器依然使用@Controller注解,或者@RestController(返回json,Controller和ResponseBody合體),我們在templates下新建一個index.vm視圖文件,輸出hello,world!
?
5、打包,啟動
????使用mvn clean package將應用打成一個jar包,比如test.jar。
????在命令行執行命令:java -jar test.jar(也可以在IDE中直接執行main方法)
?
在瀏覽器輸入localhost:8081/test/看一下效果:
?
6、總結:
-
優點:簡化配置,快速構建應用。
-
缺點:坑很多啊,踩過才知道,對spring平臺不了解,所以還是老老實實的自己配置。
?
?
?
轉載于:https://www.cnblogs.com/hyxz/p/7080753.html
總結
以上是生活随笔為你收集整理的Springboot搭建web项目的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 如何垂直居中一个浮动元素
- 下一篇: 3.strcpy使用注意(3)