Spring-1-深入理解Spring XML中的依赖注入(DI):简化Java应用程序开发

news2024/10/5 15:34:59

学习目标

前两篇文章我们介绍了什么是Spring,以及Spring的一些核心概念,并且快速快发一个Spring项目,以及详细讲解IOC,今天详细介绍一些DI(依赖注入)

能够配置setter方式注入属性值

能够配置构造方式注入属性值

能够理解什么是自动装配

一、依赖注入(DI配置)

1 依赖注入方式【重点】

思考:向一个类中传递数据的方式有几种?(给类中的属性赋值)

  • setter方法

  • 构造方法

思考:依赖注入描述了在容器中建立bean与bean之间依赖关系的过程,注入数据类型有哪些?

  • 简单类型=八种基本数据类型+String

  • 引用类型

1.1 依赖注入的两种方式

  • setter注入
    • 简单类型

    • 引用类型(很常用)

  • 构造器注入
    • 简单类型

    • 引用类型

2 setter方式注入

思考:setter方式注入使用什么子标签?

property标签: 调用set方法赋值

name: 成员变量名, 准确来说对应set方法名,首字母大写

value: 对简单类型的成员变量赋值

ref: 对引用类型的成员变量赋值

2.1简单类型setter注入

格式:

<!-- property标签: 调用set方法赋值
       name: 成员变量名, 准确来说对应set方法名,首字母大写
       value: 对简单类型的成员变量赋值 -->
<property name="age" value="20"></property>

2.2 引用类型setter注入

格式:

<!--property标签: 调用set方法赋值
      name: 成员变量名, 准确来说对应set方法名,首字母大写
      ref: 对引用类型的成员变量赋值, 引用的对象 -->
<property name="studentDao" ref="studentDao">
</property>

2.3 setter注入代码实现

【第0步】创建项目
【第1步】导入Spring坐标
【第2步】导入Student实体类
【第3步】定义Spring管理的类(接口)
【第4步】创建Spring配置文件在resources目录下创建`application.xml`,配置setter的简单类型
【第5步】在test目录下创建`StudentServiceTest`,进行测试
【第6步】在`application.xml`,配置对应引用类型注入
【第7步】测试

【第0步】创建项目

【第1步】导入Spring坐标

  <dependencies>
      <!--导入spring的坐标spring-context,对应版本是5.2.10.RELEASE-->
      <dependency>
          <groupId>org.springframework</groupId>
          <artifactId>spring-context</artifactId>
          <version>5.3.15</version>
      </dependency>

      <!-- 导入junit的测试包 -->
      <dependency>
          <groupId>org.junit.jupiter</groupId>
          <artifactId>junit-jupiter</artifactId>
          <version>5.8.2</version>
          <scope>test</scope>
      </dependency>

      <dependency>
          <groupId>org.projectlombok</groupId>
          <artifactId>lombok</artifactId>
          <version>1.18.28</version>
      </dependency>
  </dependencies>

【第2步】导入Student实体类

@Data
@ToString
@AllArgsConstructor
public class Student {
    private String name;
    private String address;
    private Integer age;

    private Integer status;
}

【第3步】定义Spring管理的类(接口)

  • StudentDao接口和StudentDaoImpl实现类用于简单类型注入

package com.zbbmeta.dao;

public interface StudentDao {
    /**
     * 添加学生
     */
    void save();
}
public class StudentDaoImpl implements StudentDao {
    //简单类型属性
    private Integer age;
    public void setAge(Integer age) {
        this.age = age;
    }
    @Override
    public void save() {
        System.out.println("DAO: 年龄:"+this.age);
        System.out.println("DAO: 添加学生信息到数据库...");
    }
}
  • StudentService接口和StudentServiceImpl实现类用于引用类型注入

package com.zbbmeta.service;

public interface StudentService {
    /**
     * 添加学生
     */
    void save();
}

package com.zbbmeta.service.impl;

import com.zbbmeta.dao.StudentDao;
import com.zbbmeta.service.StudentService;


public class StudentServiceImpl implements StudentService {

