使用Spring 的命名空间p 装配属性

本文介绍如何使用Spring的P命名空间简化Bean属性配置。通过引入特定的schemaURI,可以直接在<bean>元素中使用p:前缀进行属性赋值,使配置更为简洁。文章对比了传统的<property>配置方式,并展示了P命名空间的具体应用实例。

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

使用<property> 元素为Bean 的属性装配值和引用并不太复杂。尽管如此,Spring 的命名空间p 提供了另一种Bean 属性的装配方式,该方式不需要配置如此多的尖括号。

命名空间p 的schema URI 为http://www.springframework.org/schema/p。如果你想使用命名空间p,只需要在Spring 的XML 配置中增加如下一段声明:

  1. <?xml version="1.0" encoding="UTF-8"?> 
  2. <beans xmlns="http://www.springframework.org/schema/beans" 
  3. xmlns:p="http://www.springframework.org/schema/p" 
  4. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
  5. xsi:schemaLocation="http://www.springframework.org/schema/beans  
  6. http://www.springframework.org/schema/beans/spring-beans-3.0.xsd"> 

通过此声明,我们现在可以使用p: 作为<bean> 元素所有属性的前缀来装配Bean 的属性。为了示范,我们重新声明了kenny Bean 的配置:

<bean id="sqlSessionFactory" 
class="org.mybatis.spring.SqlSessionFactoryBean"
   p:dataSource-ref="dataSource" 
   p:configLocation="classpath:conf/mybatis-config.xml"
   p:mapperLocations="classpath:mapper/*.xml" />

p:configLocation 属性的值被设置为“”中的值。同样,p:dataSource-ref 属性的值被设置为“dataSource”,将使用一个ID 为dataSource的Bean 引用来装配dataSource-ref属性。-ref 后缀作为一个标识来告知Spring 应该装配一个引用而不是字面值。

选择<property> 还是命名空间p 取决于你,它们是等价的。命名空间p 的最主要优点是更简洁。在固定宽度的纸张上编写样例时,选择命名空间相对更合适。因此,在本书中你可能看到我不时的使用命名空间p,特别是水平页面空间比较紧凑时。

若不使用P命名空间的定义方式,使用<property>,对应的定义应该是:

<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">  
        <property name="dataSource" ref="dataSource" /> 
        <!-- 文件映射器,指定类文件 -->
        <property name="configLocation" value="classpath:conf/mybatis-config.xml"/>  
        <!-- 自动扫描mapping.xml文件 -->  
        <property name="mapperLocations" value="classpath:mapper/*.xml"></property>  
    </bean> 
### 使用 Setter 方法、构造函数和 P 命名空间实现 Teacher 和 Student 类的对象装配 #### 1. 使用 Setter 方法实现对象装配 Setter 方法是一种常见的用于设置对象属性的方式。通过 `set` 方法,可以在运行时动态地将一个类的实例赋值给另一个类的某个字段。 以下是基于引用[^3]中的示例代码,展示如何使用 `setStudent` 方法完成 `Teacher` 和 `Student` 的对象装配: ```java class Student { private String name; public Student(String name) { this.name = name; } public String getName() { return name; } } class Teacher { private Student student; public void setStudent(Student student) { this.student = student; } public void say() { System.out.println(this.student.getName() + ",叫家长来一下。"); } } // 测试代码 public class Main { public static void main(String[] args) { Student s = new Student("张三"); Teacher t = new Teacher(); t.setStudent(s); // 装配 Student 到 Teacher 中 t.say(); // 输出:张三,叫家长来一下。 } } ``` 上述代码展示了如何利用 `setStudent` 方法将 `Student` 对象注入到 `Teacher` 对象中。 --- #### 2. 使用构造函数实现对象装配 除了使用 Setter 方法外,还可以通过构造函数直接传递依赖对象。这种方式在创建对象的同时完成了依赖注入。 下面是修改后的代码示例,其中 `Teacher` 类新增了一个带参数的构造函数,可以直接传入 `Student` 对象: ```java class Student { private String name; public Student(String name) { this.name = name; } public String getName() { return name; } } class Teacher { private Student student; // 构造函数注入方式 public Teacher(Student student) { this.student = student; } public void say() { System.out.println(this.student.getName() + ",叫家长来一下。"); } } // 测试代码 public class Main { public static void main(String[] args) { Student s = new Student("李四"); Teacher t = new Teacher(s); // 在构造函数中装配 Student 到 Teacher 中 t.say(); // 输出:李四,叫家长来一下。 } } ``` 此部分实现了通过构造函数完成对象装配的功能[^4]。 --- #### 3. 使用 P 命名空间实现对象装配 P 命名空间通常指的是 Spring 框架中的 Property 注解或 XML 配置支持的一种简化写法。虽然题目未提及框架环境,但在实际开发中,Spring 是一种常用的依赖注入工具。 假设我们使用 Spring 来管理 `Teacher` 和 `Student` 的对象关系,则可以通过配置文件或注解实现自动化装配。下面是一个简单的 Spring Bean 定义示例: ##### XML 配置方式 (P 命名空间) ```xml <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"> <!-- 定义 Student Bean --> <bean id="student" class="com.example.Student" p:name="王五"/> <!-- 定义 Teacher Bean 并通过 P 命名空间注入 Student --> <bean id="teacher" class="com.example.Teacher" p:student-ref="student"/> </beans> ``` 在此配置下,Spring 将自动完成 `Teacher` 和 `Student` 的对象装配[^5]。 ##### Java 注解方式 如果更倾向于注解驱动的方式,也可以如下定义: ```java import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; @Component class Student { private String name; @Autowired public Student(String name) { this.name = name; } public String getName() { return name; } } @Component class Teacher { private final Student student; @Autowired public Teacher(Student student) { this.student = student; } public void say() { System.out.println(this.student.getName() + ",叫家长来一下。"); } } ``` 以上代码片段说明了如何借助 Spring 的依赖注入机制完成对象装配--- ### 总结 - **Setter 方法**适合在对象已经存在的情况下动态注入依赖。 - **构造函数**则更适合在对象初始化阶段就绑定好依赖关系。 - **P 命名空间**属于 Spring 框架的一部分,提供了声明式的依赖注入能力。 ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值