Spring Framework Study Notes 01: Use Spring Configuration File to Manage Beans

Spring framework
overview:
.Spring official website: https://spring.io/projects/spring-framework
Insert picture description here
Insert picture description here
Use Spring container to manage Beans
Use Spring configuration files to manage Beans
Use annotations to streamline Spring configuration files
Use annotation configuration classes to replace Spring configuration files
Use Java configuration classes Management Bean
2. Introductory case demonstration
(1) Create a Maven project [SpringDemo2021]
Insert picture description here
Insert picture description here
(2) Add dependencies in the pom.xml file, and it will take a little longer to download the shelf package for the first time.

<?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>net.tjl.spring</groupId>
    <artifactId>SpringDemo2021</artifactId>
    <version>1.0-SNAPSHOT</version>
    <properties>
        <!-- spring.version -->
        <spring.version>5.3.4</spring.version>
    </properties>

    <dependencies>
        <!--Spring核心-->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-core</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <!--Spring Bean-->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-beans</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <!--Spring容器-->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <!--Spring测试-->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-test</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <!--单元测试-->
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.13</version>
            <scope>test</scope>
        </dependency>
    </dependencies>

</project>

(3) Create the dragon-killing task class-SlayDragonQuest
* Create the net.tjl.spring.lesson01 package in main/java, and then create the SlayDragonQuest class in it
Insert picture description here

package net.tjl.spring.lesson01;

/**
 * 功能:杀龙任务类
 * 作者:谭金兰
 * 日期:2021年03月15日
 */

public class SlayDragonQuest {
    
    
    public void embark(){
    
    
        System.out.println("执行杀龙任务");

    }
}
//国际消费者权益日:Internqtional Consumer Rights Day

(4) Create brave knight class-BraveKnight
creates BraveKnight class in net.tjl.spring.lesson01 package
Insert picture description here
Insert picture description here

package net.tjl.spring.lesson01;
        /**
        * 功能:勇敢骑士类
        * 作者:谭金兰
        * 日期:2021年03月15日
        */

        public class BraveKnight {
    
    
            private SlayDragonQuest slayDragonQuest;

            public void setSlayDragonQuest(SlayDragonQuest slayDragonQuest) {
    
    
                this.slayDragonQuest = slayDragonQuest;
            }

            public void embarkOnQuest() {
    
    
                slayDragonQuest.embark();
            }
        }

(5) Use the traditional method to let the brave knight complete the task of killing the dragon
* Create the net.tjl.spring.lesson01 package in test/java, and then create the TestBraveKnightOld class in it.
Insert picture description here
Insert picture description here
Run the test method testBraveKnight() to see the result

Insert picture description here
(6) Using the Spring framework to let the brave knight complete the task of killing the dragon
1. Create log4j.properties in the resources directory
Insert picture description here
Insert picture description here
2. Create the Spring configuration file spring-config.xml
Insert picture description here
Insert picture description here
and view the configured spring-config.xml in the project structure window
Insert picture description here
3. Create a Bean in the Spring configuration file
(1) Create a dragon-killing task Bean

<bean id="slayDragonQuest" class="net.tjl.spring.lesson01.SlayDragonQuest"/>

(2) Create a brave knight bean

 <bean id="Mike" class="net.tjl.spring.lesson01.BraveKnight">
        <property name="slayDragonQuest" ref="slayDragonQuest"/>
    </bean>

4. Create a test class-TestBraveKnightNew Create a test class TestBraveKnightNew
in test/java/net.tjl.spring.lesson01
Insert picture description here

package net.tji.spring.lesson01;

import net.tjl.spring.lesson01.BraveKnight;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.springframework.context.support.ClassPathXmlApplicationContext;

/**
 * 功能:采用Spring容器测试勇敢骑士类
 * 作者:谭金兰
 * 日期:2021年03月15日
 */
public class TestBraveKnightNew {
    
    
    private ClassPathXmlApplicationContext context;

    @Before
    public void init() {
    
    
        // 基于Spring配置文件创建应用容器
        context = new ClassPathXmlApplicationContext("xmlconfig/spring-config.xml");
    }

    @Test
    public void testBraveKnight() {
    
    
        // 根据名称从应用容器中获取勇敢骑士对象
        BraveKnight braveKnight = (BraveKnight) context.getBean("Mike");
        // 勇敢骑士执行任务
        braveKnight.embarkOnQuest();
    }

    @After
    public void destroy() {
    
    
        // 关闭应用容器
        context.close();
    }
}

Run the test method to view the results
Insert picture description here
5. Create two more brave knight beans in the Spring configuration file

 <bean id="Mike1" class="net.tjl.spring.lesson01.BraveKnight">
        <property name="slayDragonQuest" ref="slayDragonQuest"/>
    </bean>
    <bean id="Mike2" class="net.tjl.spring.lesson01.BraveKnight">
        <property name="slayDragonQuest" ref="slayDragonQuest"/>
    </bean>

6. Modify the test class-TestBraveKnightNew
Insert picture description here
(7) Use the constructor injection method to inject attributes into the Bean.
1. Create a rescue task class-RescueDamselQuest
creates a RescueDamselQuest class in the net.tjl.spring.lesson01 package.
Insert picture description here
Insert picture description here
2. Creates a knight class to rescue beauty- DamselRescuingKnight
Insert picture description here
Insert picture description here
3. Create a rescue knight bean in the Spring configuration file

<bean id="rescueDamselQuest" class="net.tjl.spring.lesson01.RescueDamselQuest"/>
    <bean id="damselRescuingKnight" class="net.tjl.spring.lesson01.DamselRescuingKnight">
        <constructor-arg ref="rescueDamselQuest"/>
    </bean>
   

4. Create a test class TestDamselRescuingKnightOld
Insert picture description here
Insert picture description here

Run the test method to view the results
Insert picture description here
5. Create a test class TestDamselRescuingKnightNew
Insert picture description here

package net.tji.spring.lesson01;

import net.tjl.spring.lesson01.DamselRescuingKnight;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.springframework.context.support.ClassPathXmlApplicationContext;

/**
 * 功能:采用Spring容器测试拯救少女骑士类
 * 作者:谭金兰
 * 日期:2021年03月15日
 */
public class TestDamselRescuingKnightNew {
    
    
    private ClassPathXmlApplicationContext context;

    @Before
    public void init() {
    
    
        // 基于Spring配置创建应用容器
        context = new ClassPathXmlApplicationContext("xmlconfig/spring-config.xml");
    }

    @Test
    public void testDamselRescuingKnight() {
    
    
        // 根据名称从应用容器里获取拯救少女骑士对象
        DamselRescuingKnight damselRescuingKnight = (DamselRescuingKnight) context.getBean("damselRescuingKnight");
        // 救美骑士执行任务
        damselRescuingKnight.embarkOnQuest();
    }

    @After
    public void destroy() {
    
    
        // 关闭应用容器
        context.close();
    }
}

Run the test method to see the effect
Insert picture description here

Guess you like

Origin blog.csdn.net/triet/article/details/114818193