Spring XML configuration file structure

Source: Internet
Author: User
Tags apc


The structure of the XML file is generally as follows

<?xml version= "1.0" encoding= "UTF-8"? ><beansxmlns= "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.xsdhttp://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 abbreviation for XML Namespace, which represents the spring namespace. Spring finds all the spring.handlers in Classpath and parses the XML configuration namespace with the corresponding processing class. These items of the namespace are not fixed and can be selected from http://www.springframework.org/schema/according to demand.

Here we do not discuss it, mainly look at the composition of <bean> </bean>, because it shows how to get the object (bean) from the IOC container and do the functions we need.


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

The bean configuration items are as follows:

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

Bean Behavior : These define the Bean The behavior in the container, including scope (singleton, prototype creation), lazy initialization and life cycle, etc.;

Bean How to create : Describes whether to create from a constructor or factory method ;

Bean Relationships : That is, for other Bean reference, which is the dependency definition, which Bean You can call it a colleague . Bean or dependent Bean , i.e. dependency injection.


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


Name of the Bean

1. Do not specify an ID, configure only the required fully qualified class name, generate an identity for it by the IoC container, the program must be obtained through "Getbean (class<t> requiredtype)" Bean;

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

Get The Bean Program

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

2. Specify the ID, which must be unique within the Ioc container;

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

Get The Bean Program

<span style= "FONT-SIZE:12PX;" >applicationcontext APC = new Classpathxmlapplicationcontext ("Springxml/helloworld.xml");  HelloWorld Hello = Apc.getbean ("HelloWorld", helloworld.class);  System.out.println (Hello.sayhi ());  </span>


3. Specify name, must <alias alias= "alias1" name= "bean"/> must be unique in the Ioc container

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

Get The Bean Program

<span style= "FONT-SIZE:14PX;" ><span style= "FONT-SIZE:12PX;" >applicationcontext APC = new Classpathxmlapplicationcontext ("Springxml/helloworld.xml");  HelloWorld Hello = Apc.getbean ("HelloWorld", helloworld.class);  System.out.println (Hello.sayhi ());  </span><span style= "Font-family:microsoft Yahei;" ></span></span>

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

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

<aliasalias= "ALIAS1" name= "Bean"/>

Get The Bean Program

ApplicationContext APC = new Classpathxmlapplicationcontext ("Springxml/helloworld.xml");  HelloWorld Hello = Apc.getbean ("alias1", helloworld.class);  It is also possible to get System.out.println (Hello.sayhi ()) by name.  

id and name id is the identifier, and name ioc container only;

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

Note:nameorIDas "identifiers", whyexist at the same time? This is because when using theXMLWhen you configure metadata for aXMLinIDis a realXML IDproperty, socan take advantageXMLparser to verify the reference to thisIDis present, so that earlier discoveries are made of references to a non-existentBean, while usingname, you may want to actually use theBeanwhen you find a reference to a non-existentBean.





Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Spring XML configuration file structure

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.