Pure XML Mode
Adopt Spring IoC and pure XML mode to transform the previous IoC and AOP implementation.
Preparation
We remove the previous beans.xml file and add:
- applicationContext.xml
Three Ways to Create Beans
Previously, we used the beans.xml file with BeanFactory to complete Bean initialization and other configurations. Next, we will use the Spring framework for configuration. After configuring beans in applicationContext, we let Spring manage the Beans.
Using No-Arg Constructor
By default, it creates objects through reflection by calling the no-arg constructor. If the class does not have a no-arg constructor, creation will fail.
<bean id="wzkTransferService" class="wzk.service.impl.WzkTransferServiceImpl"></bean>
Using Static Method
In actual development, some objects are sometimes not directly created through constructors. There may be many additional operations during creation. At this point, a method for creating objects is provided, and恰好 this method is static modified.
For example, when using JDBC, we use implementation classes of java.sql.Connection interface. For MySQL database, it is JDBC4Connection, but we would not write JDBC4Connection connection = new JDBC4Connection(). We need to register the driver and provide URL and other information.
In actual development, especially in early projects when we did not use the Spring framework to manage and create objects, but used factory pattern for decoupling, when integrating with Spring, this approach can be used.
<bean id="transferService" class="wzk.factory.BeanFactory" factory-method="getTransferService"></bean>
For example, if we have a singleton class that does many operations to ensure safety, and the getInstance() method is static. Using no-arg constructor is also not feasible (private constructor), so we need a factory-method to get the object.
Using Instance Method Creation
This method is similar to the static method creation above. The difference is that the method to get the object is no longer static, but an ordinary method in the class. This method is used more frequently than static method creation.
In early projects, factory methods could be static or non-static. When non-static methods are used, the following configuration method can be adopted:
<bean id="beanFactory" class="wzk.factory.BeanFactory"></bean>
<bean id="transferService" factory-bean="beanFactory" factory-method="getTransferService"></bean>
Bean Lifecycle
Scope Change
When the Spring framework manages Bean object creation, Beans are singleton by default. However, it supports configuration to change the scope. See the official documentation for scope options.
Among the options provided above, the most used in actual development are Singleton and Prototype (also called multi-instance mode).
<!-- Configure service object -->
<bean id="wzkTransferService"
class="wzk.service.impl.WzkTransferServiceImpl" scope="singleton"></bean>
Lifecycle for Different Scopes
Singleton mode: singleton
- Object initialization: Object is created when the container is created
- Object survival: Object lives as long as the container exists
- Object death: Object is destroyed when the container is destroyed
- One-sentence summary: Singleton Bean lifecycle is the same as the container
Multi-instance mode: prototype
- Object initialization: When using the object, create a new object instance
- Object survival: Object lives as long as it is in use
- Object death: When the object is not used for a long time, it is recycled by Java’s garbage collector
- One-sentence summary: For prototype Beans, the Spring framework only负责创建,不负责销毁
Bean Tag Attributes
In XML-based IoC configuration, the Bean tag is the basic tag, representing an object in the IoC container. In other words, if an object is managed by Spring, it needs to be configured using this tag in XML. Bean tag attributes:
- id attribute: Used to give Bean a unique identifier. Within a tag, the identifier must be unique
- class attribute: Used to specify the fully qualified class name for creating the Bean object
- name attribute: Used to give Bean one or more names, separated by spaces
- factory-bean attribute: Used to specify the unique identifier of the factory Bean that creates the current Bean. When this attribute is specified, the class attribute becomes invalid
- factory-method attribute: Used to specify the factory method that creates the current Bean object. If factory-bean is configured, the class attribute becomes invalid. If used with the class attribute, the method must be static
- scope attribute: Used to specify the Bean object’s scope. Usually singleton. Can configure prototype when needed
- init-method attribute: Used to specify the Bean object’s initialization method. This method is called after the Bean object is assembled. Must be a no-arg method
- destory-method attribute: Used to specify the Bean object’s destruction method. This method executes before Bean destruction. Only works when scope is singleton
DI Injection XML Configuration
Classified by Injection Method
- Constructor injection: As the name suggests, uses parameterized constructor to assign data to class members
- Set method injection: Uses class member’s set method to implement data injection (most used)
Classified by Injected Data Type
- Basic types and String: Injected data is basic type or String type data
- Other Bean types: If the injected data is object type, it is called other Bean because this object needs to exist in the IoC container. For the current Bean, it is another Bean
- Complex types (Collection types): Injected data is one of Array, List, Set, Map, Properties types
Constructor Injection
Constructor injection, as the name suggests, uses constructor to assign values to class members. The requirement is that the number of constructor parameters must match the configured number, and data types must match.
Also note that when there is no no-arg constructor, constructor injection must be provided, otherwise the Spring framework will report an error.
When using constructor injection, the involved tag is constructor-arg, which has the following attributes:
- name: Used to assign value to the parameter with the specified name in the constructor
- index: Used to assign value to the parameter at the specified index position in the constructor
- value: Used to specify data of basic type or String type
- ref: Used to specify data of other Bean type. Write the unique identifier of the other Bean
<!-- Configure Bean -->
<bean id="wzkTransferService" class="wzk.service.impl.WzkTransferServiceImpl">
<constructor-arg name="wzkAccountDao" ref="wzkAccountDao"></constructor-arg>
</bean>
Set Method Injection
Uses the provided setXxxx method to implement injection through assignment.
<bean id="wzkAccountDao" class="wzk.dao.WzkAccountDao"></bean>
<!-- Configure Bean -->
<bean id="wzkTransferService" class="wzk.service.impl.WzkTransferServiceImpl">
<!-- Constructor injection -->
<!-- <constructor-arg name="wzkAccountDao" ref="wzkAccountDao"></constructor-arg> -->
<!-- Set method injection -->
<!-- ref references object -->
<property name="wzkAccountDao" ref="wzkAccountDao"></property>
<!-- value is the injected value -->
<property name="name" value="wzkicu"></property>
</bean>
When using set method injection, the property tag is needed. This tag’s attributes:
- name: Specifies the set method name to call during injection
- value: Specifies the injected data. Supports basic types and String types
- ref: Specifies the injected data. Supports other Bean types. Write the unique identifier of the other Bean
For complex data type injection, it refers to collection type data. Collections are divided into two categories:
- List structure
- Map structure
For List structure collection data injection, the three tags array, list, set are interchangeable. Additionally, for value injection, the value tag can write values directly inside, or use the bean tag to configure an object, or use the ref tag to reference a configured Bean’s unique identifier.
For Map structure collection data injection, the map tag uses the entry sub-tag to implement data injection. The entry tag can use key and value attributes to specify data stored in the map. Use the value-ref attribute to specify a reference to a configured Bean. The entry tag can also use the ref tag, but cannot use the bean tag. The property tag cannot use ref or bean tag to reference objects.