首页 > 其他分享 >spring —— IoC 容器(一)

spring —— IoC 容器(一)

时间:2024-07-22 13:25:21浏览次数:13  
标签:user 容器 name spring age User IoC public String

IoC 不是一种技术,而是一种设计思想,旨在降低代码之间的耦合性。Spring 通过 IoC 容器管理所有 Java 对象的实例化和初始化,控制对象与对象之间的依赖关系。

一、基于 XML 管理 bean

(一)通过 XML 获取 bean

public class User {
    private String name;
    private int age;

    public User(String name, int age) {
        this.name = name;
        this.age = age;
    }

    public User(){
       
    }

    public void setName(String name) {        
        this.name = name;
    }
    public void setAge(int age) {
        this.age = age;
    }
}
public class TestUser {
    @Test
    public void userTest(){
        ApplicationContext context = new ClassPathXmlApplicationContext("bean.xml");
        User user = (User) context.getBean("user");
        System.out.println(user);
    }
}
<?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 id="user" class="com.fourth.user.User">       
    </bean>
</beans>

(二)使用 property 注入属性 

<?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 id="user" class="com.fourth.user.User">
        <property name="name" value="tom"></property>
        <property name="age" value="20"></property>
    </bean>
</beans>

 此时在 TestUser 中获取bean,其实是通过无参构造和 set 方法注入。

(三)使用 constructor-arg 注入属性

<?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 id="user" class="com.fourth.user.User">
        <constructor-arg name="name" value="tom"></constructor-arg>
        <constructor-arg name="age" value="20"></constructor-arg>
    </bean>
</beans>

 此时在 TestUser 中获取bean,其实是通过有参构造进行注入。

(四)注入对象类型属性

1、外部 bean 注入

package com.fourth.user;

public class Person {
    private String pid;

    public Person(){}

    public void setPid(String pid) {
        this.pid = pid;
    }

    public Person(String pid) {
        this.pid = pid;
    }
}
package com.fourth.user;

public class User {
    private String name;
    private int age;
    private Person person;

    public User(String name, int age, Person person) {
        this.name = name;
        this.age = age;
        this.person = person;
    }

    public void setName(String name) {
       this.name = name;
    }
    public void setAge(int age) {
        this.age = age;
    }
    public void setPerson(Person person) {
        this.person = person;
    }
}
<?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 id="person" class="com.fourth.user.Person">
        <property name="pid" value="10010"></property>
    </bean>

    <bean id="user" class="com.fourth.user.User">
        <constructor-arg name="name" value="tom"></constructor-arg>
        <constructor-arg name="age" value="20"></constructor-arg>
        <constructor-arg name="person" ref="person"></constructor-arg>
    </bean>
</beans>

外部 bean 注入对象类型属性,通过 ref 进行引入。

2、内部 bean 注入 

<?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 id="user" class="com.fourth.user.User">
        <constructor-arg name="name" value="tom"></constructor-arg>
        <constructor-arg name="age" value="20"></constructor-arg>
        <constructor-arg name="person">
            <bean id="person" class="com.fourth.user.Person">
                <property name="pid" value="10010"></property>
            </bean>
        </constructor-arg>
    </bean>
</beans>

 3、级联属性注入

<?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 id="person" class="com.fourth.user.Person">
        <constructor-arg name="pid" value="tom"></constructor-arg>
    </bean>

    <bean id="user" class="com.fourth.user.User">
        <constructor-arg name="name" value="tom"></constructor-arg>
        <constructor-arg name="age" value="20"></constructor-arg>
        <constructor-arg name="person" ref="person"></constructor-arg>
        <property name="person.pid" value="jerry"></property>
    </bean>
</beans>

或者: 

<?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 id="user" class="com.fourth.user.User">
        <constructor-arg name="name" value="tom"></constructor-arg>
        <constructor-arg name="age" value="20"></constructor-arg>
        <constructor-arg name="person" >
            <bean id="person" class="com.fourth.user.Person">
                <constructor-arg name="pid" value="tom"></constructor-arg>
            </bean>
        </constructor-arg>
        <property name="person.pid" value="jerry"></property>
    </bean>
