Spring5入门-04-p-命名空间与c-命名空间

一、前言

目的:了解C命名空间和P命名空间的用法



二、准备工作

2.1 依赖

注意:这里用到的应该是spring-context,但是spring-webmvc由于继承的关系会有一张依赖网:

image-20200922215433508

算是省心省力吧。

<!--Spring-->
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-webmvc</artifactId>
    <version>${org.springframework.version}</version>
</dependency>

<!--JUnit-->
<dependency>
    <groupId>junit</groupId>
    <artifactId>junit</artifactId>
    <version>4.12</version>
    <scope>test</scope>
</dependency>

2.2 创建实体类

路径

image-20200924150655489

代码

Address.java

package com.huangdekai.dao;

/**
 * @Autord: HuangDekai
 * @Date: 2020/9/24 14:21
 * @Version: 1.0
 * @since: jdk11
 */
public class Address {
    int id;
    String postalCode;
    String address;

    public Address() {
    }

    public Address(int id, String postalCode, String address) {
        this.id = id;
        this.postalCode = postalCode;
        this.address = address;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getPostalCode() {
        return postalCode;
    }

    public void setPostalCode(String postalCode) {
        this.postalCode = postalCode;
    }

    public String getAddress() {
        return address;
    }

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

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

User.java

package com.huangdekai.dao;

/**
 * @Autord: HuangDekai
 * @Date: 2020/9/23 23:45
 * @Version: 1.0
 * @since: jdk11
 */
public class User {
    private int id;
    private String name;
    private String password;

    public User() {
        System.out.println("User无参构造");
    }

    public User(int id, String name, String password) {
        this.id = id;
        this.name = name;
        this.password = password;
        System.out.println("User有参构造");
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

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

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    @Override
    public String toString() {
        return "User{" +
                "id=" + id +
                ", name='" + name + '\'' +
                ", password='" + password + '\'' +
                '}';
    }
}



三、p-namespace

3.1 xml文件

路径

image-20200924150823042

代码

<?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="address1" class="com.huangdekai.dao.Address" p:id="1" p:postalCode="123456" p:address="上海市翻斗大街翻斗花园二号楼1001室"/>
    <bean id="user1" class="com.huangdekai.dao.User" p:id="1" p:name="HuTutu" p:password="123456" p:address-ref="address1"/>

</beans>

使用p命名空间需要加上xmlns:p="http://www.springframework.org/schema/p"

image-20200924151757782


3.2 测试样例

路径

image-20200924153433747

代码

import com.huangdekai.dao.User;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

/**
 * @Autord: HuangDekai
 * @Date: 2020/9/24 0:01
 * @Version: 1.0
 * @since: jdk11
 */
public class UserTest {
    @Test
    public void userTest(){
        ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
        User user1 = context.getBean("user1", User.class);

        System.out.println(user1);

    }
}

3.3 结果

image-20200924153550090

可以看到,p命名空间就是利用setter方法注入依赖的。

从使用上来说,p-namespace就是<property>标签的简化用法。

四、c-namespace

c-命名空间做的事情与p-namespace对应,就是像基于构造器注入:

修改beans.xml

  • 添加c-namespace需要的xmlns:c="http://www.springframework.org/schema/c
  • 注释掉用p-namespace注入的bean
  • 添加用c-namespace注入的bean
<?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 http://www.springframework.org/schema/beans/spring-beans.xsd">

    <bean id="address1" class="com.huangdekai.dao.Address" p:id="1" p:postalCode="123456" p:address="上海市翻斗大街翻斗花园二号楼1001室"/>

<!--    <bean id="user1" class="com.huangdekai.dao.User" p:id="1" p:name="HuTutu" p:password="123456" p:address-ref="address1"/>-->

    <bean id="user1" class="com.huangdekai.dao.User" c:id="1" c:name="Duzhuan" c:password="123456" c:address-ref="address1"/>

</beans>

运行测试样例:

image-20200924154805235

如果使用IDEA写xml的话,可能还能看到这样的:

image-20200924154904344

显然,c-namespace还允许使用index注入,即0,1,2...代表的是参数的顺序。

由于可读性不好,不推荐使用,不再赘述。

posted @ 2020-09-24 15:54  杜撰丶  阅读(483)  评论(0编辑  收藏  举报