在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上自行下载。

Spring-Context之七:使用p-namesapce和c-namespace简化bean的定义的更多相关文章

  1. [转载]vs2012中使用Spring.NET报错:Spring.Context.Support.ContextRegistry 的类型初始值设定项引发异常

    学习使用Spring.NET中的时候,写了一个Demo,在运行时报了一个错误:Spring.Context.Support.ContextRegistry 的类型初始值设定项引发异常. 重新整理思绪, ...

  2. Spring context:component-scan中使用context:include-filter和context:exclude-filter

    Spring context:component-scan中使用context:include-filter和context:exclude-filter XML: <?xml version= ...

  3. Spring context:component-scan代替context:annotation-config

    Spring context:component-scan代替context:annotation-config XML: <?xml version="1.0" encod ...

  4. Spring <context:annotation-config> 与<context-component-scan> 的作用

    <context:annotation-config> 与<context-component-scan> 的作用 <context:annotation-config& ...

  5. Spring <context:component-scan>标签属性 use-default-filters 以及子标签 include-filter使用说明

    Spring <context:component-scan>标签作用有很多,最基本就是 开启包扫描,可以使用@Component.@Service.@Component等注解: 今天要作 ...

  6. 使用web.xml方式加载Spring时,获取Spring context的两种方式

    使用web.xml方式加载Spring时,获取Spring context的两种方式: 1.servlet方式加载时: [web.xml] <servlet> <servlet-na ...

  7. 【报错】spring整合activeMQ,pom.xml文件缺架包,启动报错:Caused by: java.lang.ClassNotFoundException: org.apache.xbean.spring.context.v2.XBeanNamespaceHandler

    spring版本:4.3.13 ActiveMq版本:5.15 ======================================================== spring整合act ...

  8. Spring Context 你真的懂了吗

    今天介绍一下大家常见的一个单词 context 应该怎么去理解,正确的理解它有助于我们学习 spring 以及计算机系统中的其他知识. 1. context 是什么 我们经常在编程中见到 contex ...

  9. Spring < context:annotation-config> 、< context:component-scan>、< mvc:annotation-driven />注解配置

    Spring 中在使用注解(Annotation)会涉及到< context:annotation-config> 和 < context:component-scan>配置, ...

随机推荐

  1. IIS7.5 由于 Web 服务器上的“ISAPI 和 CGI 限制”列表设置,无法提供您请求的页面

    IIS7.5中将一网站应用程序池托管管道模式改为经典后,网站页面打不开,错误信息: 引用内容 HTTP 错误 404.2 - Not Found由于 Web 服务器上的“ISAPI 和 CGI 限制” ...

  2. 使用.NET读取exchange邮件

    公司有个第3方的系统,不能操作源码修改错误捕获,但是错误会发一个邮件出来,里面包含了主要的信息.于是想从邮件下手,提取需要的数据 开始考虑使用的是exchange service,主要参考http:/ ...

  3. HTML页面跳转的5种方法

    下面列了五个例子来详细说明,这几个例子的主要功能是:在5秒后,自动跳转到同目录下的hello.html(根据自己需要自行修改)文件.1) html的实现 <head> <!-- 以下 ...

  4. python 实现彻底删除文件夹和文件夹下的文件

    python 中有很多内置库可以帮忙用来删除文件夹和文件,当面对要删除多个非空文件夹,并且目录层次大于3层以上时,仅使用一种内置方法是无法达到彻底删除文件夹和文件的效果的,比较low的方式是多次调用直 ...

  5. kali linux 、 windows、ubuntu三系统的引导问题

    '小飞机'是一个学生,所以接触的东西,虽广泛,但并不精通,在此利用随笔,记录自己的一些学习过程,以及自己的想法,既可以有时间自己复习,也可以顺便帮助别人. 近期由于同学的引诱以及男生天生对于破解的好奇 ...

  6. Python小练习二

    # 以正确的宽度在居中的"盒子"内打印一个句子 # 注意,整数除法运算符(//)只能用在Python 2.2及后续版本,在之前的版本中,只使用普通除法(/) sentence = ...

  7. py2exe 打包scipy时遇到的问题

    最近写了个小程序,用PyQt5做的界面,写完之后用py2exe打包成独立的exe文件,运行正常. 后来由于需要,调用SciPy.io.loadmat,改写setup.py,打包之后运行错误,提示: T ...

  8. ES5 getter setter

    最近在学习vuejs,了解到内部实现使用到了es5的Getters和Setters.之前看高程的时候,没有重视这块,今天查看一下文档,了解了他们的作用,再次记录一下,可供以后查看和共享. 定义Gett ...

  9. <转>技术团队新官上任之基层篇

    发表于2013-09-04 17:17| 10455次阅读| 来源<程序员>| 35 条评论| 作者高博 <程序员>杂志2013年9月刊技术团队管理EMC高博CTO 摘要:从技 ...

  10. Cobbler学习之二--Cobbler的Web管理和维护

    Cobbler的Web管理模块和命令行模块是可以分开工作的,没有依赖关系. 1 WebUI的功能 查看所有的对象和配置文件 添加或者删除system,distro, profile 执行“cobble ...