3、Spring之入门案例
阅读原文时间:2023年08月21日阅读:3

3.1.1、右击project,创建新module

3.1.2、选择maven

3.1.3、设置module名称和路径

3.1.4、module初始状态

3.1.5、配置打包方式和依赖

<?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.rain</groupId>
    <artifactId>spring_helloword</artifactId>
    <version>1.0-SNAPSHOT</version>
    <packaging>jar</packaging>

    <dependencies>
        <!-- 基于Maven依赖传递性,导入spring-context依赖即可导入当前所需所有jar包 -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>5.3.1</version>
        </dependency>
        <!-- junit测试 -->
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
            <scope>test</scope>
        </dependency>
    </dependencies>
</project>

3.2.1、创建组件类

package org.rain.spring.component;

/**
 * @author liaojy
 * @date 2023/7/25 - 19:17
 */
public class HelloWord {

    public void sayHello(){
        System.out.println("Hello,Spring!");
    }
}

3.2.2、创建Sprig配置文件

++++++++++++++++++++++++++++++++++++分割线++++++++++++++++++++++++++++++++++++

注意:因为会通过自定义代码指定Spring配置文件,所以Spring配置文件名可以是任意的;

但当整合ssm后,就不能通过通过自定义代码指定Spring配置文件,因此文件名有硬性要求。

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

    <!--
        bean标签:配置交给Spring的IOC容器管理的对象
            id属性:设置bean的唯一标识
            class属性:设置bean所对应类型的全类名
    -->
    <bean id="helloWord" class="org.rain.spring.component.HelloWord"></bean>

</beans>

3.2.3、创建测试类

如图所示,获取到了IOC容器和容器中对应的bean组件,并成功调用了该bean组件的方法

package org.rain.spring.test;

import org.junit.Test;
import org.rain.spring.component.HelloWord;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

/**
 * @author liaojy
 * @date 2023/7/25 - 19:39
 */
public class HelloWordTest {

    @Test
    public void testHelloWord(){
        //获取IOC容器
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
        //获取IOC容器中的bean
        HelloWord helloWord = (HelloWord)applicationContext.getBean("helloWord");
        helloWord.sayHello();
    }
}

3.3.1、根据id获取

由于 id 属性是 bean 的唯一标识,所以根据 bean 标签的 id 属性可以精确获取到一个组件对象;但也存在类型转换问题,具体见上一小节。

3.3.2、根据类型获取(最常用)

如图所示,根据类型获取bean,则不存在类型转换问题

    @Test
    public void testHelloWord(){
        //获取IOC容器
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
        //获取IOC容器中的bean
        HelloWord helloWord = applicationContext.getBean(HelloWord.class);
        helloWord.sayHello();
    }

注意:如下图所示,当根据类型获取bean时,要求IOC容器中指定类型的bean有且只能有一个;否则会报错

org.springframework.beans.factory.NoUniqueBeanDefinitionException:
No qualifying bean of type 'org.rain.spring.component.HelloWord' available:
expected single matching bean but found 2: helloWord,helloWordtwo

+++++++++++++++++++++++++++++++++++分割线+++++++++++++++++++++++++++++++++++

3.3.3、根据id和类型获取

    @Test
    public void testHelloWord(){
        //获取IOC容器
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
        //获取IOC容器中的bean
        HelloWord helloWord = applicationContext.getBean("helloWord",HelloWord.class);
        helloWord.sayHello();
    }

3.3.4、重要扩展

如果组件类实现了接口,则根据接口类型可以获取 bean,前提是IOC容器中实现该接口的组件类型的bean有且只能有一个

+++++++++++++++++++++++++++++++++++分割线+++++++++++++++++++++++++++++++++++

+++++++++++++++++++++++++++++++++++分割线+++++++++++++++++++++++++++++++++++

+++++++++++++++++++++++++++++++++++分割线+++++++++++++++++++++++++++++++++++

    @Test
    public void testHelloWord(){
        //获取IOC容器
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
        //根据接口类型获取IOC容器中的bean
        HelloWord helloWord = (HelloWord) applicationContext.getBean(Hello.class);
        helloWord.sayHello();
    }