Java Spring Configuration Data unit

Source: Internet
Author: User

Fundamentals-Containers and beans

In spring, the bodies that make up your application (backbone) and the objects that are managed by the spring IoC container are called beans. In short, the bean is the object that is initialized, assembled, and managed by the Spring container , except that the bean is no different from the other objects in the application.

In other words, spring is actually the process of beans.xml the class within the <bean> tag by listening to the reflection mechanism when loading the configuration file. This can be judged by writing something in the default parameterless construction method of the class.

1. Configure meta-data

The basic structure of XML-based configuration metadata: Beans.xml

<?XML version= "1.0" encoding= "UTF-8"?><Beansxmlns= "Http://www.springframework.org/schema/beans"Xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance"xsi:schemalocation= "Http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5. XSD ">  <BeanID="..."class="...">    <!--collaborators and configuration for this bean go here -  </Bean>  <BeanID="..."class="...">    <!--collaborators and configuration for this bean go here -  </Bean>  <!--more beans when the referenced XML file must be a file with the spring DTD header -    <ImportResource= "Services.xml"/></Beans>    
Services.xml in the configuration file name is actually the same ID and name are the same
<?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"Xmlns:tx= "Http://www.springframework.org/schema/tx"xsi:schemalocation= "Http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/ Spring-beans-2.5.xsd Http://www.springframework.org/schema/context Http://www.springframework.org/schema/co Ntext/spring-context-2.5.xsd Http://www.springframework.org/schema/tx Http://www.springframework.org/schema /tx/spring-tx-2.5.xsd "> <BeanID= "UserService"name= "UserService" class= "Com.sun.service.UserService">< Propertyname= "Name">  <value>Sunxin</value></ Property></Bean> </Beans>

2. Instantiating a container
ApplicationContext context = new Classpathxmlapplicationcontext (        new string[] {"Beans.xml"});
2. The Bean Alias
the!--name points to a bean,alias that already exists for that ID, which is the alias given to the bean-- <  name= "UserService"  alias= "user"/>

The call can be done by:

    UserService US  = (userservice) app.getbean ("User");

Java Spring Configuration Data unit

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.