Spring中Bean属性的依赖注入(四种方式)

什么是Bean属性的注入?就是对 一个对象的属性赋值`

构造方法注入

@Data
@AllArgsConstructor
@NoArgsConstructor
//必须有参无参构造方法,set get方法  toString方法
public class Car {
    private Integer id;
    private String name;
    private Double price;
}

applicationContext.xml配置文件

 <bean id="car" class="pojo.Car">
        <property name="id" value="100"/>
        <property name="name" value="山地车"/>
        <property name="price" value="1000"/>
    </bean>

测试

    @Test
    public void carTest() {
        //获取配置文件
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
        
        //传统写法
        Car car = new Car(1008,"单车",2d);
        System.out.println(car);

        //构造器属性注入
        Car car2 = applicationContext.getBean("car", Car.class);
        System.out.println(car2);
    }

set方法注入

@Data
@AllArgsConstructor
@NoArgsConstructor
//必须有参无参构造方法,set get方法  toString方法
public class Car {
    private Integer id;
    private String name;
    private Double price;
}

再创建一个Person对象

@Data
@AllArgsConstructor
@NoArgsConstructor
public class Person {
    private Integer id;
    private String name;
    private Car car;
    }

applicationContext.xml配置文件

<bean id="car" class="pojo.Car">
        <property name="id" value="100"/>
        <property name="name" value="山地车"/>
        <property name="price" value="1000"/>
    </bean>
    <bean id="person" class="pojo.Person">
        <property name="id" value="100"/>
        <property name="name" value="张三"/>
         <!--这里是引的car的bean对象-->
        <property name="car" ref="car"/>
    </bean>

测试

   @Test
    public void login() {
        //获取配置文件
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");

        //set属性注入
        Person person = applicationContext.getBean("person", Person.class);
        System.out.println(person);
    }

P命名空间注入

p名称空间出现的目地为了简化以上两种注入方式

引入p名称空间约束头

<beans xmlns="http://www.springframework.org/schema/beans"
       <!--添加p约束头-->
	   xmlns:p="http://www.springframework.org/schema/p"
       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">

applicationContext.xml配置文件

    <!--p名称空间属性注入-->
    <bean id="person2" class="pojo.Person" p:id="99" p:name="rose" p:car-ref="car" />

测试

   @Test
    public void login() {
        //获取配置文件
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");

        //P命名空间注入
        Person person = applicationContext.getBean("person2", Person.class);
        System.out.println(person);
    }

集合类型属性注入

@Data
public class ConllectionBean {

    private List<String> list;
    private Set<Integer> set;
    private Map<Integer,String> map;
    private Properties properties;

}

applicationContext.xml配置文件

<bean id="conllectionBean" class="pojo.ConllectionBean">
        <!--list-->
        <property name="list">
            <list>
                <value>aaa</value>
                <value>bbb</value>
                <value>ccc</value>
            </list>
        </property>

        <!--set-->
        <property name="set">
            <set>
                <value>11</value>
                <value>22</value>
                <value>33</value>
            </set>
        </property>

        <!--map-->
        <property name="map">
            <map>
                <entry key="11" value="aa"/>
                <entry key="22" value="bb"/>
                <entry key="33" value="cc"/>
            </map>
        </property>

        <!--properties-->
        <property name="properties">
            <props>
                <prop key="a">aa</prop>
                <prop key="b">bb</prop>
                <prop key="c">cc</prop>
            </props>
        </property>
    </bean>

测试

  @Test
    public void login() {
        //获取配置文件
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");

        //集合类型属性注入
        ConllectionBean conllectionBean = applicationContext.getBean("conllectionBean", ConllectionBean.class);
        System.out.println(conllectionBean);
    }
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值