Simplified spring (1)-configuration file

Source: Internet
Author: User

Author: Jiangnan Baiyi

Collation

Everyone loves spring and hibernate.
However, the sample code of spring MVC + hibernate, such as appfuse, cannot be the most concise, elegant, and easy to read. If we continue to use the best sample painting method in our own projects, good wishes may not be fulfilled.
Therefore, the spirit of pramatic will not go off. This series is to find the most suitable spring + hibernate mode.

I-Simplified configuration file

I'm tired of all the frameworks with heavy configuration files.
The best case is,The Framework provides extremely flexible and complex configuration methods, but only when you need them.
 
Spring provides three possibilities to simplify XML. With the improvement of domestic user level, all these basic simplification skills have been mastered.
Let's look at the Follow-up improvements of 3rd, 4th -- spring 1.2 and spring 2.0.

1.1.autowire = "byname"/"bytype"

Assuming that the controller has a property named mermerdao, spring will find the bean named customerdao in the configuration file and automatically inject the bean to the Controller.
If the bean has two attributes, one for default injection and the other for customization, you only need to set autowire and explicitly declare the one for customization. This should meet the requirements and provide configuration when special configuration is required. Otherwise, a default injection will be provided to me.

In another lazy place, write default-autovwrie = "byname" in the root <beans> node to make all beans in the file default autowrie.
Rod believes that this can be done in the development phase, but autowire should not be used on the production server. I think those custom ones, such as transcationmanager, should be defined in detail, while Dao and controller can be a bit lazy if there are a large number of repeated bean definitions.

1.2. <bean> Abstract Public definitions and inner beans between nodes

This is too convenient and lazy. I cannot think of two independent XML nodes that can play with inheritance and derivation. The child node has all the attributes of the parent node.
It is best to use the Transtion proxy definition. First define a long and redundant parent class, and then inherit it with the subclass.

In addition, there is an inner bean mechanism that can write Dao as the internal class of proxy. Why write it as an internal class? In order to let the proxy impersonate it to let the Controller autowire. (For details, see the following example)

1.3. Loose configuration, to XML or not to XML
It is said that spring is much looser than struts configuration, which gives people the opportunity to recall things from the configuration file in the original code.
I do not agree to expose everything to the configuration file, resulting in a rich information configuration file. When modifying or viewing the configuration file, you must open the configuration file and the original code at the same time to understand everything.
I want the configuration file to focus on some overall configuration, as well as the redundant code required by the framework that does not need to be managed. However, for configuration and logic with little changes in some details, try not to stick it in. Therefore, we do not recommend that you configure success/fail view.

2. Simplified configuration file

1. Only one sentence left for Controller

<Bean name = "customercontroller" class = "org. springside. Bookstore. Web. customercontroller" autowire = "byname"/>

2. There is only one sentence left for Dao.

<Bean id = "customerdao" class = "org. springside. Bookstore. Dao. customerdao"/>

3. There are only five lines left in the service class

<Bean id = "customermanager" parent = "basetxservice">
<Property name = "target">
<Bean class = "org. springside. Bookstore. Service. customermanager"/>
</Property>
</Bean>

3. Simplified XML syntax after spring 1.2

 The main simplification is to extract the attribute values and reference beans fromSubnodeChanged backAttribute ValueIt is useful for brothers who do not like autowire.
Of course, if the value requires CDATA, it must use subnodes. In addition, list values can be separated by spaces, which is more practical.

1. Attribute Value

<Property name = "foo">
<Value> foovalue </value>
</Property>
Simplified
<Property name = "foo" value = "foovalue"/>

2. Reference bean <property name = "foo">
<Ref bean = "foobean">
</Property>
Simplified
<Property name = "foo" ref = "foobean"/>

3. List can be simplified to a string separated by Spaces

 

<Property name = "myfriendlist">
<List>
<Value> gigix </value>
<Value> wuyu </value>
</List>
</Property>
Simplified
<Property name = "myfriendlist" value = "gigix wuyu"/>



4. Spring 2.0 is coming.
If there is no external force, spring XML may not change. But now XML has become a street mouse, which is forced by ror's default configuration and jdk5's Annotation. Of course, we need to continue to make changes.
For example, some developers think that the node name must start with bean and append an attribute ID to indicate the bean name. The attribute value must create a property subnode, the sub-node has an attribute name to indicate the attribute name, which is very intuitive to the machine.

<Bean id = "customerdao" class = "org. springside... customerdao">
<Property name = "maxcount" value = "10">
</Bean>

What you want to see should be written

<Customerdao class = "org. springside... customerdao" maxcount = "10"/>

Spring 2.0 Implements similar syntaxes using schema. For details, see its jpetstore sample.

5. Use the DTD provided by spring to make the editor smart.

If you do not use the spring plug-in of Eclipse, you should at least use the DTD that comes with spring to make the XML editor smart, which can automatically generate attributes for you and determine whether node/attribute names are spelled incorrectly.

6. More unusual simplified configuration methods
For example, autoproxy, but I think it is more simplified and uncontrollable, so it is not used.

Related Articles
Simplified spring (1) -- configuration file
Simplified spring (2) -- model layer
Simplified spring (3) -- controller Layer
Simplified spring (4) -- View Layer

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.