    //创建成员对象
    private StudentDao studentDao ;

    //提供依赖对象对应的setter方法
    public void setStudentDao(StudentDao studentDao) {
        this.studentDao = studentDao;
    }
    
    @Override
    public void save() {
        System.out.println("Service: 添加学生信息到数据库...");
        studentDao.save();
    }
}

【第4步】创建Spring配置文件在resources目录下创建application.xml,配置setter的简单类型

  • 定义application.xml文件中创建StudentDao类到IOC容器,并实现简单类型注入

<?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">

    <!-- 目标:setter简单类型注入-->
    <bean class="com.zbbmeta.dao.impl.StudentDaoImpl" id="studentDao">
<!--        property标签: 调用set方法赋值
            name: 成员变量名, 准确来说对应set方法名,首字母大写
            value: 对简单类型的成员变量赋值 -->
        <property name="age" value="20"></property>
    </bean>
</beans>

【第5步】在test目录下创建StudentServiceTest,进行测试

package com.zbbmeta;

import com.zbbmeta.dao.StudentDao;
import com.zbbmeta.service.StudentService;
import org.junit.jupiter.api.Test;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class StudentServiceTest {

    //目标:测试setter的简单类型的注入
    @Test
    public void test1(){

        //1.根据配置文件application.xml创建IOC容器
        ClassPathXmlApplicationContext ac = new ClassPathXmlApplicationContext("application.xml");

        //2.从IOC容器里面获取id="bookService"对象
        StudentDao studentDao = (StudentDao) ac.getBean("studentDao");

        //3.执行对象方法
        studentDao.save();

        //4.关闭容器
        ac.close();
    }
  • 控制台结果:

【第6步】在application.xml,配置引用类型注入

    <!-- 目标:setter引用类型注入-->
    <bean class="com.zbbmeta.service.impl.StudentServiceImpl" id="studentService">
        <!--property标签: 调用set方法赋值
    name: 成员变量名, 准确来说对应set方法名,首字母大写
    ref: 对引用类型的成员变量赋值, 引用的对象 -->
        <property name="studentDao" ref="studentDao"></property>
    </bean>

【第7步】测试

    //目标:测试setter的引用类型的注入
    @Test
    public void test2(){
        //1.根据配置文件application.xml创建IOC容器
        ClassPathXmlApplicationContext ac = new ClassPathXmlApplicationContext("application.xml");
        //2.从IOC容器里面获取id="bookService"对象
        StudentService studentService = (StudentService) ac.getBean("studentService");
        //3.执行对象方法
        studentService.save();
        //4.关闭容器
        ac.close();
    }
  • 控制台结果

3 构造器方式注入

思考:构造方式注入使用什么子标签?

3.1 构造器注入简单类型

格式:配置中使用constructor-arg标签value属性注入简单类型

<!--constructor-arg标签: 调用构造函数方法赋值
    name: 成员变量名, 准确来说对应构造方法中参数名称
    value: 简单类型,方法参数对应的值-->
<constructor-arg name="age" value="30"></constructor-arg>

2.2 构造器注入引用类型

格式:配置中使用constructor-arg标签ref属性注入引用类型

<!--constructor-arg标签: 调用构造函数方法赋值
    name: 成员变量名, 准确来说对应构造方法中参数名称
    ref: 引用类型,属性注入引用类型对象-->
<constructor-arg name="studentDao" ref="studentDao"></constructor-arg>

3.3 构造器注入代码实现

【第0步】创建`11_2_DI_Construce`项目结构
【第1步】导入依赖坐标
【第2步】导入Student实体类
【第3步】定义Spring管理的类(接口)
【第4步】创建Spring配置文件在resources目录下创建`application.xml`,配置构造器注入简单类型
【第5步】在test目录下创建`StudentServiceTest`,进行测试
【第6步】在`application.xml`,配置构造器注入引用类型
【第7步】测试

【第0步】创建11_2_DI_Construce项目结构

【第1步】导入依赖坐标

和之前项目依赖一致

【第2步】导入Student实体类

和之前一致

【第3步】定义Spring管理的类(接口)

  • StudentDao接口和StudentDaoImpl实现类实现构造器注入简单类型

package com.zbbmeta.dao;

public interface StudentDao {
    /**
     * 添加学生
     */
    void save();
}
package com.zbbmeta.dao.impl;

import com.zbbmeta.dao.StudentDao;

public class StudentDaoImpl implements StudentDao {
    //简单类型属性
    private Integer age;

    public StudentDaoImpl(Integer age){
        System.out.println("DAO: 注入简单类型 age");
        this.age =age;
    }

    @Override
    public void save() {
        System.out.println("DAO: 年龄:"+this.age);
        System.out.println("DAO: 添加学生信息到数据库...");
    }
}

  • StudentService接口和StudentServiceImpl实现类实现构造器注入引用类型

package com.zbbmeta.service;

public interface StudentService {
    /**
     * 添加学生
     */
    void save();
}

package com.zbbmeta.service.impl;

import com.zbbmeta.dao.StudentDao;
import com.zbbmeta.service.StudentService;

public class StudentServiceImpl implements StudentService {

    //创建成员对象
    private StudentDao studentDao ;
    public StudentServiceImpl(StudentDao studentDao){
        System.out.println("StudentService: 注入引用类型studentDao");
        this.studentDao =studentDao;
    }
    @Override
    public void save() {
        System.out.println("Service: 添加学生信息到数据库...");
        studentDao.save();
    }
}

【第4步】创建Spring配置文件在resources目录下创建application.xml,配置构造器注入简单类型

  • 定义application.xml配置文件并配置StudentDaoImpl实现构造器注入简单类型

<?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 class="com.zbbmeta.dao.impl.StudentDaoImpl" id="studentDao">
        <!--constructor-arg标签: 调用构造函数方法赋值
            name: 成员变量名, 准确来说对应构造方法中参数名称
            value: 简单类型,方法参数对应的值-->
        <constructor-arg name="age" value="30"></constructor-arg>
    </bean>
</beans>

【第5步】在test目录下创建StudentServiceTest,进行测试

package com.zbbmeta;

import com.zbbmeta.dao.StudentDao;
import com.zbbmeta.service.StudentService;
import org.junit.jupiter.api.Test;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class StudentServiceTest {
    //目标:测试构造器的简单类型的注入
    @Test
    public void test1(){
        //1.根据配置文件application.xml创建IOC容器
        ClassPathXmlApplicationContext ac = new ClassPathXmlApplicationContext("application.xml");
        //2.从IOC容器里面获取id="studentDao"对象
        StudentDao studentDao = (StudentDao) ac.getBean("studentDao");
        //3.执行对象方法
        studentDao.save();
        //4.关闭容器
        ac.close();
    }
}
  • 控制台结果:

**【第6步】在application.xml,配置构造器注入引用类型 **

<!-- 目标:构造器依赖注入引用类型【了解】-->
<bean class="com.zbbmeta.service.impl.StudentServiceImpl" id="studentService">
    <!--constructor-arg标签: 调用构造函数方法赋值
        name: 成员变量名, 准确来说对应构造方法中参数名称
        ref: 引用类型,属性注入引用类型对象-->
    <constructor-arg name="studentDao" ref="studentDao"></constructor-arg>
</bean>

【第7步】测试

    //目标:测试setter的引用类型的注入
    @Test
    public void test2(){
        //1.根据配置文件application.xml创建IOC容器
        ClassPathXmlApplicationContext ac = new ClassPathXmlApplicationContext("application.xml");
        //2.从IOC容器里面获取id="bookService"对象
        StudentService studentService = (StudentService) ac.getBean("studentService");
        //3.执行对象方法
        studentService.save();
        //4.关闭容器
        ac.close();
    }
  • 控制台结果

4 依赖自动装配【理解】

4.1 自动装配概念

  • IoC容器根据bean所依赖的资源在容器中自动查找并注入到bean中的过程称为自动装配

  • 自动装配方式
    • 按类型

    • 按名称

    • 按构造方法

autowire: 自动装配, 在容器中找对应对象,自动给成员变量赋值
    byType: 通过类型注入
    byName: 通过名字注入
    constructor: 通过构造器注入
    no: 不自动注入

4.2 自动装配类型

4.2.1 依赖类型自动装配

配置中使用bean标签autowire属性设置自动装配的类型byType

使用按类型装配时(byType)必须保障容器中相同类型的bean唯一,推荐使用

格式:

<!--给成员变量赋值
    autowire: 自动装配, 在容器中找对应对象,自动给成员变量赋值
        byType: 通过类型注入
        byName: 通过名字注入
        constructor: 通过构造器注入
        no: 不自动注入
-->
<bean class="com.zbbmeta.service.impl.StudentServiceImpl" id="studentService" autowire="byType">
</bean>

4.2.2依赖bean容器名字自动装配

配置中使用bean标签autowire属性设置自动装配的类型byName

使用按名称装配时(byName)必须保障容器中具有指定名称的bean,不推荐使用

<!--
autowire="byType" 根据成员属性名自动注入
-->
<bean class="com.zbbmeta.service.impl.StudentServiceImpl" id="studentService2" autowire="byName">
</bean>

4.2.3 依赖bean容器根据构造器自动装配注入

配置中使用bean标签autowire属性设置自动装配的类型constructor

<!--
autowire="constructor" 
据成员的所属类型去IOC容器中查找一样类型的对象进行调用构造函数进行给成员赋值
-->
<bean class="com.zbbmeta.service.impl.StudentServiceImpl" id="studentService3" autowire="constructor">
</bean>

4.3 依赖自动装配代码实现

【第0步】创建11_2_DI_Autowire项目
【第1步】导入依赖坐标
【第2步】导入Student实体类
【第3步】定义Spring管理的类(接口)
【第4步】创建Spring配置文件在resources目录下创建`application.xml`
【第5步】在test目录下创建`StudentServiceTest`

【第0步】创建11_2_DI_Autowire项目

【第1步】导入Spring坐标

和之前项目依赖一致

【第1步】导入依赖坐标

和之前一致

【第3步】定义Spring管理的类(接口)

  • StudentDao接口和StudentDaoImpl实现类

package com.zbbmeta.dao;

public interface StudentDao {
    /**
     * 添加学生
     */
    void save();
}
package com.zbbmeta.dao.impl;

import com.zbbmeta.dao.StudentDao;

public class StudentDaoImpl implements StudentDao {

    @Override
    public void save() {
        System.out.println("DAO: 添加学生信息到数据库...");
    }
}
  • StudentService接口和StudentServiceImpl实现类

package com.zbbmeta.service;

public interface StudentService {
    /**
     * 添加学生
     */
    void save();
}

package com.zbbmeta.service.impl;

import com.zbbmeta.dao.StudentDao;
import com.zbbmeta.service.StudentService;

public class StudentServiceImpl implements StudentService {

    //创建成员对象
    private StudentDao studentDao ;

    //提供依赖对象对应的setter方法
    public void setStudentDao(StudentDao studentDao)     {
        this.studentDao = studentDao;
    }

    //构造函数
    StudentServiceImpl(){}
    public StudentServiceImpl(StudentDao studentDao)    {
        System.out.println("Service 构造器方法");
        this.studentDao=studentDao;
    }

    @Override
    public void save() {
        System.out.println("Service: 添加学生信息到数据库...");
        studentDao.save();
    }
}

【第4步】创建Spring配置文件在resources目录下创建application.xml

  • 定义application.xml配置文件

<?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 class="com.zbbmeta.dao.impl.StudentDaoImpl" id="studentDao">
    </bean>
    <!--
     autowire="byType" 根据类型自动注入【重点】
     成员属性:private StudentDao studentDao ;  根据成员的所属类型去IOC容器中查找一样类型的对象进行调用成员的setStudentDao(参数)注入数据
    -->
    <bean class="com.zbbmeta.service.impl.StudentServiceImpl" id="studentService" autowire="byType"></bean>


    <!--
autowire="byName" 根据成员属性名自动注入
成员属性:private StudentDao studentDao ;  根据成员的属性名字去IOC容器中查找一样名称的对象进行调用成员的setStudentDao(参数)注入数据

-->
    <bean class="com.zbbmeta.service.impl.StudentServiceImpl" id="studentService2" autowire="byName"></bean>
    
    <!--
   autowire="constructor" 
   成员属性:private StudentDao studentDao
   构造函数:public StudentServiceImpl(StudentDao studentDao){
        this.studentDao = studentDao;
   }
   据成员的所属类型去IOC容器中查找一样类型的对象进行调用构造函数进行给成员赋值
  -->
    <bean class="com.zbbmeta.service.impl.StudentServiceImpl" id="studentService3" autowire="constructor"></bean>
</beans>

**【第5步】在test目录下创建StudentServiceTest进行测试

package com.zbbmeta;

import com.zbbmeta.dao.StudentDao;
import com.zbbmeta.service.StudentService;
import org.junit.jupiter.api.Test;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class StudentServiceTest {

    //目标:根据类型自动注入
    @Test
    public void test1(){

        //1.根据配置文件application.xml创建IOC容器
        ClassPathXmlApplicationContext ac = new ClassPathXmlApplicationContext("application.xml");
        //2.从IOC容器里面获取id="studentService"对象
        StudentService studentService = (StudentService) ac.getBean("studentService");
        //3.执行对象方法
        studentService.save();
        //4.关闭容器
        ac.close();
    }


    //目标:测试自动注入根据名称查找注入
    @Test
    public void test2(){
        //1.根据配置文件application.xml创建IOC容器
        ClassPathXmlApplicationContext ac = new ClassPathXmlApplicationContext("application.xml");
        //2.从IOC容器里面获取id="bookService"对象
        StudentService studentService = (StudentService) ac.getBean("studentService2");
        //3.执行对象方法
        studentService.save();
        //4.关闭容器
        ac.close();
    }


    //目标:测试构造器根据名称查找注入
    @Test
    public void test3(){
        //1.根据配置文件application.xml创建IOC容器
        ClassPathXmlApplicationContext ac = new ClassPathXmlApplicationContext("application.xml");
        //2.从IOC容器里面获取id="bookService"对象
        StudentService studentService = (StudentService) ac.getBean("studentService3");
        //3.执行对象方法
        studentService.save();
        //4.关闭容器
        ac.close();
    }
}

5 集合注入

5.1 注入数组类型数据

格式:

<!--调用setArray方法给成员array赋值-->
<property name="array">
    <array>
        <!--new String("数据") 引用类型赋值-->
        <bean class="java.lang.String" id="s">
            <constructor-arg value="100"></constructor-arg>
        </bean>
        <!--简单类型赋值-->
        <value>200</value>
        <value>300</value>
    </array>
</property>

5.2 注入List类型数据

格式:

<!--调用setList方法给成员list赋值-->
<property name="list">
    <list>
        <value>张三</value>
        <value>李四</value>
        <value>王五</value>
    </list>
</property>

5.3 注入Set类型数据

格式:

<!--调用setSet方法给成员set赋值-->
<property name="set">
    <set>
        <value>珠海</value>
        <value>江门</value>
        <value>惠州</value>
    </set>
</property>

5.4 注入Map类型数据

<!--调用setMap方法给成员map赋值-->
<property name="map">
    <map>
        <entry key="country" value="china"></entry>
        <entry key="province" value="广东"></entry>
        <entry key="city" value="广州"></entry>
    </map>
</property>

5.5 注入Properties类型数据

<!--调用setMap方法给成员map赋值-->
<property name="properties">
    <props>
        <prop key="country">china</prop>
        <prop key="province">广东</prop>
        <prop key="city">广州</prop>
    </props>
</property>

说明:property标签表示setter方式注入,构造方式注入constructor-arg标签内部也可以写<array>、<list>、<set>、<map>、<props>标签

5.6 集合注入完整代码

【第0步】在11_3_DI_Autowired的entity包下创建Person类
【第1步】创建Spring配置文件在resources目录下创建`application-person.xml`
【第2步】在test目录下创建`PersonTest`

【第0步】在11_3_DI_Autowired的entity包下创建Person类

package com.zbbmeta.entity;

import java.util.*;

public class Person {
    private String[] array;
    private List<String> list;
    private Set<String> set;
    private Map<String,Object> map;
    private Properties properties;


    public Person() {
    }

    public Person(String[] array, List<String> list, Set<String> set, Map<String, Object> map, Properties properties) {
        this.array = array;
        this.list = list;
        this.set = set;
        this.map = map;
        this.properties = properties;
    }

    /**
     * 获取
     * @return array
     */
    public String[] getArray() {
        return array;
    }

    /**
     * 设置
     * @param array
     */
    public void setArray(String[] array) {
        this.array = array;
    }

    /**
     * 获取
     * @return list
     */
    public List<String> getList() {
        return list;
    }

    /**
     * 设置
     * @param list
     */
    public void setList(List<String> list) {
        this.list = list;
    }

    /**
     * 获取
     * @return set
     */
    public Set<String> getSet() {
        return set;
    }

    /**
     * 设置
     * @param set
     */
    public void setSet(Set<String> set) {
        this.set = set;
    }

    /**
     * 获取
     * @return map
     */
    public Map<String, Object> getMap() {
        return map;
    }

    /**
     * 设置
     * @param map
     */
    public void setMap(Map<String, Object> map) {
        this.map = map;
    }

    /**
     * 获取
     * @return properties
     */
    public Properties getProperties() {
        return properties;
    }

    /**
     * 设置
     * @param properties
     */
    public void setProperties(Properties properties) {
        this.properties = properties;
    }

    @Override
    public String toString() {
        return "User{" +
                "array=" + Arrays.toString(array) +
                ", list=" + list +
                ", set=" + set +
                ", map=" + map +
                ", properties=" + properties +
                '}';
    }
}

【第1步】创建Spring配置文件在resources目录下创建application-person.xml

<?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">
    <!--
    目标:创建User对象并依赖注入赋值
    -->
    <bean class="com.zbbmeta.entity.Person" id="person">
        <!--调用setArray方法给成员array赋值-->
        <property name="array">
            <array>
                <!--new String("数据") 引用类型赋值-->
                <bean class="java.lang.String" id="s">
                    <constructor-arg value="100"></constructor-arg>
                </bean>
                <!--简单类型赋值-->
                <value>200</value>
                <value>300</value>
            </array>
        </property>
        <!--调用setList方法给成员list赋值-->
        <property name="list">
            <list>
                <value>张三</value>
                <value>李四</value>
                <value>王五</value>
            </list>
        </property>
        <!--调用setSet方法给成员set赋值-->
        <property name="set">
            <set>
                <value>珠海</value>
                <value>江门</value>
                <value>惠州</value>
            </set>
        </property>
        <!--调用setMap方法给成员map赋值-->
        <property name="map">
            <map>
                <entry key="country" value="china"></entry>
                <entry key="province" value="广东"></entry>
                <entry key="city" value="广州"></entry>
            </map>
        </property>
        <!--调用setMap方法给成员map赋值-->
        <property name="properties">
            <props>
                <prop key="country">china</prop>
                <prop key="province">广东</prop>
                <prop key="city">广州</prop>
            </props>
        </property>
    </bean>
</beans>

【第2步】在test目录下创建PersonTest

package com.zbbmeta;

import com.zbbmeta.entity.Person;
import org.junit.jupiter.api.Test;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class PersonTest {
    @Test
    public void test(){

        //1.创建IOC容器
        ClassPathXmlApplicationContext ac = new ClassPathXmlApplicationContext("application-person.xml");

        //2.获取对象
        Person person = ac.getBean(Person.class); //根据指定类型去IOC容器中查找对象

        //3.打印对象
        System.out.println(person);

        //4.关闭容器
        ac.close();
    }
}

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/849236.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

lwip不同的socket分别作为监听和客户端连接

在LWIP中&#xff0c;一个网络设备&#xff08;如以太网卡&#xff09;可以创建多个socket&#xff0c;用于处理不同的网络连接。一般&#xff0c;你可以创建一个socket用于监听&#xff08;listen&#xff09;连接&#xff0c;另一个socket用于主动发起&#xff08;connect&am…

本地化部署自建类ChatGPT服务远程访问

本地化部署自建类ChatGPT服务远程访问 文章目录 本地化部署自建类ChatGPT服务远程访问前言系统环境1. 安装Text generation web UI2.安装依赖3. 安装语言模型4. 启动5. 安装cpolar 内网穿透6. 创建公网地址7. 公网访问8. 固定公网地址 &#x1f340;小结&#x1f340; 前言 Te…

【ARM64 常见汇编指令学习 15 -- ARM 标志位的学习】

文章目录 ARM 标志位介绍Zero Condition flag(零标志位)零标志位判断实例 上篇文章&#xff1a;ARM64 常见汇编指令学习 14 – ARM 汇编 .balign,.balignw,.balign 伪指令学习 下篇文章&#xff1a;ARM64 常见汇编指令学习 16 – ARM64 SMC 指令 ARM 标志位介绍 在ARM架构中&am…

医学图像处理

医学图像处理 opencv批量分片高像素图像病理图像数据增强提升样本多样性基于 imgaug、skimage 实现色彩增强 降低样本多样性基于 DCGAN、TransposeConv 完成染色标准化 精细化提取特征自动生成数据标注 分类场景下的医学图像分析分割场景下的医学图像分析检测场景下的医学图像分…

揭秘Word高级技巧:事半功倍的文字处理策略

Microsoft Word是一款广泛使用的文字处理软件&#xff0c;几乎每个人都有使用过它的经历。但是&#xff0c;你是否知道Word中隐藏着许多高级技巧和功能&#xff0c;可以帮助你事半功倍地处理文字&#xff1f;在本文中&#xff0c;我们将揭秘一些Word的高级技巧&#xff0c;让你…

[] Adobe XD免费版功能一览,设计师们速来免费使用!

Adobe XD 作为一款流行的原型设计工具&#xff0c;免费使用对许多设计师来说是非常重要的。但现在 Adobe XD 的免费版体验已经不是那么舒适了。别担心&#xff0c;本文将为你推荐真正免费好用的 Adobe XD 替代工具。 Adobe XD 是免费的吗&#xff1f; Adobe XD 在早期确实是完…

Licheepi Nano屏幕驱动并输出打印信息

Licheepi Nano买回来好长时间&#xff0c;没咋玩&#xff0c;最近看了一个利用F1C100S自制迷你电脑的博客&#xff0c;里面主要参考的就是Licheepi Nano。我打算先在Licheepi Nano上完成屏幕操作、Debian文件系统和USB键盘等内容&#xff0c;这里介绍怎样利用Licheepi Nano外接…

Java进阶(1)——JVM的内存分配 反射Class类的类对象 创建对象的几种方式 类加载(何时进入内存JVM) 注解 反射+注解的案例

目录 引出java内存分配java内存分布概略图堆方法区常量池 创建对象内存分配 反射class文件的底层类加载顺序1.检查2.开辟静态资源空间3.常量池4.其他...5.创建一个唯一的类的对象获取Class对象的几种方式 创建对象几种方式new 看到new : new Book()反射 Class.forName(“包名.类…

断路器回路电阻试验

试验目的 断路器回路电阻主要取决于断路器动、 静触头的接触电阻, 其大小直接影响正常 运行时的发热情况及切断短路电流的性能, 是反应安装检修质量的重要数据。 试验设备 回路电阻测试仪 厂家&#xff1a; 湖北众拓高试代销 试验接线 对于单断口的断路器, 通过断口两端的接线…

8年测试经验,接口测试总结,测试进阶之路一篇打通...

目录&#xff1a;导读 前言一、Python编程入门到精通二、接口自动化项目实战三、Web自动化项目实战四、App自动化项目实战五、一线大厂简历六、测试开发DevOps体系七、常用自动化测试工具八、JMeter性能测试九、总结&#xff08;尾部小惊喜&#xff09; 前言 1、什么是接口测试…

开源数据库Mysql_DBA运维实战 (部署服务篇)

前言❀ 1.数据库能做什么 2.数据库的由来 数据库的系统结构❀ 1.数据库系统DBS 2.SQL语言(结构化查询语言) 3.数据访问技术 部署Mysql❀ 1.通过rpm安装部署Mysql 2.通过源码包安装部署Mysql 前言❀ 1.数据库能做什么 a.不论是淘宝&#xff0c;吃鸡&#xff0c;爱奇艺…

k8sday02

第四章 实战入门 本章节将介绍如何在kubernetes集群中部署一个nginx服务&#xff0c;并且能够对其进行访问。 Namespace ​ Namespace是kubernetes系统中的一种非常重要资源&#xff0c;它的主要作用是用来实现多套环境的资源隔离或者多租户的资源隔离。 ​ 默认情况下&…

机器学习实战1-kNN最近邻算法

文章目录 机器学习基础机器学习的关键术语 k-近邻算法&#xff08;KNN&#xff09;准备&#xff1a;使用python导入数据实施kNN分类算法示例&#xff1a;使用kNN改进约会网站的配对效果准备数据&#xff1a;从文本文件中解析数据分析数据准备数据&#xff1a;归一化数值测试算法…

淘宝API接口为开发者提供了与淘宝平台进行数据交互和操作的便捷途径

淘宝API接口是指淘宝开放平台提供的一套接口&#xff0c;用于与淘宝网进行数据交互和操作。通过使用淘宝API接口&#xff0c;第三方开发者可以实现商品搜索、店铺信息获取、订单管理、商家服务等功能&#xff0c;从而实现与淘宝平台的对接和数据共享。 淘宝API接口的使用可以帮…

C语言赋值号的运算顺序

从右到左。 int & f(int & a) { printf("参数 %d\n", a); return a; } int main(void) {int a 9;int b 3;f(a) f(b);// 运行到此处&#xff0c;a 3&#xff0c;b 3return 0; } 输出

卡尔曼滤波算法demo

代码 learn_kalman.py #codingutf-8 import numpy as np import time from kinematic_model import freedrop from controller import kalman_filterimport matplotlib.pyplot as plt # 支持中文 import matplotlib as mpl mpl.rcParams[font.family]SimHei plt.rcParams[a…

每天一道leetcode:剑指Offer 25.合并两个链表

今日份题目&#xff1a; 输入两个递增排序的链表&#xff0c;合并这两个链表并使新链表中的节点仍然是递增排序的。 示例 输入&#xff1a;1->2->4, 1->3->4 输出&#xff1a;1->1->2->3->4->4 提示 0 < 链表长度 < 1000 题目思路 递归…

BL302嵌入式ARM控制器:高性能处理器驱动的储能优化利器

嵌入式ARM控制器钡铼技术BL302系列是工业级坚固型工业控制器&#xff0c;采用NXP的高性能处理器I.MX6ULL&#xff0c;搭配先进的ARM Cortex-A7构架&#xff0c;运行速度高达800MHz&#xff0c;具有高度的稳定性。本产品最高可提供4路RS485/RS232&#xff0c;1路CAN口&#xff0…

嵌入式开发学习(STC51-13-温度传感器)

内容 通过DS18B20温度传感器&#xff0c;在数码管显示检测到的温度值&#xff1b; DS18B20介绍 简介 DS18B20是由DALLAS半导体公司推出的一种的“一线总线&#xff08;单总线&#xff09;”接口的温度传感器&#xff1b; 与传统的热敏电阻等测温元件相比&#xff0c;它是一…

SpringBoot整合达梦数据库

近期接到了一个需要国产化的项目&#xff0c;用到了达梦数据库&#xff0c;没想到一开始配置就出现了问题&#xff0c;下面把配置给大家粘贴出来&#xff0c;大家少踩点坑。 一、先下载达梦数据库 这是达梦数据库下载链接&#xff0c;达梦数据库没有免费的&#xff0c;个人好…