Spring(04)——p命名空间和c命名空间

本文围绕Spring的p命名空间和c命名空间展开。在注入关联项时,有了这两个命名空间可将其当作属性定义。介绍了p命名空间的使用,包括声明及不同情况的定义方式,还提到使用注意点;c命名空间用法类似,也需声明,不过对编译方式有要求。

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

Spring(04)——p命名空间和c命名空间

 

4 p命名空间和c命名空间

在通过构造方法或set方法给bean注入关联项时通常是通过constructor-arg元素和property元素来定义的。在有了p命名空间和c命名空间时我们可以简单的把它们当做bean的一个属性来进行定义。

4.1 p命名空间

使用p命名空间时需要先声明使用对应的命名空间,即在beans元素上加入xmlns:p="http://www.springframework.org/schema/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">

	<bean id="world" class="com.app.World"/>
	
	<!-- 通过set方法注入的传统的bean定义 -->
	<bean id="hello1" class="com.app.Hello">
		<property name="p1" value="v1"/>
		<property name="p2" value="v2"/>
		<property name="world" ref="world"/>
	</bean>
	
	<!-- 通过set方法注入的使用p命名空间的bean定义 -->
	<bean id="hello2" class="com.app.Hello" p:p1="v1" p:p2="v2" p:world-ref="world"/>

</beans>

在上面示例中,idhello1bean是传统的bean定义,而idhello2bean是基于p命名空间的bean定义。当传统的property元素定义的value是基础数据类型时,我们可以直接把property元素对应的name加上p命名空间的前缀作为bean的一个属性进行定义,对应的值就是原property元素对应的value。如上述示例中name“p1”property使用p命名空间后就变成了“p:p1”;当传统的property元素定义的是对其它bean的关联时,我们可以直接把property元素对应的name加上“-ref”,再加上p命名空间的前缀作为bean的一个属性进行定义,对应的值为原property元素对应的ref值,如上述示例中name“world”property就是定义了对其它bean的关联,使用p命名空间后就变成了“p:world-ref”。这里有一点需要注意的地方就是property对应的是set方法,而不是对应的属性,如name“world”property实际上对应的是setWorld()方法,这个时候不管对应的bean是否真存在名为world的属性;另一点需要注意的地方是使用p命名空间时要注意以“-ref”结尾的property,这会导致Spring以其前部分作为property,因为“-ref”会被Spring作为关联的关键字。

4.2 c命名空间

c命名空间的用法和p命名空间类似,其对应于constructor-arg,即可以将constructor-arg元素替换为bean的一个以c命名空间前缀开始的属性。使用c命名空间之前也需要通过xmlns:c=”http://www.springframework.org/schema/c”进行声明。

<?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: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="world" class="com.app.World"/>
	
	<!-- 传统的使用constructor-arg通过构造方法注入的bean定义 -->
	<bean id="hello1" class="com.app.Hello">
		<constructor-arg index="0" value="arg1"/>
		<constructor-arg index="1" value="2"/><!-- arg2 -->
		<constructor-arg index="2" ref="world"/><!-- arg3 -->
	</bean>
	<!-- 使用c命名空间通过构造方法注入的bean定义 -->
	<bean id="hello2" class="com.app.Hello" c:arg1="c_arg1" c:arg2="2" c:arg3-ref="world"/>
</beans>

如上所示,c命名空间的用法和p命名空间的用法类似。对于通过构造方法注入原始类型的对象可以把对应的构造参数名称加上c命名空间的前缀作为bean的一个属性进行定义,对应的值即是构造参数的值;如果通过构造参数注入的是其它bean的一个引用,则可将该构造参数名称加上“-ref”,再加上c命名空间的前缀作为该bean的一个属性进行定义,对应的值为所关联beanidname,如上述示例中的“c:arg3-ref”

需要注意的是直接把构造参数名称加上c命名空间的前缀作为bean的一个属性定义来替代对应的constructor-arg只对以debug方式编译的class有效,因为对于非debug方式编译的class文件Spring将无法获取到对应构造方法的参数名。对于这种情况我们可以直接使用构造方法参数的索引加上下划线“_”前缀来代替对应的参数名,索引是从0开始的,如上面的示例以索引来代替时将是如下这个样子。

<?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: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="world" class="com.app.World"/>
	
	<!-- 传统的使用constructor-arg通过构造方法注入的bean定义 -->
	<bean id="hello1" class="com.app.Hello">
		<constructor-arg index="0" value="arg1"/>
		<constructor-arg index="1" value="2"/><!-- arg2 -->
		<constructor-arg index="2" ref="world"/><!-- arg3 -->
	</bean>
	<!-- 使用c命名空间并且是使用构造参数的索引作为属性来通过构造方法注入的bean定义 -->
	<bean id="hello2" class="com.app.Hello" c:_0="c_arg1" c:_1="2" c:_2-ref="world"/>
</beans>

(注:本文是基于Spring4.1.0所写)

### Spring 框架中的命名空间及其使用 #### 什么是命名空间? 在 Spring 中,XML 文件通过自定义命名空间扩展其功能。这些命名空间允许开发者更简洁地配置特定的功能模块而无需手动编写大量的 `<bean>` 定义[^1]。 #### 如何引入命名空间? 要在 XML 配置文件中使用命名空间,需先声明该命名空间以及对应的 URI schema 地址。例如: ```xml <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> </beans> ``` 上述代码片段展示了如何引入 `context` 命名空间,并指定其 XSD 文件的位置以便验证配置文件的合法性[^2]。 #### 名称空间的具体应用实例 ##### 1. 自动扫描组件 (`context`) Spring 提供了一个非常有用的命名空间——`context`,它支持自动发现并注册类路径下的组件为 Spring Beans。这通常用于基于注解的应用程序开发模式。 ```xml <context:component-scan base-package="com.example"/> ``` 此段代码会告诉 Spring 在包 `com.example` 及其子包中查找带有注解(如 `@Component`, `@Service`, `@Repository`, 或者 `@Controller`)标记的类,并将其作为 Bean 加载到容器中。 ##### 2. 缓存管理 (`cache`) 如果项目需要实现缓存机制,则可以利用 `cache` 命名空间来简化配置过程。下面是一个简单的例子展示如何启用缓存支持: ```xml <cache:annotation-driven/> <bean id="cacheManager" class="org.springframework.cache.ehcache.EhCacheCacheManager"> <property name="cacheManager" ref="ehCacheManager"/> </bean> <bean id="ehCacheManager" class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean"> <property name="configLocation" value="classpath:ehcache.xml"/> </bean> ``` 这里启用了基于注解驱动的缓存策略,并设置了 EhCache 的具体参数。 ##### 3. 数据源事务控制 (`tx`) 对于数据库操作而言,事务处理至关重要。借助于 `tx` 命名空间能够轻松设置全局或者局部范围内的事务行为。 ```xml <tx:advice id="txAdvice" transaction-manager="transactionManager"> <tx:attributes> <tx:method name="*" propagation="REQUIRED"/> </tx:attributes> </tx:advice> <aop:config> <aop:pointcut expression="execution(* com..service.*(..))" id="businessService"/> <aop:advisor advice-ref="txAdvice" pointcut-ref="businessService"/> </aop:config> ``` 以上示例说明了如何围绕服务层的方法实施环绕通知形式的事务拦截逻辑。 #### 总结 通过合理运用不同的命名空间,可以使 Spring 应用更加灵活高效。无论是自动化装配还是高级特性集成都可以显著减少冗余编码量的同时提高可维护性清晰度。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值