Spring-Context之七:使用p-namesapce和c-namespace简化bean的定义

简介: 在Spring中定义bean的方式多种多样,即使使用xml的方式来配置也能派生出很多不同的方式。 比如如下的bean定义: 1 2 3 4 5 6 7 8 9 10 11 12 这样的bean有三行,通过使用p-namespace以后可以简化为一行。

在Spring中定义bean的方式多种多样,即使使用xml的方式来配置也能派生出很多不同的方式。

比如如下的bean定义:

1
2
3
4
5
6
7
8
9
10
11
12
<beans xmlns="http://www.springframework.org/schema/beans"
       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="person" class="Person">
        <property name="name" value="Tom"/>
        <property name="age" value="20"/>
    </bean>

</beans>

这样的bean有三行,通过使用p-namespace以后可以简化为一行。

1
2
3
4
5
6
7
8
9
<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="person" class="Person" p:name="Tom" p:age="20"/>

</beans>

那么什么是p-namespace那?它的作用就是使用xml中的元素属性取代<property/>节点来定义bean的属性。这个神奇的p是什么东西那?它其实是使用了namespace的xml扩展配置格式。beans的配置格式是定义在一个xsd格式中的(即 http://www.springframework.org/schema/beans/spring-beans.xsd),但p却没有一个xsd格式文件与其对应,但是它可以被spring内核解析处理。

上面只是演示了对属性为普通值的时使用p-namespace的注入,如果属性为另一个bean的引用时该如何处理那?很简单。

这是使用正常方式注入属性。

1
2
3
4
5
6
    <bean id="messageService" class="SimpleMessageService"/>
    <bean id="messageHandler" class="MessageHandler">
        <property name="messageService">
            <ref bean="messageService" />
        </property>
    </bean>

使用p-namespace后是这样的。

1
2
    <bean id="messageService" class="SimpleMessageService"/>
    <bean id="messageHandler" class=“MessageHandler” p:messageService-ref=“messageService”/>

加上-ref后缀即表示是对一个bean的引用。

那既然setter方法注入bean可以使用p-namespace,那么构造器方式注入有没有相应的简写那?答案是肯定的,那就是c-namespace,原理和使用方法与p-namespace大同小异。

使用c-namespace前:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
<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="person" class="Person">
        <constructor-arg name="name">
            <value>Tom</value>
        </constructor-arg>
        <constructor-arg name="age" value="20"/>
    </bean>

</beans>

使用c-namespace后:

1
2
3
4
5
6
7
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="person"  c:name="Tom" c:age="20"/>
</beans>

也可以使用-ref后缀来表示对另一个bean的引用。

1
2
 <bean id="messageService" class="SimpleMessageService"/>
    <bean id="messageHandler" class="MessageHandler" c:messageService-ref="messageService"/>

在前面章节讲解构造器注入时,可以使用构造参数索引来注入依赖,c-namespace也支持这一方式。

1
2
3
4
5
6
7
8
9
10
11
12
<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="person”  c:_0="Tom" c:_1="20"/>

<bean id="messageService" class="SimpleMessageService"/>
<bean id="messageHandler" class="MessageHandler" c:_0-ref="messageService"/>

</beans>

怎么样,是不是很强大啊。但是太过强大也容易伤人伤己。在项目中使用这些技巧之前最好先和项目成员达成一致。

本例中的源码请在我的GitHub上自行下载。

相关文章
|
5天前
|
Java 开发者 Spring
解析Spring中Bean的生命周期
解析Spring中Bean的生命周期
14 2
|
5天前
|
XML Java 数据格式
深度解析 Spring 源码:从 BeanDefinition 源码探索 Bean 的本质
深度解析 Spring 源码:从 BeanDefinition 源码探索 Bean 的本质
14 3
|
1月前
|
安全 Java Spring
Spring框架中的单例Bean是线程安全的吗?
Spring框架中的单例Bean是线程安全的吗?
28 1
|
5天前
|
Java 开发者 Spring
Spring 中 Bean 的生命周期
Spring 中 Bean 的生命周期
9 2
|
1月前
|
Java 容器 Spring
Spring的加载配置文件、容器和获取bean的方式
Spring的加载配置文件、容器和获取bean的方式
29 3
Spring的加载配置文件、容器和获取bean的方式
|
1月前
|
Java Spring 容器
Spring注解开发,bean的作用范围及生命周期、Spring注解开发依赖注入
Spring注解开发,bean的作用范围及生命周期、Spring注解开发依赖注入
34 1
Spring注解开发,bean的作用范围及生命周期、Spring注解开发依赖注入
|
12天前
|
Java Spring
解决 Spring 中 Prototype Bean 注入后被固定的问题
【6月更文挑战第8天】学习 Spring 框架内不原理的意义就是,当遇到问题时,分析出原因,就可以从多个切入点,利用 Spring 的特性,来解决问题。
24 2
|
22天前
|
Java Spring 缓存
Spring Bean循环依赖详解
【6月更文挑战第2天】
26 2
|
5天前
|
XML Java 数据格式
Spring框架第三章(基于注解管理bean)
Spring框架第三章(基于注解管理bean)
|
5天前
|
XML Java 数据格式
Spring框架第二章(基于XML管理bean)
Spring框架第二章(基于XML管理bean)