configuring Beans in Spring's IOC container
Configuration Bean form: based on XML file mode, annotation-based approach
To configure the bean through the bean node in the XML file:
<?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.xsd" > <Beanname= "Hellospring"class= "Com.dx.spring.beans.HelloSpring"> < Propertyname= "Name"value= "Spring"></ Property> </Bean></Beans>
Reference: "Spring (ii): Spring Framework &hello Spring" in the configuration process
Name of the Id:bean
1) must be unique within the IOC container;
2) If the ID is not specified, spring will automatically train the whole nag as the name of the bean;
3) ID can specify more than one name, separated by commas, semicolons, or spaces.
Bean Configuration: through the full class name (reflection implementation, at which point the Bean class is required to have a parameterless constructor), through the factory method (static factory method & Instance Factory method), Factorybean
IOC container Beanfactory&applicationcontext Overview Spring container
1) It must be instantiated before the spring IOC container reads the bean configuration to create the bean instance. The bean instance can be obtained from the IOC container and used only after the container is instantiated.
2) Spring provides two types of IOC container implementations
---The basic implementation of the BEANFACTORY:IOC container. Beanfactory is the infrastructure of the spring framework, facing spring itself.
---applicationcontext: Provides more advanced properties, which are sub-interfaces of the beanfactory. ApplicationContext for developers using the spring framework, almost all applications use ApplicationContext directly, rather than the underlying beanfactory.
Note:
1) The configuration file is the same regardless of which one is used in both ways.
2) How to do not Applicatincontext is beanfactory sub-interface:
Class definitions for Applicatincontext:
//Compiled from Applicationcontext.java (version 1.8:52.0, no super bit) Public Abstract InterfaceOrg.springframework.context.ApplicationContext
extendsorg.springframework.core.env.EnvironmentCapable,
Org.springframework.beans.factory.ListableBeanFactory,
Org.springframework.beans.factory.HierarchicalBeanFactory,
Org.springframework.context.MessageSource,
Org.springframework.context.ApplicationEventPublisher,
Org.springframework.core.io.support.ResourcePatternResolver {//Method Descriptor #10 () ljava/lang/string;@org. Springframework.lang.Nullable Public Abstractjava.lang.String getId (); //Method Descriptor #10 () ljava/lang/string; Public Abstractjava.lang.String getapplicationname (); //Method Descriptor #10 () ljava/lang/string; Public Abstractjava.lang.String GetDisplayName (); //Method Descriptor #16 () J Public Abstract Longgetstartupdate (); //Method Descriptor #18 () Lorg/springframework/context/applicationcontext;@org. Springframework.lang.Nullable Public AbstractOrg.springframework.context.ApplicationContext getParent (); //Method Descriptor #20 () lorg/springframework/beans/factory/config/autowirecapablebeanfactory; Public AbstractOrg.springframework.beans.factory.config.AutowireCapableBeanFactory Getautowirecapablebeanfactory ()throwsjava.lang.IllegalStateException;}
From the top we know ApplicationContext inherited the org.springframework.beans.factory.ListableBeanFactory, Org.springframework.beans.factory.HierarchicalBeanFactory interface, and both interfaces inherit the Org.springframework.beans.factory.BeanFactory interface.
Public Interfacebeanfactory {String factory_bean_prefix= "&"; Object Getbean (String name)throwsbeansexception; <T> T Getbean (String name, @Nullable class<t> requiredtype)throwsbeansexception; Object Getbean (String name, Object ... args)throwsbeansexception; <T> T Getbean (class<t> requiredtype)throwsbeansexception; <T> T Getbean (class<t> requiredtype, Object ... args)throwsbeansexception; BooleanContainsbean (String name); BooleanIssingleton (String name)throwsnosuchbeandefinitionexception; BooleanIsprototype (String name)throwsnosuchbeandefinitionexception; BooleanIstypematch (String name, Resolvabletype typetomatch)throwsnosuchbeandefinitionexception; BooleanIstypematch (String name, @Nullable class<?> typetomatch)throwsnosuchbeandefinitionexception; @Nullable Class<?> GetType (String name)throwsnosuchbeandefinitionexception; String[] Getaliases (String name);}
Therefore, it is said that ApplicationContext is the sub-interface class of Beanfactory.
Applicaioncontext
ApplicationContext's main implementation class:
---classpathxmlapplicationcontext: Loading a configuration file from a Classpath
---filesystemxmlapplicationcontext: Loads the configuration file from the file system.
The way of Dependency injection: attribute injection; constructor injection; factory methods (not recommended, not commonly used)
Inject attribute value details
Automatic assembly
The relationship between beans: inheritance, dependency.
Bean Scope: Singleton;property;web Environment scope
Using the external properties file
Spel
Declaration period of beans in IOC container
spring4.x new concept: generic dependency Injection.
Spring (eight): Spring config Bean