Spring XML configuration file structure and bean naming __spring

Source: Internet
Author: User
Tags apc xml parser


The structure of the XML file is generally as follows

<?xml version= "1.0" encoding= "UTF-8"?> <beans xmlns=

"http://www.springframework.org/schema/" Beans "
xmlns:xsi=" http://www.w3.org/2001/XMLSchema-instance "
xmlns:context=" http:// Www.springframework.org/schema/context "
xsi:schemalocation="
http://www.springframework.org/schema/ Beans	      
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/ Schema/context		   
http://www.springframework.org/schema/context/spring-context-3.0.xsd ">

<bean id=" HelloWorld "class=" Main.java.com.sommer.learn.HelloWorldImpl "></bean>

</beans>

XMLNS is an acronym FOR XML Namespace, which represents the spring namespace. Spring finds all spring.handlers in classpath and resolves XML configuration namespaces and corresponding processing classes. These items of namespaces are not fixed and can be selected from http://www.springframework.org/schema/according to requirements.

Let's not discuss it here, mainly looking at the composition of <bean> </bean>, because it represents how to get objects (beans) from the IOC container and complete the functionality we need.


The above code <bean id= "HelloWorld" class= "Main.java.com.sommer.learn.HelloWorldImpl" > </bean> where HelloWorld represents the identity of the bean, Main.java.com.sommer.learn.HelloWorldImpl the class that represents the bean. This is just one of the simplest configurations of beans.

The bean's configuration entry is as follows:

fully qualified class name ( class ): Used to define the implementation class for the bean;

Bean behavior : These define the behavior of the bean in the container, including scope (single case, prototype creation), inertia initialization and lifecycle, etc.

Bean How to create : Whether the description is created through a constructor or a factory method;

Bean Relationships : References to other beans, that is, dependency definitions, which can also be referred to as colleague beans or dependent beans, that is, dependency injection.


In general, only fully qualified class names are required, and others are optional.


Name of Bean

1. Do not specify the ID, only the required fully qualified class name, generated by the IOC container for its identity, the program must Pass "Getbean (class<t> requiredtype)" to obtain the bean;

<bean class= "Main.java.com.sommer.learn.HelloWorldImpl" ></bean>

Get the Bean's program

ApplicationContext APC = new Classpathxmlapplicationcontext ("Springxml/helloworld.xml");  
HelloWorld Hello = Apc.getbean (helloworld.class);  
System.out.println (Hello.sayhi ());  

2. Specify ID, must be unique in IOC container;

<bean id= "HelloWorld" class= "Main.java.com.sommer.learn.HelloWorldImpl" ></bean>

Get the Bean's program

ApplicationContext APC = new Classpathxmlapplicationcontext ("Springxml/helloworld.xml");  
HelloWorld Hello = Apc.getbean ("HelloWorld", helloworld.class);  
System.out.println (Hello.sayhi ());  


3. Specify name, which must be unique in the IOC container

<bean name= "HelloWorld" class= "Main.java.com.sommer.learn.HelloWorldImpl" ></bean>

Get the Bean's program

ApplicationContext APC = new Classpathxmlapplicationcontext ("Springxml/helloworld.xml");  
HelloWorld Hello = Apc.getbean ("HelloWorld", helloworld.class);  
System.out.println (Hello.sayhi ()); 

4. Specify aliases alias (one bean can have multiple)

<bean name= "HelloWorld" alias= "alias1" class= "Main.java.com.sommer.learn.HelloWorldImpl" ></bean>


Get the Bean's program

ApplicationContext APC = new Classpathxmlapplicationcontext ("Springxml/helloworld.xml");  
HelloWorld Hello = Apc.getbean ("alias1", helloworld.class);  Here, of course, you can get
System.out.println (Hello.sayhi ()) according to name;  

If both ID and Name,id are specified as identifiers, and name is an alias, it must be unique within the IOC container;

If more than one name is specified, the first is used as an identifier, the other is an alias, and all identifiers must be unique within the IOC container;

Note: The name or ID are all "identifiers", so why do they exist at the same time? This is because the ID in XML is a true XML id attribute when using xml-based configuration metadata, so you can use an XML parser to verify that this ID of the reference exists, thus finding out earlier whether a nonexistent bean was referenced and using name, You might want to find a reference to a nonexistent bean when you actually use the bean.





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.