spring3 DI依赖注入

本文详细介绍了Spring框架中Bean的依赖注入,包括通过XML配置文件进行属性、数组、集合以及Properties的注入,并展示了两种不同的注入方式。同时,文章还探讨了Bean的两种作用域,即单例和原型模式,解释了它们的关键条件和应用场景。
摘要由CSDN通过智能技术生成

1 set方式注入

  • 依赖:bean对象的创建依赖于容器
  • 注入:bean对象中的所有属性,由容器来注入

创建实体类【使用lombok jar包】

Student实体类

import java.util.*;
@Data
@AllArgsConstructor //全参构造
@NoArgsConstructor // 无参构造
public class Student {
    private String name; 
    private Address address;
    private String[] books;
    private List<String> hobbys;
    private Map<String,String> card;
    private Set<String> games;
    private String wife;
    private Properties info;
}

Address实体类

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

@Data
@AllArgsConstructor
@NoArgsConstructor
public class Address {
    private String address;
}
  • 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
        https://www.springframework.org/schema/beans/spring-beans.xsd">

    <bean id="address" class="com.xgh.pojo.Address">
        <property name="address" value="上海"/>
    </bean>

    <bean id="student" class="com.xgh.pojo.Student">
        <property name="name" value="xgh"/>
        <!--注入类-->
        <property name="address" ref="address"/>

        <!--注入数组-->
        <property name="books">
            <array>
                <value>三国演义</value>
                <value>水浒传</value>
                <value>西游记</value>
                <value>红楼梦</value>
            </array>
        </property>

        <!--注入List<>集合对象-->
        <property name="hobbys">
            <list>
                <value>game</value>
                <value>code</value>
                <value>lianai</value>
                <value>wan</value>
            </list>
        </property>

        <!--注入Map集合-->
        <property name="card">
            <map>
                <entry key="张三" value="123"/>
                <entry key="李四" value="456"/>
                <entry key="王二" value="789"/>
                <entry key="赵六" value="135"/>
            </map>
        </property>

        <!--注入Set集合-->
        <property name="games">
            <set>
                <value>吃鸡</value>
                <value>王者</value>
                <value>lol</value>
                <value>穿越</value>
            </set>
        </property>

        <!--注入wife指针-->
        <!--方式一-->
<!--        <property name="wife">-->
<!--            <null/>-->
<!--        </property>-->

        <!--方式二-->
        <property name="wife" value=""/>

        <!--注入Properties文件-->
        <property name="info">
            <props>
                <prop key="学号">1234567</prop>
                <prop key="姓名">法外狂徒张三</prop>
                <prop key="性别"></prop>
            </props>
        </property>
    </bean>
</beans>
  • 测试类
public class SpringTest {
    public static void main(String[] args) {
       ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
        Student student = (Student) context.getBean("student");
        String s = student.toString();
        System.out.println(s);
    }
}

结果展示
在这里插入图片描述

拓展方式注入【p c】

创建实体类:

user实体类

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

@Data
@AllArgsConstructor //全参构造
@NoArgsConstructor // 无参构造
public class User {
    private String name;
    private int age;
}

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"
       xmlns:p="http://www.springframework.org/schema/p"
       xmlns:c="http://www.springframework.org/schema/c"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
        https://www.springframework.org/schema/beans/spring-beans.xsd">

    <!--方式一 通过p命名空间注入,可以直接注入属性的值,property 通过无参构造的方式-->
    <bean id="user1" class="com.xgh.pojo.User" p:age="12" p:name="xgh"/>

    <!--方式二 通过c命名空间注入,通过构造器注入:constructor-args-->
    <bean id="user2" class="com.xgh.pojo.User" c:name="法外狂徒张三" c:age="12"/>

</beans>
  • 注意
    需要添加两个外部引用
 xmlns:p="http://www.springframework.org/schema/p"
xmlns:c="http://www.springframework.org/schema/c"

测试

public class Spring_2 {
    public static void main(String[] args) {
        ApplicationContext context = new ClassPathXmlApplicationContext("userbean.xml");
        User users = context.getBean("user2", User.class);
        System.out.println(users);
    }
}

结果展示
在这里插入图片描述

3 bean的作用域

  • 1 单例模式(Spring默认机制)
    关键条件 scope="singleton"
<bean id="user1" class="com.xgh.pojo.User" p:age="12" p:name="xgh" scope="singleton"/>
  • 2 原型模式:每一次从容器中get的时候,都会产生一个新的对象
    关键条件 scope="prototype"
<bean id="user1" class="com.xgh.pojo.User" p:age="12" p:name="xgh" scope="prototype"/>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值