3.1 Spring IOC container and Beans Brief introduction
The Beanfactory interface provides the configuration of arbitrary objects;
ApplicationContext is a sub-interface of Beanfactory. Integrated with spring AOP functionality, message resource control. Event advertisement, Application Layer special context (in Web application)
The objects that are instantiated, assembled, and managed by the IOC container are bean
3.2 Container Overview
Org.springframework.context.ApplicationContext represents the spring IOC container and is responsible for instantiating, configuring, and assembling beans by reading configuration metadata. Configuration metadata can be expressed in three forms: Xml,java gaze, Java code (this does not understand)
In an independent environment. Instantiates an Classpathxmlapplicationcontext or Filesystemxmlapplicationcontext object.
3.2.1 Configuring metadata
In addition to the configuration in the XML file, there are two ways:
annotation-based configuration:?
java-based configuration:?
Can you use spring's integration with ASPECTJ to configure objects that are not under IOC container control?
Based on the basic form of the XML configuration:
<? xml version= "1.0" encoding= "UTF-8"?><Beans xmlns="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-3.0.x SD "> <Bean ID="..." class="..."> <!--collaborators and configuration for this bean go here--> </bean> <Bean ID="..." class="..."> <!--collaborators and configuration for this bean go here--> </bean> <!--more beans definitions go here--></beans>
3.2.2 Instantiation of a container
ApplicationContext context = new classpathxmlapplicationcontext (new string[] {" Services.xml"" Daos.xml "});
Note: The 4th chapter has a more convenient mechanism for reading XML files?
3.2.2.1 composition of meta-data based on XML configuration
<Beans> <Import Resource="Services.xml"/> <Import Resource="Resources/messagesource.xml"/> <Import Resource="/resources/themesource.xml"/> <Bean ID="Bean1" class="..."/> <Bean ID="Bean2" class="..."/></beans>
Services.xml must be in the same folder as the XML file that contains the file, or under the Classpath folder.
Able to pass relative path ". /path "This way to configure the file path, but this method is not recommended (in this way to create a dependent file is outside the current application?) )。 In particular, it is not recommended for a URL such as "Classpath:" (The execution-time resolver chooses "recent" classpth and looks at its parent folder, causing the corresponding XML file to be found).
Ability to use:"File:c:/config/services.xml" or "Classpath:/config/services.xml". But remember that if you use absolute paths, there is a coupling problem.
3.2.3 Using containers
//Create and configure BeansApplicationContext context =NewClasspathxmlapplicationcontext (NewString[] {"Services.xml","Daos.xml"});//Retrieve configured instancePetstoreserviceimpl Service = Context.getbean ("Petstore", Petstoreserviceimpl.class);//Use configured instanceList userlist service.getusernamelist ();
SPRING3.0 Official Website Document study Note (iv)--3.1~3.2.3