第五节 bean的p命名空间注入

亮子 2021-06-13 08:48:35 17169 0 0 0

1、p命名空间

Spring提供了p、c命名空间注入,实际上p就是property的意思、c就是constructor-arg的意思。算是对构造器注入和set注入的简化吧!

使用c、p命名空间注入需要引入两个标签在beans的配置中。

1)、引入依赖

<!-- p命名空间依赖 -->
xmlns:p="http://www.springframework.org/schema/p"

<!-- c命名空间依赖 -->
xmlns:c="http://www.springframework.org/schema/c"

2)、p命名空间使用

  • 在beans的xml配置项中引入p命名空间依赖

  • 在使用之前保证实体类中必须要有Set方法、空构造器。

<bean id="user" class="com.pojo.User" p:age="18" p:name="小明"/>

3)、c命名空间使用

在beans的xml配置项中引入c命名空间依赖

实体类中必须要有有参构造方法

<bean id="user2" class="com.pojo.User" c:age="20" c:name="大明"/>

2、p空间使用示例

1)、重新定义TbScore

这里需要注意的是,该类必须实现set方法。

package com.shenmazong.pojo;

public class TbScore {
    private Integer math;
    private Integer history;

    public TbScore() {
    }

    public TbScore(Integer math, Integer history) {
        this.math = math;
        this.history = history;
    }

    public Integer getMath() {
        return math;
    }

    public void setMath(Integer math) {
        this.math = math;
    }

    public Integer getHistory() {
        return history;
    }

    public void setHistory(Integer history) {
        this.history = history;
    }

    @Override
    public String toString() {
        return "TbScore{" +
                "math=" + math +
                ", history=" + history +
                '}';
    }
}

2)、配置文件

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:p="http://www.springframework.org/schema/p"
       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="tbScore" class="com.shenmazong.pojo.TbScore" p:math="66" p:history="99"></bean>
    <bean id="tbStudent" class="com.shenmazong.pojo.TbStudent">
        <constructor-arg name="name" value="武松"></constructor-arg>
        <property name="studentScore" ref="tbScore"></property>
    </bean>
</beans>

3)、测试代码

package com.shenmazong;

import com.shenmazong.pojo.TbScore;
import com.shenmazong.pojo.TbStudent;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class DemoTest {
    public static void main(String[] args) {
        ApplicationContext app = new ClassPathXmlApplicationContext("applicationContext.xml");
        TbStudent tbStudent = app.getBean(TbStudent.class);
        System.out.println(tbStudent);
    }
}

3、c空间使用示例

1)、修改对象类

package com.shenmazong.pojo;

public class TbStudent {

    private String name;
    private TbScore studentScore;

    public TbStudent(String name) {
        this.name = name;
    }

    public TbStudent(String name, TbScore studentScore) {
        this.name = name;
        this.studentScore = studentScore;
    }

    public TbScore getStudentScore() {
        return studentScore;
    }

    public void setStudentScore(TbScore studentScore) {
        this.studentScore = studentScore;
    }

    @Override
    public String toString() {
        return "TbStudent{" +
                "name='" + name + '\'' +
                ", studentScore=" + studentScore +
                '}';
    }
}

2)、修改配置文件

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:p="http://www.springframework.org/schema/p"
       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">

    <bean id="tbScore" class="com.shenmazong.pojo.TbScore" p:math="66" p:history="99"></bean>
    <bean id="tbStudent" class="com.shenmazong.pojo.TbStudent" c:name="武松" c:studentScore-ref="tbScore">
    </bean>
</beans>

3)、测试代码

package com.shenmazong;

import com.shenmazong.pojo.TbScore;
import com.shenmazong.pojo.TbStudent;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class DemoTest {
    public static void main(String[] args) {
        ApplicationContext app = new ClassPathXmlApplicationContext("applicationContext.xml");
        TbStudent tbStudent = app.getBean(TbStudent.class);
        System.out.println(tbStudent);
    }
}