Basic XML configuration for beans in spring

Source: Internet
Author: User
Tags add constructor manual key string
Xml

Piecing together beans within the spring container is called assembly. When you assemble a bean, you are telling the container what beans are needed and how the container uses dependency injection to tie them together.
In theory, the bean assembly can be obtained from any resource, including property files, relational databases, and so on, but XML is the most common spring application system configuration source. Several containers in spring support the use of XML to assemble beans, including:
Xmlbeanfactory,
Classpathxmlapplicationcontext,
Filesystemxmlapplicationcontext,
Xmlwebapplicationcontext

Basic XML configurations include the following:

1. Add a bean
2. Set the properties of a bean
2.1 Manual Settings
2.1.1 By setter method
2.1.2 through the constructor
2.2 Automatic setting
Where the Bean's properties are member variables in the bean, these member variable values can be obtained by setter methods, such as name for a property, the setter method is SetName (String name), or initialized by the constructor when the class is instantiated. Setter methods, such as the SetName method, or calls to constructors, can be configured in an XML file so that the spring container is implemented automatically.

1. Add a bean
Here is an example:
<bean
id = "Mybean"
Class = "Blog.spring.MyBean"
Singleton = "false"
Init-method = "Initmethod"
Destroy-method = "Destroymethod"
Autowire = "Autowire type"
/>
The following is an explanation of each attribute in the label:
ID: Identifies the name of the bean and obtains the instance by Factory.getbean ("id").
Class: The Bean's classpath.
Singleton: The default is true, i.e. single-instance mode, each time Getbean ("id") gets the same
An instance, if set to false, that is, the prototype mode, and each fetch is a newly created
The instance.
Init-method: The method to invoke when the bean is instantiated (the method defined in the bean).
Destroy-method:bean the method to be invoked before removing it from the container.
Autowire: The method by which properties are automatically assembled.
For each of the above attributes, ID and class are necessary, and others can be omitted. For example, if the value of the Autowire is set, automatic assembly is required, otherwise it is manually assembled.

2. Manually set properties in a bean by setter method
The properties in the bean are identified by the <property> tag. There are several situations:
Simple Type properties
<bean id = "Mybean" class = "Blog.spring.MyBean" >
<property name = "Name" >
<value>springTest</value>
</property>
</bean>
referencing other beans
<bean id = "Mybean" class = "Blog.spring.MyBean"/>
<bean id = "Mybean1" class = "Blog.spring.MyBean1" >
<property name = "Name" >
<ref bean = "Mybean"/>
</property>
</bean>
You can also change the <ref> to
<bean class = ".." >
This is called an internal bean, and the disadvantage is that you cannot reuse instances of this bean elsewhere.
Assembling collections
There are several sets of assemblies:
Assemble list and array * * *
<property name = "NameList" >
<list>
<value>something</value>
<ref bean = "Blog.spring.MyBean"/>
<value>otherThing</value>
</list>
</property>
Assembly set****
<property name = "NameList" >
<set>
<value>something</value>
<ref bean = "Blog.spring.MyBean"/>
<value>otherThing</value>
</set>
</property>
Assembly map****
<property name = "NameList" >
<map>
<entry key = "Key1" >
<value>value1</value>
</entry>
<entry key = "Key2" >
<ref bean = "Mybean"/>
</entry>
</map>
</property>
Assembly properties****
<property name = "NameList" >
<props>
<prop key = "Prop1" >value1</prop>
<prop key = "PROP2" >value2</prop>
</props>
</property>
Set NULL
To null a property, you need to pass the <null/> label, and if not set, the property is the default value (when instantiated) instead of NULL.
<property name= "name" > <null/> </property>

The

3. Manually set the properties in the bean through the constructor
    Suppose there is a bean:
    public class Mybean {
         public Mybean (string arg1, MyBean1 arg2, String arg3)
   }
You can configure the Bean in XML in this way:
<bean id = "Mybean" class = "Blog.spring.MyBean"
         <constructor-arg index = "1"
             <value>springtest</value>
        <constructor-arg
        <constructor-arg index = "0"
             <ref bean = "Mybean1"/>
         <CONSTRUCTOR-ARG>
</bean>
where index is used to identify the position of the parameter in the constructor and starts at 0.

The

4. Let spring finish automatically assemble
    example:
<bean
id = "Mybean"
class = "Blog.spring.MyBean"
Autowire = " Autowire type
/>
The following are descriptions of several autowire types:
ByName: A bean or ID that tries to find the same property name as the one that needs to be automatically assembled in the container, if no corresponding bean is found, This property is not assembled.
Bytype: An attempt was made to find a bean or ID in the container that is the same as the type of property that needs to be automatically assembled, and if not found, the property is not assembled.
Constructor: Tries to find one or more beans in the container that are consistent with the constructor parameters of the bean that needs to be assembled automatically, and throws an exception if it is not found.
AutoDetect: First try to use constructor from the dynamic assembly, and then use the Bytype method.
As you can see from above, if a bean does not manually set the Autowire property, the default is manual assembly. If you need to set all the beans to auto assemble, you can set the Default-autowire property in the <beans> tab. The <beans> tag is the root of the entire XML document, and underneath it is a <bean>.
The value of the Default-autowire also has byname,bytype,constructor,autodetect four species.
For example, configure the following:
<beans default-autowire = "byname"
    ...
</beans>

Automatic assembly can create uncertainties. For example, you might find two identical types when using Bytype, and you don't know which one to use. So it is possible to mix automatic and manual assembly. For example, a bean is set to auto assemble, and one of its properties is manually set to its value, for example:
<bean id = "Mybean" class = "Blog.spring.MyBean"
Autowire = "Bytype"
>
<property name = "Name" >
<ref bean = "MyBean1" >
</property>
</bean>
With such a configuration, the name attribute in Mybean is manually assembled, and other attributes other than name are automatically assembled.




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.