Spring5框架完整(三)注入属性的三种方式(set方法、构造器、p名称空间:本质还是set方法)

一、通过set方法注入属性

Book类

public class Book {
    private String bname;
    private String bauthor;

    public void setBname(String bname) {
        this.bname = bname;
    }

    public void setBauthor(String bauthor) {
        this.bauthor = bauthor;
    }

    @Override
    public String toString() {
        return "Book{" +
                "bname='" + bname + '\'' +
                ", bauthor='" + bauthor + '\'' +
                '}';
    }
}

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 id="book" class="com.atguigu.SpringIOC.Book">
        <property name="bname" value="西游记"></property>
        <property name="bauthor" value="PMH"></property>
    </bean>
</beans>

测试

public class Test1 {
    @org.junit.Test
    public void test(){
        //通过读取配置文件的方式创建对象
        ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("bean.xml");
        Book book = context.getBean("book", Book.class);
        System.out.println(book.toString());
    }
}

结果
在这里插入图片描述

二、通过构造器注入属性

注意问题:当bean中没有标签constructor-arg默认采用空参构造器创建对象,只有有标签constructor-arg才会使用对应构造器
在这里插入图片描述
对应构造器A
以上bean对应下面的构造器
在这里插入图片描述
具体代码:
Book类

public class Book {
    private String bname;
    private String bauthor;

    public Book(String bname, String bauthor) {
        this.bname = bname;
        this.bauthor = bauthor;
    }

    @Override
    public String toString() {
        return "Book{" +
                "bname='" + bname + '\'' +
                ", bauthor='" + bauthor + '\'' +
                '}';
    }
}

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 id="book" class="com.atguigu.SpringIOC.Book">
        <constructor-arg name="bname" value="西游记"></constructor-arg>
        <constructor-arg name="bauthor" value="PMH"></constructor-arg>
    </bean>
</beans>

测试

public class Test1 {
    @org.junit.Test
    public void test(){
        //通过读取配置文件的方式创建对象
        ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("bean.xml");
        Book book = context.getBean("book", Book.class);
        System.out.println(book.toString());
    }
}

结果:
在这里插入图片描述

三、通过p名称空间注入属性(本质还是使用set方法)

具体代码:
Book类

public class Book {
    private String bname;
    private String bauthor;

    public void setBname(String bname) {
        this.bname = bname;
    }

    public void setBauthor(String bauthor) {
        this.bauthor = bauthor;
    }

    @Override
    public String toString() {
        return "Book{" +
                "bname='" + bname + '\'' +
                ", bauthor='" + bauthor + '\'' +
                '}';
    }
}

新增了p名称空间,注意这边xsi:schemaLocation=“这边不需要新增,我当时加了反而多次一举

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

    <bean id="book" class="com.atguigu.SpringIOC.Book" p:bname ="西游记" p:bauthor = "PMH">

    </bean>
</beans>

测试

public class Test1 {
    @org.junit.Test
    public void test(){
        //通过读取配置文件的方式创建对象
        ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("bean.xml");
        Book book = context.getBean("book", Book.class);
        System.out.println(book.toString());
    }
}

测试结果
在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值