Bean configuration example in Spring

Source: Internet
Author: User

Bean configuration example in Spring: Below is a complete Bean configuration example: <beans> <description> Spring Bean Configuration Sample </description> <bean id = "TheAction" www.2cto.com 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. Unique id of Java Bean in BeanFactory, in the code, you must use BeanFactory as the index name to obtain the JavaBean instance. 2. class Java Bean class name 3. singleton specifies whether this Java Bean uses Singleton mode. If it is set to "true", it is within the scope of BeanFactory, only one instance of this Java Bean is maintained, and the code obtains reference of this Java Bean instance through BeanFactory. 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 will be 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. 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 a Bean instance 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.

Related Article

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.