长生的梦呓


  • 归档
  • 算法
  • 基础
  • 关于

  • 分类
  • 日志
  • Servlet
  • Archive
  • 数据结构
  • IO 流

  • 标签
  • 友链
  • MyBatis
  • About
  • Spring 5
  • Java SE
  • Java EE
  • Algorithms
  • 新特性
  • 位运算技巧

  • 搜索
内网穿透 项目实战 数据库 MySQL 安卓踩坑 开发工具 设计模式 Enum 枚举 Linux MyBatis-plus JSON IDEA Transactions AOP IO 流 DP IoC 与 DI 位运算技巧 工具类 学习技巧 Git JDK 排序 Spring Boot Spring MVC Spring Framework MyBatis Log4J Regex Jsoup JDBC 数据结构 递推 递归 算法 Servlet与JSP 小难 中等 简单

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

发表于 2020-04-05 | 1 | 阅读次数 300

一、依赖注入扩展

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

使用: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 主要是减少一点代码量。

  • 本文作者: 长生的梦呓
  • 本文链接: https://shengjava.com/archives/spring35依赖注入扩展
  • 版权声明: 本博客所有文章除特别声明外,均采用CC BY-NC-SA 3.0 许可协议。转载请注明出处!
# 内网穿透 # 项目实战 # 数据库 # MySQL # 安卓踩坑 # 开发工具 # 设计模式 # Enum # 枚举 # Linux # MyBatis-plus # JSON # IDEA # Transactions # AOP # IO 流 # DP # IoC 与 DI # 位运算技巧 # 工具类 # 学习技巧 # Git # JDK # 排序 # Spring Boot # Spring MVC # Spring Framework # MyBatis # Log4J # Regex # Jsoup # JDBC # 数据结构 # 递推 # 递归 # 算法 # Servlet与JSP # 小难 # 中等 # 简单
【Spring】(3.4)依赖注入(基于setter)
【Spring】(3.6)自动装配
  • 文章目录
  • 站点概览
长生的梦呓

长生的梦呓

110 日志
39 分类
40 标签
RSS
E-mail CSDN
Creative Commons
Links
  • CSDN 地址
  • waltz26
  • Ryan Wang's Blog
  • JohnNiang's Blog
  • 廖雪峰
  • 菜鸟教程
© 2021 长生的梦呓
浙ICP备20005262号-1