Bean configuration example in spring

Source: Internet
Author: User

The following is a complete configuration instance:

<Beans>
<Description>
Spring bean configuration sample
</Description>
<Bean
Id = "theaction"
Class = "net. xiaxin. Spring. Qs. upperaction"
Singleton = "true"
Init-method = "init"
Destroy-method = "Cleanup"
Depends-on = "actionmanager"
>

<Property name = "message">
<Value> Hello </value>
</Property>

<Property name = "DESC">
<Null/>
</Property>

<Property name = "datasource">
<Ref local = "datasource"/>
</Property>
</Bean>

<Bean id = "datasource" class = "org. springframework. JNDI. jndiobjectfactorybean">
<Property name = "jndiname">
<Value> JAVA: COMP/ENV/jdbc/sample </value>
</Property>
</Bean>
</Beans>

 

1. ID
The unique identifier of the Java Bean in beanfactory. This index name must be used to obtain the JavaBean instance through beanfactory in the code.

2. Class
Java Bean class name

3. Singleton
Specifies whether the Java Bean adopts Singleton mode. If it is set to "true", only one Java Bean instance is maintained within the scope of beanfactory, the Code uses beanfactory to obtain the reference of this Java Bean instance. Otherwise, if it is set to "false", when beanfactory is used to obtain the Java Bean instance, beanfactory will create a new instance every time and return it.

4. init-Method
Initialization method. This method is executed after beanfactory creates a JavaBean instance and before returning a reference to the application layer. It is generally used for initialization of some resources.

5. Destroy-Method
Destruction method. This method will be executed when beanfactory is destroyed and is generally used for resource release.

6. Depends-on
Bean dependency. Generally, this parameter is not required. Spring organizes the construction of various dependencies according to the situation (the depends-on attribute in this example is not mandatory ). Only in some special cases, such as some static variables in JavaBean, need to be initialized (this is a badsmell and should be avoided in design ). You can use depends-on to specify its dependency to ensure that the resources specified by depends-on are loaded before the bean is loaded.

7. <value>
You can specify the attribute value through the <value/> node. Beanfactory will automatically match according to the attribute type corresponding to Java Bean. The "DESC" attribute below provides an example of setting a null value. Note: <value> </value> indicates an empty string. To set the attribute value to null, you must use the <null/> node.

8. <ref>
Specifies the reference relationship between attributes and other beans in beanfactory. In this example, the datasource attribute of theaction references the bean with the ID of datasource. Beanfactory will create a datasource bean instance at runtime and pass its reference to the datasource attribute of theaction bean.

The following code demonstrates how to obtain bean instances through beanfactory:
Inputstream is = new fileinputstream ("bean. xml ");
Xmlbeanfactory factory = new xmlbeanfactory (is );
Action action = (Action) Factory. getbean ("theaction ");

In this case, beanfactory loads the obtained action instance and initializes the instance and sets its attributes based on the configuration file.

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.