【Spring】(3.5)依赖注入(扩展)

一、依赖注入扩展

该篇讲依赖注入的扩展,用其他的方式注入。

使用:p-namespace、c-namespace 简化代码。

项目结构:

在这里插入图片描述

创建实体类Student和Address。

public class Address {
    private String address;

    @Override
    public String toString() {
        return "Address{" +
                "address='" + address + '\'' +
                '}';
    }

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }
}
public class Student {
    private String name;
    /** 现住址 */
    private Address address;
    /** 籍贯 */
    private Address nativePlace;

    public Student() {
    }

    public Student(String name, Address address, Address nativePlace) {
        this.name = name;
        this.address = address;
        this.nativePlace = nativePlace;
    }

    @Override
    public String toString() {
        return "Student{" +
                "name='" + name + '\'' +
                ", address=" + address +
                ", nativePlace=" + nativePlace +
                '}';
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Address getAddress() {
        return address;
    }

    public void setAddress(Address address) {
        this.address = address;
    }

    public Address getNativePlace() {
        return nativePlace;
    }

    public void setNativePlace(Address nativePlace) {
        this.nativePlace = nativePlace;
    }
}

设置配置文件

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

    <!-- 地址bean -->
    <bean id="address" class="com.shengjava.pojo.Address">
        <property name="address" value="中国浙江"></property>
    </bean>

    <!-- p-namespace使用(p-namespace中的p相当于property标签,简化代码用的) -->
    <!-- 不使用p-namespace时,使用无参构造器创建bean对象 -->
    <bean id="student01Before" class="com.shengjava.pojo.Student">
        <property name="name" value="张三"></property>
        <property name="address">
            <ref bean="address"></ref>
        </property>
        <property name="nativePlace">
            <ref bean="address"></ref>
        </property>
    </bean>
    <!-- 使用p-namespace时,使用无参构造器创建bean对象 -->
    <bean id="student01After" class="com.shengjava.pojo.Student" p:name="张三" p:address-ref="address" p:nativePlace-ref="address"></bean>

    <!-- c-namespace使用(c-namespace中的c相当于constructor-arg标签,简化代码用的) -->
    <!-- 不使用c-namespace时,使用有参构造器创建bean对象 -->
    <bean id="student02Before" class="com.shengjava.pojo.Student">
        <constructor-arg name="name" value="张三"/>
        <constructor-arg name="address" ref="address"/>
        <constructor-arg name="nativePlace" ref="address"/>
    </bean>
    <!-- 不使用c-namespace时,使用有参构造器创建bean对象 -->
    <bean id="student02After" class="com.shengjava.pojo.Student" c:name="张三" c:address-ref="address" c:nativePlace-ref="address"></bean>
</beans>

注意点:配置文件中,需要引入xml命名空间。

xmlns:p=“http://www.springframework.org/schema/p”,代表引入p-namespace

xmlns:c=“http://www.springframework.org/schema/c”,代表引入c-namespace

编写测试类

public class StudentTest {
    public static void main(String[] args) {
        // 获取context
        ApplicationContext context = new ClassPathXmlApplicationContext("studentBeans.xml");
        // 获取对象
        Student student01Before = context.getBean("student01Before", Student.class);
        Student student01After = context.getBean("student01After", Student.class);
        Student student02Before = context.getBean("student02Before", Student.class);
        Student student02After = context.getBean("student02After", Student.class);
        System.out.println(student01Before);
        System.out.println(student01After);
        System.out.println(student02Before);
        System.out.println(student02After);
    }
}

输出:

Student{name='张三', address=Address{address='中国浙江'}, nativePlace=Address{address='中国浙江'}}
Student{name='张三', address=Address{address='中国浙江'}, nativePlace=Address{address='中国浙江'}}
Student{name='张三', address=Address{address='中国浙江'}, nativePlace=Address{address='中国浙江'}}
Student{name='张三', address=Address{address='中国浙江'}, nativePlace=Address{address='中国浙江'}}

以上就是依赖注入的扩展,p-namespace、c-namespace 主要是减少一点代码量。


相关

我的该分类的其他相关文章,请点击:【Spring + Spring MVC + MyBatis】文章目录

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值