1、添加parent父級依賴
在pom.xml文件中,要首先添加parent父級依賴
1
2
3
4
5
6
7
8
|
<!-- 這個parent是springboot的父級依賴, 它提供相關的starter的maven管理以及版本號管理,還有相關maven插件的公共配置 --> < parent > < groupId >org.springframework.boot</ groupId > < artifactId >spring-boot-starter-parent</ artifactId > < version >2.2.4.RELEASE</ version > < relativePath /> <!-- lookup parent from repository --> </ parent > |
2、添加spring-boot-starter核心依賴和測試依賴
在dependencies中,添加spring-boot-starter核心依賴,并添加核心測試依賴
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
|
< dependencies > <!-- 這是springboot的核心starter,它將完成起步依賴,自動配置,日志,YAML配置等功能 --> < dependency > < groupId >org.springframework.boot</ groupId > < artifactId >spring-boot-starter</ artifactId > </ dependency > <!-- 測試依賴 --> < dependency > < groupId >org.springframework.boot</ groupId > < artifactId >spring-boot-starter-test</ artifactId > < scope >test</ scope > < exclusions > < exclusion > < groupId >org.junit.vintage</ groupId > < artifactId >junit-vintage-engine</ artifactId > </ exclusion > </ exclusions > </ dependency > </ dependencies > |
3、添加properties屬性配置
properties屬性配置主要是存放依賴的版本號,可以自定義,相對于定義了一個變量
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
< properties > <!-- 指定jdk版本 --> < java.version >1.8</ java.version > <!-- druid連接池版本 --> < druid.version >1.1.17</ druid.version > </ properties > < dependencies > <!-- alibaba開發的druid連接池 --> < dependency > < groupId >com.alibaba</ groupId > < artifactId >druid-spring-boot-starter</ artifactId > <!-- 對應properties中的<druid.version> --> < version >${druid.version}</ version > </ dependency > </ dependencies > |
4、添加build打包插件配置
1
2
3
4
5
6
7
8
9
|
<!-- spring boot打包插件,主要將spring boot應用打包成jar文件或者war文件 --> < build > < plugins > < plugin > < groupId >org.springframework.boot</ groupId > < artifactId >spring-boot-maven-plugin</ artifactId > </ plugin > </ plugins > </ build > |
5、搭建入口類
Spring boot項S目一般都有一個*Application.java的入口類,里面有一個main的方法,這是標準Java應用程序的入口方法。
1
2
3
4
5
6
|
@SpringBootApplication public class TestApplication(){ public static void main(String[] args){ SpringApplication.run(TestApplication. class , args); } } |
在main方法中執行的Spring Application的run方法,返回一個上下文的容器實例
1
2
3
4
5
6
7
|
public static void main(String[] args) { //SpringApplication的run方法返回一個上下文的容器實例 ApplicationContext context = SpringApplication.run(TestApplication. class , args); //從容器獲取bean對象 UserService service = context.getBean(UserServiceImpl. class ); service.say(); } |
@SpringBootApplication注解是Spring boot的核心注解,它是一個組合注解,主要包含以下注解:
1、@SpringBootConfiguration:這是Spring boot項目的配置注解,這也是一個組合注解
1
2
3
4
5
6
7
8
9
10
|
@Target ({ElementType.TYPE}) @Retention (RetentionPolicy.RUNTIME) @Documented @Configuration public @interface SpringBootConfiguration { @AliasFor ( annotation = Configuration. class ) boolean proxyBeanMethods() default true ; } |
2、@EnableAutoConfiguration:啟用自動配置,該注解會使Spring boot根據項目中依賴的jar包自動配置項目的配置項
3、@ComponentScan:默認掃描@SpringBootApplication所在類的同級目錄以及它的子目錄。
如果Spring boot項目中整合了SpringMVC,那么就需要添加一個注解@MapperScan,這個注解的作用是告訴容器dao包中的接口的路徑,使得可以在運行時動態代理生成實現類并放入到容器中管理。
1
2
3
4
5
6
7
|
@SpringbootApplication @MapperScan ({ "edu.nf.ch01.user.dao" , "edu.nf.ch01.product.dao" }) public class TestApplication(){ public static void main(String[] args){ SpringApplication.run(TestApplication. class , args); } } |
6、application配置文件
在resource目錄中創建一個application.properties文件或者application.yml(推薦)文件,對項目的相關配置都可以在這個文件中配置。
7、搭建測試環境
在test目錄下創建測試類,在類名上加@SpringBootTest注解,在測試類中還可以注入bean,Spring boot會完成自動注入。
1
2
3
4
5
6
7
8
9
10
11
|
@SpringBootTest public class UsersTest(){ @AutoWired private UserService service; @Test void testUser(){ ... } } |
到此這篇關于idea將maven項目改成Spring boot項目的方法步驟的文章就介紹到這了,更多相關idea maven改成Springboot內容請搜索服務器之家以前的文章或繼續瀏覽下面的相關文章希望大家以后多多支持服務器之家!
原文鏈接:https://www.cnblogs.com/zhangcaihua/p/12786427.html