idea將maven項目改成Spring boot項目


idea將maven項目改成Spring boot項目

 

1、添加parent父級依賴

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

<!-- 這個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核心依賴,並添加核心測試依賴

<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屬性配置主要是存放依賴的版本號,可以自定義,相對於定義了一個變量

<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打包插件配置

<!-- 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應用程序的入口方法。

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

在main方法中執行的Spring Application的run方法,返回一個上下文的容器實例

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項目的配置注解,這也是一個組合注解

@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包中的接口的路徑,使得可以在運行時動態代理生成實現類並放入到容器中管理。

@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會完成自動注入。

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

 


免責聲明!

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



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