spring之基于p命名c命名空间的注入

本文介绍了Spring框架中p命名空间和c命名空间的使用方法。p命名空间简化了属性的set注入,c命名空间则简化了构造方法注入。通过具体的Java类和Spring配置文件示例,展示了如何利用这两种命名空间简化依赖注入。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >


前言

P命名空间注入:
目的:简化set方法注入
使用p命名空间注入的前提条件包括两个:

  • 在XML头部信息中添加p命名空间的配置信息:xmlns:p=“http://www.springframeworl.org/schema/p”
  • p命名空间注入是基于setter方法的,所以需要对应的属性提供set方法

c命名空间注入:
目的:简化构造方法
使用c命名空间注入的前提条件包括两个:

  • 在XML头部信息中添加p命名空间的配置信息:xmlns:c=“http://www.springframeworl.org/schema/c”
  • c命名空间注入是基于构造方法的,所以需要提供构造方法

一、p命名空间

1.1、编写一个普通的Java类

public class Dog {
    //简单类型
    private String name;
    private int age;
    //非简单类型
    private Date birth;

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

    public void setAge(int age) {
        this.age = age;
    }

    public void setBirth(Date birth) {
        this.birth = birth;
    }

    @Override
    public String toString() {
        return "Dog{" +
                "name='" + name + '\'' +
                ", age=" + age +
                ", birth=" + birth +
                '}';
    }
}

1.2、spring配置文件

spring配置文件:spring-p.xml
第一步:在spring的配置文件头部添加p命名空间,xmlns:p=“http://www.springframework.org/schema/p”
第二步:使用 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">


    <!--
        第一步:在spring的配置文件头部添加p命名空间,xmlns:p="http://www.springframework.org/schema/p"
        第二步:使用 p:属性名
    -->
    <bean id="dogBean" class="com.powernode.spring6.bean.Dog" p:name="小陈" p:age="3" p:birth-ref="birthBean"></bean>
    <!--这里获取的是当前系统时间-->
    <bean id="birthBean" class="java.util.Date"></bean>
</beans>

1.3、测试

测试类:

    @Test
    public void testSpringP(){
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("spring-p.xml");
        Dog dog = applicationContext.getBean("dogBean", Dog.class);
        System.out.println(dog);
    }

1.4、运行结果

在这里插入图片描述

二、c命名空间

2.1、编写一个普通的Java类

public class People {
    private String name;
    private int age;
    private boolean sex;

    //c命名空间注入办法是基于构造方法的
    public People(String name, int age, boolean sex) {
        this.name = name;
        this.age = age;
        this.sex = sex;
    }

    @Override
    public String toString() {
        return "People{" +
                "name='" + name + '\'' +
                ", age=" + age +
                ", sex=" + sex +
                '}';
    }
}

2.2、spring配置文件

spring配置文件:spring-c.xml
第一步:在spring的配置文件头部添加c命名空间,xmlns:c=“http://www.springframework.org/schema/c”
第二步:使用 c:参数名方式
也可以使用c:下标方式

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:c="http://www.springframework.org/schema/c"
       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">
    <!--
        第一步:在spring的配置文件头部添加c命名空间,xmlns:c="http://www.springframework.org/schema/c"
        第二步:使用 c:下标方式  或者 c:参数名方式
    -->
    <bean id="peopleBean" class="com.powernode.spring6.bean.People" c:_0="小花" c:_1="20" c:_2="true"></bean>
</beans>

2.3、测试

测试类:

    @Test
    public void testSpringC(){
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("spring-c.xml");
        People people = applicationContext.getBean("peopleBean", People.class);
        System.out.println(people);
    }

2.4、运行结果

在这里插入图片描述


总结

p命名空间本质上还是set注入,只不过p命名空间注入可以让spring配置变得更简单
c命名空间是简化构造方法注入的

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值