This appendix details the use of XML Schema-based configuration in Spring.
The 'classic'
<object/>
-based schema is good, but its
generic-nature comes with a price in terms of configuration overhead.
Creating a custom XML Schema-based configuration makes Spring XML
configuration files substantially clearer to read. In addition, it allows
you to express the intent of an object definition.
The key thing to remember is that creating custom schema tags work best for infrastructure or integration objects: for example, AOP, collections, transactions, integration with 3rd-party frameworks, etc., while the existing object tags are best suited to application-specific objects, such as DAOs, service layer objects, etc.
Please note the fact that the XML configuration mechanism is totally customisable and extensible. This means you can write your own domain-specific configuration tags that would better represent your application's domain; the process involved in doing so is covered in the appendix entitled Appendix B, Extensible XML authoring.
As a reminder, you reference the standard objects schema as shown below
<?xml version="1.0" encoding="UTF-8"?>
<objects xmlns="http://www.springframework.net"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.net http://www.springframework.net/schema/objects/spring-objects-1.1.xsd">
<!-- <object/>
definitions here -->
</objects>
Note | |
---|---|
The |
The above Spring XML configuration fragment is boilerplate that
you can copy and paste (!) and then plug
<object/>
definitions into like you have always
done. However, the entire point of using custom schema tags is to make
configuration easier.
The rest of this chapter gives an overview of custom XML Schema based configuration that are included with the release.
Note | |
---|---|
As of Spring.NET 1.2.0 it is no longer necessary to explicitly configure the namespace parsers that come with Spring via a custom section in App.config. You will still need to register custom namespace parsers if you are writing your own. |
The tx
tags deal with configuring objects in
Spring's comprehensive support for transactions. These tags are covered
in the chapter entitled Chapter 17, Transaction management.
Tip | |
---|---|
You are strongly encouraged to look at the
|
In the interest of completeness, to use the tags in the
tx
schema, you need to have the following preamble at
the top of your Spring XML configuration file; the emboldened text in
the following snippet references the correct schema so that the tags in
the tx
namespace are available to you.
<?xml version="1.0" encoding="UTF-8"?>
<object xmlns="http://www.springframework.net"
xmlns:aop="http://www.springframework.net/aop"
xmlns:tx="http://www.springframework.net/tx">
<!-- <object/>
definitions here -->
<!-- <tx/> transaction definitions here -->
<!-- <aop/> AOP definitions here -->
</object>
Note | |
---|---|
Often when using the tags in the |
You will also need to configure the AOP and Transaction namespace parsers in the main .NET application configuration file as shown below
Note | |
---|---|
As of Spring.NET 1.2.0 it is no longer necessary to explicitly configure the namespace parsers that come with Spring via a custom section in App.config. You will still need to register custom namespace parsers if you are writing your own. |
<configuration> <configSections> <sectionGroup name="spring"> <!-- other Spring config sections handler like context, typeAliases, etc not shown for brevity --> <section name="parsers" type="Spring.Context.Support.NamespaceParsersSectionHandler, Spring.Core"/> </sectionGroup> </configSections> <spring> <parsers> <parser type="Spring.Aop.Config.AopNamespaceParser, Spring.Aop" /> <parser type="Spring.Transaction.Config.TxNamespaceParser, Spring.Data" /> </parsers> </spring> </configuration>
The aop
tags deal with configuring all things
AOP in Spring. These tags are comprehensively covered in the chapter
entitled Chapter 13, Aspect Oriented Programming with Spring.NET.
In the interest of completeness, to use the tags in the
aop
schema, you need to have the following preamble
at the top of your Spring XML configuration file; the emboldened text in
the following snippet references the correct schema so that the tags in
the aop
namespace are available to you.
<?xml version="1.0" encoding="UTF-8"?>
<objects xmlns="http://www.springframework.net"
xmlns:aop="http://www.springframework.net/aop">
<!-- <object/>
definitions here -->
<!-- <aop/> AOP definitions here -->
</objects>
You will also need to configure the AOP namespace parser in the main .NET application configuration file as shown below
Note | |
---|---|
As of Spring.NET 1.2.0 it is no longer necessary to explicitly configure the namespace parsers that come with Spring via a custom section in App.config. You will still need to register custom namespace parsers if you are writing your own. |
<configuration> <configSections> <sectionGroup name="spring"> <!-- other Spring config sections handler like context, typeAliases, etc not shown for brevity --> <section name="parsers" type="Spring.Context.Support.NamespaceParsersSectionHandler, Spring.Core"/> </sectionGroup> </configSections> <spring> <parsers> <parser type="Spring.Aop.Config.AopNamespaceParser, Spring.Aop" /> </parsers> </spring> </configuration>
The db
tags deal with creating
IDbProvider
instances for a given database client
library. The following snippet references the correct schema so that the
tags in the db
namespace are available to you. The
tags are comprehensively covered in the chapter entitled Chapter 19, DbProvider.
<?xml version="1.0" encoding="UTF-8"?>
<objects xmlns="http://www.springframework.net"
xmlns:db="http://www.springframework.net/db">
<!-- <object/>
definitions here -->
<!-- <db/> database definitions here -->
</objects>
You will also need to configure the Database namespace parser in the main .NET application configuration file as shown below
Note | |
---|---|
As of Spring.NET 1.2.0 it is no longer necessary to explicitly configure the namespace parsers that come with Spring via a custom section in App.config. You will still need to register custom namespace parsers if you are writing your own. |
<configuration> <configSections> <sectionGroup name="spring"> <!-- other Spring config sections handler like context, typeAliases, etc not shown for brevity --> <section name="parsers" type="Spring.Context.Support.NamespaceParsersSectionHandler, Spring.Core"/> </sectionGroup> </configSections> <spring> <parsers> <parser type="Spring.Data.Config.DatabaseNamespaceParser, Spring.Data" /> </parsers> </spring> </configuration>
The remoting
tags are for use when you want to
export an existing POCO object as a .NET remoted object or to create a
client side .NET remoting proxy. The tags are comprehensively covered in
the chapter Chapter 25, .NET Remoting
<?xml version="1.0" encoding="UTF-8"?>
<objects xmlns="http://www.springframework.net"
xmlns:r="http://www.springframework.net/remoting">
<!-- <object/>
definitions here -->
<!-- <r/> remoting definitions here -->
</objects>
You will also need to configure the remoting namespace parser in the main .NET application configuration file as shown below
Note | |
---|---|
As of Spring.NET 1.2.0 it is no longer necessary to explicitly configure the namespace parsers that come with Spring via a custom section in App.config. You will still need to register custom namespace parsers if you are writing your own. |
<configuration> <configSections> <sectionGroup name="spring"> <!-- other Spring config sections handler like context, typeAliases, etc not shown for brevity --> <section name="parsers" type="Spring.Context.Support.NamespaceParsersSectionHandler, Spring.Core"/> </sectionGroup> </configSections> <spring> <parsers> <parser type="Spring.Remoting.Config.RemotingNamespaceParser, Spring.Services" /> </parsers> </spring> </configuration>
The nms
tags are for use when you want to
configure Spring's messaging support. The tags are comprehensively
covered in the chapter Chapter 29, Message Oriented Middleware - Apache ActiveMQ
<?xml version="1.0" encoding="UTF-8"?>
<objects xmlns="http://www.springframework.net"
xmlns:r="http://www.springframework.net/nms">
<!-- <object/>
definitions here -->
<!-- <nms/> remoting definitions here -->
</objects>
You will also need to configure the remoting namespace parser in the main .NET application configuration file as shown below
Note | |
---|---|
As of Spring.NET 1.2.0 it is no longer necessary to explicitly configure the namespace parsers that come with Spring via a custom section in App.config. You will still need to register custom namespace parsers if you are writing your own. |
<configuration> <configSections> <sectionGroup name="spring"> <!-- other Spring config sections handler like context, typeAliases, etc not shown for brevity --> <section name="parsers" type="Spring.Context.Support.NamespaceParsersSectionHandler, Spring.Core"/> </sectionGroup> </configSections> <spring> <parsers> <parser type="Spring.Messaging.Nms.Config.NmsNamespaceParser, Spring.Messaging.Nms" /> </parsers> </spring> </configuration>
The validation
tags are for use when you want
definte IValidator
object instances. The tags are
comprehensively covered in the chapter Chapter 12, Validation Framework
<?xml version="1.0" encoding="UTF-8"?>
<objects xmlns="http://www.springframework.net"
xmlns:v="http://www.springframework.net/validation">
<!-- <object/>
definitions here -->
<!-- <v/> valdiation definitions here -->
</objects>
You will also need to configure the validation namespace parser in the main .NET application configuration file as shown below
Note | |
---|---|
As of Spring.NET 1.2.0 it is no longer necessary to explicitly configure the namespace parsers that come with Spring via a custom section in App.config. You will still need to register custom namespace parsers if you are writing your own. |
<configuration> <configSections> <sectionGroup name="spring"> <!-- other Spring config sections handler like context, typeAliases, etc not shown for brevity --> <section name="parsers" type="Spring.Context.Support.NamespaceParsersSectionHandler, Spring.Core"/> </sectionGroup> </configSections> <spring> <parsers> <parser type="Spring.Validation.Config.ValidationNamespaceParser, Spring.Core" /> </parsers> </spring> </configuration>
Last but not least we have the tags in the
objects
schema. Examples of the various tags in the
objects
schema are not shown here because they are
quite comprehensively covered in the section entitled Section 5.3.2, “Dependencies and configuration in detail” (and indeed in that
entire chapter).
<?xml version="1.0" encoding="UTF-8"?> <objects xmlns="http://www.springframework.net" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.net http://www.springframework.net/schema/objects/spring-objects-1.1.xsd"> <object id="foo" class="X.Y.Foo, X"> <property name="name" value="Rick"/> </object> </objects>
To setup VS.NET to provide intellisence while editing XML file for your custom XML schemas you will need to copy your XSD files to an appropriate VS.NET directory. Refer to the following chapter for details, Chapter 32, Visual Studio.NET Integration
For SharpDevelop, follow the directions on the "Editing XML" product documentation.