</beans>

  级联属性注入,首先通过外部 bean 或者内部 bean 进行引入,然后通过 value 更改对象内部的属性。

(五)注入数组类型属性

package com.fourth.user;

public class User {
    private String name;
    private int age;
    private String[] hobby;

    public User(String name, int age, String[] hobby) {
        this.name = name;
        this.age = age;
        this.hobby = hobby;
    }

    public User() {
        
    }

    public void setName(String name) {
       this.name = name;
    }
    public void setAge(int age) {
        this.age = age;
    }
    public void setHobby(String[] hobby) {
        this.hobby = hobby;
    }

    public String getName() {
        return name;
    }
    public int getAge() {
        return age;
    }
    public String[] getHobby() {
        return hobby;
    }
}
package com.fourth.test;

import com.fourth.user.User;
import org.junit.jupiter.api.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import java.util.Arrays;

public class TestUser {
    @Test
    public void userTest(){
        ApplicationContext context = new ClassPathXmlApplicationContext("bean.xml");
        User user = (User) context.getBean("user");
        System.out.println(Arrays.toString(user.getHobby()));
    }
}
<?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 id="user" class="com.fourth.user.User">
        <constructor-arg name="name" value="tom"></constructor-arg>
        <constructor-arg name="age" value="20"></constructor-arg>
        <constructor-arg name="hobby" >
            <array>
                <value>eating</value>
                <value>sleeping</value>
                <value>playing</value>
            </array>
        </constructor-arg>
    </bean>
</beans>

 本案例中,hobby 是一个 String 类型数组,对其进行属性注入通过 array 标签进行。

(六)注入 list 类型属性

package com.fourth.user;

import java.util.List;

public class User {
    private String name;
    private int age;
    private List<Person> personList;

    public User(String name, int age, List<Person> personList) {
        this.name = name;
        this.age = age;
        this.personList = personList;
    }

    public User() {
        
    }

    public void setName(String name) {
       this.name = name;
    }
    public void setAge(int age) {
        this.age = age;
    }
    public void setPersonList(List<Person> personList) {
        this.personList = personList;
    }

    public String getName() {
        return name;
    }
    public int getAge() {
        return age;
    }
    public List<Person> getPersonList() {
        return personList;
    }
}
package com.fourth.user;

public class Person {

    private String pid;

    public Person(){}

    public void setPid(String pid) {
        this.pid = pid;
    }

    public Person(String pid) {

        this.pid = pid;
    }

    public String getPid() {
        return pid;
    }
}
package com.fourth.test;

import com.fourth.user.Person;
import com.fourth.user.User;
import org.junit.jupiter.api.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class TestUser {
    @Test
    public void userTest(){
        ApplicationContext context = new ClassPathXmlApplicationContext("bean.xml");
        User user = (User) context.getBean("user");
        for(Person person:user.getPersonList()){
            System.out.println(person.getPid());
        }
    }
}
<?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 id="person1" class="com.fourth.user.Person">
        <property name="pid" value="10011"></property>
    </bean>
    <bean id="person2" class="com.fourth.user.Person">
        <property name="pid" value="10086"></property>
    </bean>

    <bean id="user" class="com.fourth.user.User">
        <constructor-arg name="name" value="tom"></constructor-arg>
        <constructor-arg name="age" value="20"></constructor-arg>
        <constructor-arg name="personList" >
            <list>
                <ref bean="person1"></ref>
                <ref bean="person2"></ref>
            </list>
        </constructor-arg>
    </bean>
</beans>

(七)注入 map 类型属性 

package com.fourth.user;

import java.util.Map;

public class User {
    private String name;
    private int age;
    private Map<String,Person> personMap;

    public User(String name, int age, Map<String,Person> personMap) {
        this.name = name;
        this.age = age;
        this.personMap = personMap;
    }

