Spring Schemas and Namespaces
来源:互联网 发布:赚钱最多的职业知乎 编辑:程序博客网 时间:2024/05/16 06:52
本章研究Spring命名空间,你将学到新的并且更简单的办法创建Spring bean XML file,并且我们将讨论所有Spring框架提供的schema。此外,你还将看到如何创建自定义Spring schema和他的处理器。 新的schema最主要的吸引力是他们使用命名空间样式的配置文件,这样他们将更加简单明了并且有更丰富的表达力。他们允许你非常准确的了解每一个元素和属性允许的值。因为你可以创建自己的scheam和命名空间,所以你可以使配置文件写起来非常简单。最普通的事情是你有一个共享组件具有他的自定义Spring配置schema和parser,比如说在你们公司的Maven2仓库中。其他的开发者可以使用这个共享组件而不需要花费很多时间去阅读说明文档。你也可以明显的减少丢失配置的情况。 在使用Spring 1.x时,我们使用非常复杂的XML文件去描述bean的配置信息和依赖关系。我们可以在Spring 2.5中继续这么做,不过现在我们可以获得一些标准XSD schema和scheam handler带来的好处。 考虑就样式的配置文件(如下):他对于老练的Spring 1.x开发这来说是非常易读的,但是仍然非常冗长。 Description: Very Verbose Spring 1.x-Style Context File 一个Spring 2.5样式的配置文件并不一定非常短小精悍,但是他更加清楚并且更容易理解。下面是新样式的配置文件, Descrption: The Configuration File from Listing 7-1 in Spring 2.5 正如你看到的一样,新的配置文件并不是必须短小精悍,即使XML scheam定义的内容非常多。新代码主要的优势是非常清楚并且容易理解:greeterService不在像target bean一样隐藏在TransactionProxyFactoryBean中。除此之外使用指定的XSD schema还可以使很多IDE获得代码提示功能,你可以使用code completion获得可能的值或者其他功能。 下面让我们看一下Spring框架支持的不同schema。下边总结了可用的schema和他们的用途, Description: Schemas Included in the Spring 2.5 Distribution 除了上面的这些,Spring文件夹还提供了很多其他的,比如Spring Security,和其他第三方提供的,比如Oracle Coherence就有他们的自定义namespace。下面我们将讨论每一个上表列出的schema 我们从这些schema中最重要的beans schema开始。他与Spring框架中最早出现的标签相同。在某个XML文件中指定这个schema为默认的schema只需要在XML文件中简单的生命他,如, Description: beans Schema Example 上面的代码非常简单,并没有引用beans schema中的元素。beans scheam包含四个元素:bean,alias,import和description。bean元素匹配Spring DTD中定义bean的元素。在本书第三章和第四章中对此有详细的描述。 Spring 2.5引入了context schema。这是排名第二的基础scheam,虽然在大多数应用程序中你都不会使用他。他修改Spring的bean创建进程的核心行为。下面清单显示了如何在Spring context files中配置property placeholders。 Description: Beans for the Property Placeholder Configuration 上面的代码显示了如何使用context schema配置property placeholder。他可能使用一个位置(location)或使用逗号分隔的多个位置的列表配置property placeholder。如果无法找到你要使用的property,property placeholder将以Java System properties为标准进行检查。 It contains definitions that are shorthand for the old factory beans. Description: Usage of the util Schema 下面让我们看一下util schema中的若干元素,从最简单的util:constant开始, Description: Constant Declaration 上面的代码声明了一个DI为X的bean,并设置它的值为Integer.MAX_VALUE。 The next element,util:list,声明一个实现了java.util.List的bean,更准确的说是java.util.ListSpring Schemas and Namespaces
Why the New Configuration?
01
<?
xml
version
=
"1.0"
encoding
=
"UTF-8"
?>
02
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN"
03
"http://www.springframework.org/dtd/spring-beans.dtd">
04
<
beans
>
05
<
bean
id
=
"transactionManager"
06
class
=
"com.apress.prospring2.namespaces.NoopPlatformTransactionManager"
/>
07
<
bean
id
=
"greetingsList"
08
class
=
"org.springframework.beans.factory.config.ListFactoryBean"
>
09
<
property
name
=
"sourceList"
>
10
<
list
>
11
<
value
>Hello, World</
value
>
12
<
value
>What do you want to do tomorrow?</
value
>
13
<
value
>Nazdar, prdi</
value
>
14
</
list
>
15
</
property
>
16
</
bean
>
17
<
bean
id
=
"greeterService"
18
class="org.springframework.transaction.interceptor.
19
TransactionProxyFactoryBean">
20
<
property
name
=
"transactionManager"
ref
=
"transactionManager"
/>
21
<
property
name
=
"target"
>
22
<
bean
class
=
"com.apress.prospring2.namespaces.ComplexGreeterService"
>
23
<
property
name
=
"greetings"
ref
=
"greetingsList"
/>
24
</
bean
>
25
</
property
>
26
<
property
name
=
"transactionAttributes"
>
27
<
props
>
28
<
prop
key
=
"greet*"
>PROPAGATION_REQUIRED</
prop
>
29
</
props
>
30
</
property
>
31
</
bean
>
32
</
beans
>
01
<?
xml
version
=
"1.0"
encoding
=
"UTF-8"
?>
02
<
beans
xmlns
=
"http://www.springframework.org/schema/beans"
03
xmlns:xsi
=
"http://www.w3.org/2001/XMLschema-instance"
xmlns:util
=
"http://www.springframework.org/schema/util"
04
xmlns:tx
=
"http://www.springframework.org/schema/tx"
xmlns:aop
=
"http://www.springframework.org/schema/aop"
05
xsi:schemaLocation="http://www.springframework.org/schema/beans
06
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
07
http://www.springframework.org/schema/util
08
http://www.springframework.org/schema/util/spring-util-2.5.xsd
09
http://www.springframework.org/schema/tx
10
http://www.springframework.org/schema/tx/spring-tx-2.5.xsd
11
http://www.springframework.org/schema/aop
12
http://www.springframework.org/schema/aop/spring-aop-2.5.xsd">
13
<
bean
id
=
"transactionManager"
14
class
=
"com.apress.prospring2.namespaces.NoopPlatformTransactionManager"
/>
15
<
util:list
id
=
"greetingsList"
>
16
<
value
>Hello, world</
value
>
17
<
value
>What do you want to do tomorrow?</
value
>
18
<
value
>Nazdar, prdi</
value
>
19
</
util:list
>
20
<
bean
id
=
"greeterService"
class
=
"com.apress.prospring2.namespaces.ComplexGreeterService"
>
21
<
property
name
=
"greetings"
ref
=
"greetingsList"
/>
22
</
bean
>
23
<
tx:advice
id
=
"greeterServiceAdvice"
transaction-manager
=
"transactionManager"
>
24
<
tx:attributes
>
25
<
tx:method
name
=
"greet*"
propagation
=
"REQUIRED"
/>
26
</
tx:attributes
>
27
</
tx:advice
>
28
<
aop:config
>
29
<
aop:pointcut
id
=
"greeterServiceOperation"
30
expression
=
"execution(* com.apress.*.*.GreeterService.*(..))"
/>
31
<
aop:advisor
advice-ref
=
"greeterServiceAdvice"
32
pointcut-ref
=
"greeterServiceOperation"
/>
33
</
aop:config
>
34
</
beans
>
Schemas Included in Spring 2.5
The beans Schema
1
<?
xml
version
=
"1.0"
encoding
=
"UTF-8"
?>
2
<
beans
xmlns
=
"http://www.springframework.org/schema/beans"
3
xsi:schemaLocation="http://www.springframework.org/schema/beans
4
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
5
<!-- bean declarations here -->
6
</
beans
>
The context Schema
01
<?
xml
version
=
"1.0"
encoding
=
"UTF-8"
?>
02
<
beans
xmlns
=
"http://www.springframework.org/schema/beans"
03
xmlns:xsi
=
"http://www.w3.org/2001/XMLSchema-instance"
04
xmlns:context
=
"http://www.springframework.org/schema/context"
05
xsi:schemaLocation="http://www.springframework.org/schema/beans
06
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
07
http://www.springframework.org/schema/context
08
http://www.springframework.org/schema/context/spring-context-2.5.xsd">
09
<
bean
id
=
"test"
class
=
"com.apress.prospring2.ch07.context.TestBean"
>
10
<
property
name
=
"name"
value
=
"${name}"
/>
11
</
bean
>
12
<
context:property-placeholder
location
=
13
"classpath:/com/apress/prospring2/ch07/context/test.properties"
/>
14
</
beans
>
The util Schema
1
<
beans
xmlns
=
"http://www.springframework.org/schema/beans"
2
xmlns:xsi
=
"http://www.w3.org/2001/XMLschema-instance"
3
xmlns:util
=
"http://www.springframework.org/schema/util"
4
xsi:schemaLocation="http://www.springframework.org/schema/beans
5
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
6
http://www.springframework.org/schema/util
7
http://www.springframework.org/schema/util/spring-util-2.5.xsd">
8
<!-- bean declarations here -->
9
</
beans
>
1
<
util:constant
id
=
"X"
static-field
=
"java.lang.Integer.MAX_VALUE"
/>
- Spring Schemas and Namespaces
- Spring Schemas and Namespaces(转)
- 8.12 Namespaces and assemblies
- XPath and XML Namespaces
- User namespaces and setgroups()
- Python Scopes and Namespaces
- Namespaces and name conflicts
- Naming Files, Paths, and Namespaces
- rails Controller Namespaces and Routing
- Identifying and Troubleshooting Neutron Namespaces
- Namespaces
- Namespaces
- 自定义spring.schemas和spring.handlsers
- Namespaces, VLANs, Open vSwitch, and GRE Tunnels
- chapter11 separate Compilition and Namespaces(part1)
- Difference between Scopes and Namespaces in python
- Understanding and Using Assemblies and Namespaces in .NET
- less-Namespaces and Accessors(名称空间和访问器)
- android wifi (WIFI状态的获取和更改)
- apk签名 keytool
- 跟我一起写 Makefile (五)——使用变量
- 长蛾一号程序用什么软件开发的
- Windows消息机制
- Spring Schemas and Namespaces
- 跟我一起写 Makefile (六)——使用条件判断
- HDU Push Box
- 我怎么就什么都不会啊
- iPhone上页面触屏事件的简单实现
- Java.util.date 与 java.sql.date区别和转换
- 跟我一起写 Makefile (七)——使用函数
- zoj 2433
- 跟我一起写 Makefile (八)——make的运行