最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

idea將maven項(xiàng)目改成Spring boot項(xiàng)目的方法步驟

 更新時(shí)間:2020年09月07日 14:16:13   作者:張財(cái)華  
這篇文章主要介紹了idea將maven項(xiàng)目改成Spring boot項(xiàng)目的方法步驟,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧

1、添加parent父級(jí)依賴

在pom.xml文件中,要首先添加parent父級(jí)依賴

<!-- 這個(gè)parent是springboot的父級(jí)依賴,
  它提供相關(guān)的starter的maven管理以及版本號(hào)管理,還有相關(guān)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核心依賴和測(cè)試依賴

在dependencies中,添加spring-boot-starter核心依賴,并添加核心測(cè)試依賴

<dependencies>
  <!-- 這是springboot的核心starter,它將完成起步依賴,自動(dòng)配置,日志,YAML配置等功能 -->
  <dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter</artifactId>
  </dependency>
  <!-- 測(cè)試依賴 -->
  <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屬性配置主要是存放依賴的版本號(hào),可以自定義,相對(duì)于定義了一個(gè)變量

<properties>
  <!-- 指定jdk版本 -->
  <java.version>1.8</java.version>
  <!-- druid連接池版本 -->
  <druid.version>1.1.17</druid.version>
</properties>

<dependencies>
  <!-- alibaba開(kāi)發(fā)的druid連接池 -->
  <dependency>
    <groupId>com.alibaba</groupId>
    <artifactId>druid-spring-boot-starter</artifactId>
    <!-- 對(duì)應(yīng)properties中的<druid.version> -->
    <version>${druid.version}</version>
  </dependency>
</dependencies>

4、添加build打包插件配置

<!-- spring boot打包插件,主要將spring boot應(yīng)用打包成jar文件或者war文件 -->
<build>
  <plugins>
    <plugin>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-maven-plugin</artifactId>
    </plugin>
  </plugins>
</build>

5、搭建入口類

Spring boot項(xiàng)S目一般都有一個(gè)*Application.java的入口類,里面有一個(gè)main的方法,這是標(biāo)準(zhǔn)Java應(yīng)用程序的入口方法。

@SpringBootApplication
public class TestApplication(){
  public static void main(String[] args){
    SpringApplication.run(TestApplication.class, args);
  }
}

在main方法中執(zhí)行的Spring Application的run方法,返回一個(gè)上下文的容器實(shí)例

public static void main(String[] args) {
  //SpringApplication的run方法返回一個(gè)上下文的容器實(shí)例
  ApplicationContext context = SpringApplication.run(TestApplication.class, args);
  //從容器獲取bean對(duì)象
  UserService service = context.getBean(UserServiceImpl.class);
  service.say();
}

@SpringBootApplication注解是Spring boot的核心注解,它是一個(gè)組合注解,主要包含以下注解:

1、@SpringBootConfiguration:這是Spring boot項(xiàng)目的配置注解,這也是一個(gè)組合注解

@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Configuration
public @interface SpringBootConfiguration {
  @AliasFor(
    annotation = Configuration.class
  )
  boolean proxyBeanMethods() default true;
}

2、@EnableAutoConfiguration:?jiǎn)⒂米詣?dòng)配置,該注解會(huì)使Spring boot根據(jù)項(xiàng)目中依賴的jar包自動(dòng)配置項(xiàng)目的配置項(xiàng)

3、@ComponentScan:默認(rèn)掃描@SpringBootApplication所在類的同級(jí)目錄以及它的子目錄。

如果Spring boot項(xiàng)目中整合了SpringMVC,那么就需要添加一個(gè)注解@MapperScan,這個(gè)注解的作用是告訴容器dao包中的接口的路徑,使得可以在運(yùn)行時(shí)動(dòng)態(tài)代理生成實(shí)現(xiàn)類并放入到容器中管理。

@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目錄中創(chuàng)建一個(gè)application.properties文件或者application.yml(推薦)文件,對(duì)項(xiàng)目的相關(guān)配置都可以在這個(gè)文件中配置。

7、搭建測(cè)試環(huán)境

在test目錄下創(chuàng)建測(cè)試類,在類名上加@SpringBootTest注解,在測(cè)試類中還可以注入bean,Spring boot會(huì)完成自動(dòng)注入。

@SpringBootTest
public class UsersTest(){
  
  @AutoWired
  private UserService service;
  
  @Test
  void testUser(){
    ...
  }
}

到此這篇關(guān)于idea將maven項(xiàng)目改成Spring boot項(xiàng)目的方法步驟的文章就介紹到這了,更多相關(guān)idea maven改成Springboot內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評(píng)論

大兴区| 宁武县| 富顺县| 务川| 大洼县| 金山区| 承德县| 从江县| 焦作市| 乌兰县| 白玉县| 汽车| 博白县| 垦利县| 民勤县| 资阳市| 呼和浩特市| 海口市| 明星| 衡阳市| 正宁县| 正阳县| 塘沽区| 交城县| 建宁县| 汝州市| 射洪县| 无极县| 东丰县| 丰城市| 全椒县| 石首市| 开封县| 鹤壁市| 铜鼓县| 遂平县| 吉安市| 德兴市| 吉林省| 赤峰市| 吉木乃县|