    public User() {
        
    }

    public void setName(String name) {
       this.name = name;
    }
    public void setAge(int age) {
        this.age = age;
    }
    public void setPersonMap(Map<String,Person> personMap) {
        this.personMap = personMap;
    }

    public String getName() {
        return name;
    }
    public int getAge() {
        return age;
    }
    public Map<String,Person> getPersonMap() {
        return personMap;
    }
}
package com.fourth.user;

public class Person {

    private String pid;

    public Person(){}

    public void setPid(String pid) {
        this.pid = pid;
    }

    public Person(String pid) {

        this.pid = pid;
    }

    public String getPid() {
        return pid;
    }
}
package com.fourth.test;

import com.fourth.user.User;
import org.junit.jupiter.api.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class TestUser {
    @Test
    public void userTest(){
        ApplicationContext context = new ClassPathXmlApplicationContext("bean.xml");
        User user = (User) context.getBean("user");
        for(String key:user.getPersonMap().keySet()){
            System.out.println(user.getPersonMap().get(key).getPid());
        }
    }
}
<?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 id="person1" class="com.fourth.user.Person">
        <property name="pid" value="10011"></property>
    </bean>
    <bean id="person2" class="com.fourth.user.Person">
        <property name="pid" value="10086"></property>
    </bean>

    <bean id="user" class="com.fourth.user.User">
        <constructor-arg name="name" value="tom"></constructor-arg>
        <constructor-arg name="age" value="20"></constructor-arg>
        <constructor-arg name="personMap" >
            <map>
                <entry>
                    <key>
                        <value>first</value>
                    </key>
                    <ref bean="person1"></ref>
                </entry>
                <entry>
                    <key>
                        <value>second</value>
                    </key>
                    <ref bean="person2"></ref>
                </entry>
            </map>
        </constructor-arg>
    </bean>
</beans>

(八)util 形式注入属性:对数组、list、map 的补充(1) 

以 map 类型为例:

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

    <bean id="person1" class="com.fourth.user.Person">
        <property name="pid" value="10011"></property>
    </bean>
    <bean id="person2" class="com.fourth.user.Person">
        <property name="pid" value="10086"></property>
    </bean>

    <bean id="user" class="com.fourth.user.User">
        <constructor-arg name="name" value="tom"></constructor-arg>
        <constructor-arg name="age" value="20"></constructor-arg>
        <constructor-arg name="personMap" ref="map"></constructor-arg>
    </bean>

    <util:map id="map">
        <entry>
            <key>
                <value>first</value>
            </key>
            <ref bean="person1"></ref>
        </entry>
        <entry>
            <key>
                <value>second</value>
            </key>
            <ref bean="person2"></ref>
        </entry>
    </util:map>
    
</beans>

 (九)p 命名空间注入属性:对数组、list、map 的补充(2)

 同样以 map 类型为例:

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

    <bean id="person1" class="com.fourth.user.Person">
        <property name="pid" value="10011"></property>
    </bean>
    <bean id="person2" class="com.fourth.user.Person">
        <property name="pid" value="10086"></property>
    </bean>

    <bean id="user" class="com.fourth.user.User" p:name="tom" p:age="20" p:personMap-ref="map">
    </bean>

    <util:map id="map">
        <entry>
            <key>
                <value>first</value>
            </key>
            <ref bean="person1"></ref>
        </entry>
        <entry>
            <key>
                <value>second</value>
            </key>
            <ref bean="person2"></ref>
        </entry>
    </util:map>

</beans>

 等于是不再用 property 或者 constructor-arg 注入属性,而是用 p 进行注入。

 

 

 

 

标签:user,容器,name,spring,age,User,IoC,public,String
From: https://blog.csdn.net/firstgrass/article/details/140599938

