02-Spring5用xml为bean注入属性值

1.setter方法注入

1.编写一个测试类, 注意对应属性一定要有setter方法,不然报错

package com.limi.test;

public class User {
    private String name;
    private Integer age;
    private String  hoby;
    public User(){
    }
    public User(String name, Integer age){
        this.name = name;
        this.age = age;
    }
    public void say(){
        System.out.println("name="+name+" age="+age+" hoby="+hoby);
    }
    public void setName(String name) {
        this.name = name;
    }
    public void setAge(Integer age) {
        this.age = age;
    }
	public void setHoby(String hoby) {
        this.hoby = hoby;
    }
}

2.在bean的xml文件中使用setter注入

<?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">
    <!--id是唯一标识, class是全类名    -->
    <bean id="User" class="com.limi.test.User">
        <!-- set方法注入属性,类里面一定要有setter,不然报错-->
        <property name="name" value="andy"></property>
        <property name="age" value="10"></property>
    </bean>
</beans>

3.测试

package com.limi.test;

import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class MyTest {
    @Test
    public void test1(){
        //1.加载bean的xml文件, 以src为根目录
        ApplicationContext context = new ClassPathXmlApplicationContext("bean1.xml");

        //2.获取配置的对象, 参数1:bean的id值, 参数2: 类名.class
        User user = context.getBean("User", User.class);

        //3.使用User对象
        user.say();
    }
}

在这里插入图片描述

2.有参构造方法注入

1.在bean的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">
    <!--id是唯一标识, class是全类名    -->
    <bean id="User" class="com.limi.test.User">
        <!-- 使用有参构造注入属性-->
        <constructor-arg  name="name" value="bob"></constructor-arg>
        <!--也可以使用这种方式,index属性表示构造方法里的第几个参数(从0开始)-->
        <constructor-arg index="1" value="99"></constructor-arg>
    </bean>
</beans>

2.测试, 测试类和上面相同
在这里插入图片描述

3.p 名称空间注入(用的少)

1.使用 p 名称空间注入,可以一个标签一次性注入多个值, 简化配置方式

<?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"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
        <!--上面加入个xmlns:p="http://www.springframework.org/schema/p"
            添加 p 名称空间在配置文件中, 注意只能写p, 别的不行
        -->
    <!--id是唯一标识, class是全类名-->
    <bean id="User" class="com.limi.test.User" p:name="lo" p:age="88" p:hoby="ball"></bean>
</beans>

2.测试, 测试类和上面相同
在这里插入图片描述

4.特殊值的注入

<?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">
    <!--id是唯一标识, class是全类名    -->
    <bean id="User" class="com.limi.test.User">
        <!--特殊字符的注入不能直接书写,需把内容写<![CDATA[]]>里面-->
        <property name="name">
            <value><![CDATA[\n\n]]></value>
        </property>
        <property name="hoby">
            <value><![CDATA[<<basketball>>]]></value>
        </property>

        <!--空值, 使用<null>标签-->
        <property name="age">
            <null></null>
        </property>
    </bean>
</beans>

2.测试, 测试类和上面相同
在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值