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