相关文章

  • Spring Boot 的无敌描述
    1/spring-boot-app2├──src3│ ├──main4│ │ ├──java5│ │ │ └──com6│ │ │   └──example7│ │ │     └──springbootapp8│ │ │       ├──Application.jav......
  • Mailspring邮件服务器如何配置做邮件管理?
    Mailspring邮件服务器性能调优的策略?如何部署服务器?Mailspring是一款功能强大的邮件客户端,支持多种邮件服务,同时具有直观的用户界面和丰富的功能。那么,如何配置Mailspring邮件服务器来进行邮件管理呢?AokSend将详细介绍相关步骤和技巧。Mailspring邮件服务器:下载安装可以从M......
  • SpringBoot原理解析(二)- Spring Bean的生命周期以及后处理器和回调接口
    SpringBoot原理解析(二)-SpringBean的生命周期以及后处理器和回调接口文章目录SpringBoot原理解析(二)-SpringBean的生命周期以及后处理器和回调接口1.Bean的实例化阶段1.1.Bean实例化的基本流程1.2.Bean实例化图例1.3.实例化阶段的后处理器1.3.1.实例化阶段后处理器......
  • 嵌入式C++、FreeRTOS、MySQL、Spring Boot和MQTT协议:智能零售系统详细流程介绍(代码示
    项目概述随着科技的发展,零售行业正经历着一场数字化转型。智能零售系统通过集成嵌入式技术和大数据分析,为商家提供了高效的运营管理工具。该系统的核心目标是提升顾客体验、优化库存管理、降低运营成本以及实现精准营销。本项目将结合多种技术栈,包括嵌入式硬件、嵌入式软件、......
  • SpringBoot+ Sharding Sphere 轻松实现数据库字段加解密
    一、介绍在实际的软件系统开发过程中,由于业务的需求,在代码层面实现数据的脱敏还是远远不够的,往往还需要在数据库层面针对某些关键性的敏感信息,例如:身份证号、银行卡号、手机号、工资等信息进行加密存储,实现真正意义的数据混淆脱敏,以满足信息安全的需要。那在实际的业务开发过程......
  • SpringBoot利用MyBatis连接Mysql数据库时常见启动报错
    目录报错情况报错情况一:​编辑报错情况二:解决步骤一、解决命名问题1.mapper层的id是否和Dao层的方法名字相同2.检查namespace与Dao层的文件地址相同二、解决注解问题1.检查Controller层的注解是否正确和完整2.Dao层或者Mapper层的注解3.pojo层:实体类层Data注解(用来......
  • 基于springboot的助农服务平台
    基于springboot的助农服务app介绍2024届软件工程毕业设计 该项目是基于springboot的助农App的设计及实现,主要实现了管理员,用户,商家三个端的设计,其中主要实现的功能有产品模块,订单模块,购物车模块,以及相关联的管理模块,秒杀等,帮助农民出售农作物,提高农业水平的发展,提高农民的收入,......
  • C++ STL常用容器之map(关联容器)
    文章目录前言一、map的介绍1.1使用map的优点1.2使用map的缺点1.3使用场景二、map常用的操作2.1创建、初始化以及遍历容器2.2查询容器大小2.3访问容器中的元素2.4往容器中添加元素2.5删除容器中的元素2.6清空容器中的元素三、扩展3.1红黑树的概念3.2红黑树的......
  • 用 300 行代码手写提炼 Spring 核心原理 [2]
    系列文章用300行代码手写提炼Spring核心原理[1]用300行代码手写提炼Spring核心原理[2]上文中我们实现了mini-spring的1.0版本,接下来我们在此基础上进行优化,将init()方法中的代码进行封装。按照之前的思路,先搭建基础框架,再“填肉注血”。初始化阶段in......
  • 解决spring后端传前端数值为空的问题
    问题:在开发当中,由于我的数据传输从DTO在某些场景下,其中的部分字段并不需求进行值的传递,但在其他功能当中需要;(比如开发题目模块时,查询题目采用同一接口,根据题目id不同,后台判断其为多选还是单选进行回传给dto给前端)。导致出现了如下情况的诸多null值,而这些是没有作用但又不可删除的......