首頁 > 軟體

圖文教學教你IDEA中的Spring環境搭建+簡單入門

2023-03-31 06:01:11

首先利用IDEA建立Maven工程專案

1.選擇新建專案

2.選中Maven骨架

3.填寫專案名稱和專案位置

4.Finsh之後預設開啟的是pom.xml檔案

5.在pom.xml檔案下填寫Spring的相關依賴(其中有一些拓寬工具依賴)

5.1完整的pom.xml程式碼(可直接複製)

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>org.example</groupId>
    <artifactId>spring_demo</artifactId>
    <version>1.0-SNAPSHOT</version>

    <dependencies>
        <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <version>4.13</version>
        </dependency>
            <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>5.0.2.RELEASE</version>
        </dependency>
            <dependency>
            <groupId>log4j</groupId>
            <artifactId>log4j</artifactId>
            <version>1.2.9</version>
        </dependency>
            <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-test</artifactId>
            <version>5.0.2.RELEASE</version>
        </dependency>
            <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <version>1.18.12</version>
            <scope>provided</scope>
        </dependency>
    </dependencies>
</project>

6.完善簡單專案結構和編寫測試類檔案

6.1簡單專案結構編寫

6.2在resources包下建立Spring組態檔,整合紀錄檔組態檔

6.2.1 applicationContext.xml 是Spring組態檔

applicationContext是約定俗成的叫法

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:p="http://www.springframework.org/schema/p"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:util="http://www.springframework.org/schema/util"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans.xsd
       http://www.springframework.org/schema/context
       http://www.springframework.org/schema/context/spring-context.xsd
       http://www.springframework.org/schema/util
       http://www.springframework.org/schema/util/spring-util.xsd">
</beans>

6.2.2 log4j2.xml 紀錄檔組態檔
<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="WARN">
    <Appenders>
        <Console name="Console" target="SYSTEM_OUT">
            <PatternLayout pattern="%d{HH:mm:ss} %level : %msg   %l%n" />
        </Console>
    </Appenders>
    <Loggers>
        <Logger name="mylog" level="error" additivity="false">
            <AppenderRef ref="Console" />
        </Logger>
        <Root level="error">
            <AppenderRef ref="Console" />
        </Root>
    </Loggers>
</Configuration>

6.3編寫簡單User類,並加上註解(可以直接複製)

**說明:這裡的User類用到了 Lombok工具,具體可參照官網檔案
Lombok官網:Lombok
@Data 替我們生成 getter,setter,toString等方法
@AllArgs/NoArgsConstructor分別為全參構造和無參構造

package com.xxx.demo.pojo;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.stereotype.Component;

@Data
@AllArgsConstructor
@NoArgsConstructor
@Component
public class User {

    private String userName;
    private String password;

    public void add(){
        System.out.println("User add.........");
    }
}

6.4在config包下編寫AppConfig類,並加上註解(可直接複製程式碼)

@ComponentScan: 全域性掃描元件
其中的引數 basePackages 掃描元件的包
@Configuration: 宣告當前類為JavaConfig類
@Bean: 自動裝配

package com.xxx.demo.config;

import com.xxx.demo.pojo.User;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;

@ComponentScan(basePackages = "com.xxx.demo")
@Configuration
public class AppConfig {
    
    @Bean
    public User user(){
        return new User();
    }
    
}

7.至此Spring簡單設定完成,接下來測試

7.1簡單完善test包,包名和java包下保持一直一致如圖

package com.xxx.demo.pojo;

import com.xxx.demo.config.AppConfig;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = AppConfig.class)
public class TestUser {

    @Autowired
    private User user;

    @Test
    public void test(){
        user.add();
    }

}

7.3執行結果

到此這篇關於圖文教學教你IDEA中的Spring環境搭建+簡單入門的文章就介紹到這了,更多相關IDEA Spring環境搭建內容請搜尋it145.com以前的文章或繼續瀏覽下面的相關文章希望大家以後多多支援it145.com!


IT145.com E-mail:sddin#qq.com