Spring XML configuration file structure

Source: Internet
Author: User
Tags apc

Spring XML configuration file structure

 

The XML file structure is generally as follows:

Xmlns is short for XML Namespace, which indicates spring Namespace. Spring searches for all spring. handlers in Classpath and parses the namespace configured in xml and corresponding processing classes. Those items of the namespace are not fixed and can be selected from the http://www.springframework.org/schema/ as needed.

We will not discuss it here. It indicates how to obtain the object (bean) from the IoC container and complete the required functions.

 

The above code HelloWorld indicates the bean identifier, and main.java.com. sommer. learn. HelloWorldImpl indicates the bean class. This is just the simplest configuration of bean.

Bean configuration items are as follows:

Fully Qualified class name (class): Defines Bean implementation classes;

BeanAction: These define the Bean behavior in the container, including the scope (Singleton, prototype creation), whether it is inert initialization, And the lifecycle;

BeanCreation Method: Whether it is created through the constructor or the factory method;

BeanRelationship: References to other beans, that is, dependency definition. These referenced beans can also be called colleague beans or dependent beans, that is, dependency injection.

 

Generally, only the fully qualified class names are required, and others are optional.

 

Bean name

1. If no id is specified, only the fully qualified Class names are configured. The IoC container generates an id. The program must pass the "getBean (Class RequiredType) "to get Bean;

Get 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 in the Ioc container;

Get bean Program

ApplicationContext apc = new ClassPathXmlApplicationContext(springXML/HelloWorld.xml);  HelloWorld hello = apc.getBean(helloWorld,HelloWorld.class);  System.out.println(hello.sayHi());  

 

3. specify the name, which must be unique in the Ioc container

Get bean Program

ApplicationContext apc = new ClassPathXmlApplicationContext(springXML/HelloWorld.xml);  HelloWorld hello = apc.getBean(helloWorld,HelloWorld.class);  System.out.println(hello.sayHi());  

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

Get bean Program

ApplicationContext apc = new ClassPathXmlApplicationContext (springXML/HelloWorld. xml); HelloWorld hello = apc. getBean (alias1, HelloWorld. class); // You can also obtain System by name. out. println (hello. sayHi ());

If both id and name are specified, id is the identifier, and name is the alias, which must be unique in the Ioc container;

If multiple names are specified, the first one is used as the identifier, and the others are aliases, all identifiers must also be unique in the Ioc container;

Note: The name or id are both "identifiers". Why do they exist at the same time? This is because when XML-based configuration metadata is used, the id in XML is a real XML id attribute. Therefore, the XML Parser can be used to verify whether the referenced id exists, in this way, we can see whether a nonexistent bean is referenced earlier. However, when using name, we may need to see that a nonexistent bean is referenced only when the bean is actually used.

 

 

 

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.