I. Introduction of Beanfactory
1.1. Bean:
- In spring technology is component-based
- The most basic is the most commonly used unit
- In fact, the instance is stored in the spring container.
Beans are typically defined in a configuration file, Bean instantiation is managed by the spring IOC container, instances of beans can be accessed through beanfactory, and in fact most of the Java EE applications, The bean is accessed through ApplicationContext, ApplicationContext is Beanfactory's sub-interface, the function is much stronger than beanfactory
1.2. Beanfactory effect
- configuring, creating, and managing bean objects
- Maintain dependency between bean objects
- Responsible for the life cycle of the Bean object
Beanfactory Common methods:
- Containsbean (String beanname)
- Object Getbean (String beanname)
Typically, the Xmlbeanfactory class is used to implement
1.3, ApplicationContext
- Read Bean definition file
- Maintain dependency between beans
- Support for internationalization
- The Read Getrource () method of the resource file can easily read the Rource object
- Event propagation
- Multiple configuration file loading
Ii. Bean Definition 2.1, basic composition
Configuration file
- <beans/> is the root node of the sring configuration file
- A <beans/> node can have multiple <bean> nodes
When defining a <bean> node, you typically specify two properties
ID: Used to indicate the bean identifier, which is unique, spring's management of the bean and the dependency between the beans requires this property
Class: Indicates the specific implementation class of the bean, which cannot be the full path package name of the interface (which can be an interface implementation Class). Class name
Bean in spring container two behaviors (set by scope default not written as Singelton)
Singleton: Single-instance mode (default, constructed as private), only one shared instance exists in the entire spring container (singleton)
Non-singelton: Each time the bean,spring container is requested, a new bean instance is created and returned to the program (Request,session,prototype)
2.2. Creating beans
Name of the Bean:
Follow the naming conventions in Java, using clear, descriptive, consistent naming conventions
The bean naming mechanism:
ID when looking for a bean object in the Spring window, the first lookup is based on the ID, the rest is the default name of the Bean, and if the id attribute does not exist, the name attribute is used to find it (the first name is the default name). If both the ID and name do not exist, look for the name of the class
ID---------->name---------------> class name
<bean id= "ID1" class= "Implementation Class" ></bean><bean name= "ID1" class= "Implementation Class" ></bean><bean class= " Implementation Class "></bean>
The name of the bean:
specified by the Alias property:
<alias name= "Specifies the name of the associated bean FromName" alias= "the name of the alias of the associated class ToName"/>
2.3. Create Bean Step
Creating an XML file----writing configuration information---generating a bean class----adding beans to the configuration file
Third, Bean injection 3.1, basic type and string
- Use the value element
- The XML parser parses the data as a string type
If the property is not of type string, the property value is converted to another type by propertyeditors
3.2. Inject beans
-ref Elements for identification
A ref element typically has two properties:
Bean: Specifies the ID of a bean that is not in the same XML file
<bean id= "test" class= "Com.pb.test" ><property name= "user" ><ref bean= "other Bean's id"/></property ></bean>
Ocal: Specifies the ID of the bean in the same XML file
<bean id= "test" class= "Com.pb.test" ><property name= "user" ><ref bean= "ID of the bean in the same XML file"/></ Property></bean>
The difference between value and ref:
Using the ref element allows spring to verify that the dependent bean is real at deployment time
Use the value element to specify that validation only occurs when the bean instance is created, resulting in an incorrect delay and additional type-conversion overhead
3.3. Set Injection
List:
<bean id= "test" class= "Com.pb.test" > <property name= "lists" > <list> <value>1 </value> <value>2</value> <value>3</value> </list> </ Property> </bean>
MAP:
<bean id= "test" class= "Com.pb.test" > <property name= "map" > <map> <entry key= "Key1 "> <value>value1</value> </entry> <entry key=" Key2 "> <value> key2</value> </entry> </map> </property> </bean>
Props
<bean id= "test" class= "Com.pb.test" ><property name= "props" ><props><prop key= "Key1" >value1 </prop><prop key= "Key2" >value2</prop></props></property></bean>
Set
< property name = "Interest" > < Set > < value > Sing </value > < value > Dance & lt;/value > < value > Calligraphy </value > </set > </property >
3.4. Automatic binding
Using the Autowire property to set
No: the default. Default is non-binding
ByName: Automatic binding based on property name
Bytype: Automatic binding based on attribute type
Iv. Scope of the bean
- Singleton: (Single-instance mode) The spring container will only have a shared bean instance, and all requests for that Bean will only return the same bean instance.
- Propertype (No-singleton): A new bean instance is generated for each request to the bean. Equivalent to the new operation in Java. Beans defined as Propertype have a long life cycle, are difficult to recycle, and often have additional processing.
- Request: A new bean instance is generated for each HTTP request, and theBean is valid only within the current HTTP requests range
- Session: A new bean instance is generated for each HTTP request, andThe bean is valid only within the current HTTP session range
V. Bean Management life cycle 5.1, bean life cycle
5.2. Life Cycle Management
Two opportunities
Spring can manage the behavior between instantiating beans and before destroying them
After you inject the dependency relationship:
- Use the Init-method property: by specifying the Init-method property, you determine that a method should be executed after the bean dependency has ended. There is no need to couple code with spring's interface in such a way that code pollution is minimal. Typically, a method definition such as the init () method is used in the bean, and then the Init-method attribute is added to the configuration file bean element to implement the process.
- Implement the Innitializingbean interface: This method does not need to specify the Init-method property, when the window dependency injection, the Afterpropertiesset method is automatically called, and it is the same as the Init-method execution effect, However, this approach is not recommended for intrusive code design
Before destroying the bean:
- Destroy-method: A method that is used to execute a bean before it is destroyed, in a way that is as init-method as a pressure-free code that is coupled with the spring interface to minimize code contamination. Add the Destory-method attribute to the bean and implement the process
- Implement Disposeablebean interface: There is no need to indicate the Destory-method property, when the container dependency injection, will automatically call the Destroty method, is not recommended for intrusive code design
Vi. Inheritance of beans
Bean Inheritance:
What is bean inheritance? Inheritance refers to the child bean can inherit the configuration information from the parent bean , or can overwrite the specific configuration information, or add the new configuration information on the basis of the parent bean, which is similar to the inheritance of the subclass in Java and the parent class, which can save a lot of configuration work by using inheritance. In the actual project application, the shared configuration is configured as a template for the child bean to inherit, and if the configuration information between the 2 beans is approximately the same, the bean inheritance can be used to reduce the configuration effort .
Bean's Template:
In spring, since you want to configure a common configuration, the template will not need to be instantiated , but only used as a template for the child bean, but all beans are initialized by default in ApplicationContext or Beanfactory.
Use the abstract property to prevent the template from being instantiated
Abstract= "True" indicates that the bean is an abstract bean and cannot be initialized.
Spring (ii) Introduction to Beans