spring learning
1, spring configuration file
1.1 basic configuration of bean tag
-
Basic properties
- id: the unique identifier of the Bean instance in the Spring container;
- class: the fully qualified name of the Bean
for instance:
<bean id="userDao" class="com.mec.dao.impl.UserDaoImpl"></bean>
1.2 scope of bean tag
Value range | explain |
---|---|
singleton | Default, singleton |
prototype | Multiple cases |
request | In the WEB project, Spring creates a Bean object and stores the object in the request field; |
session | In the WEB project, Spring creates a Bean object and stores the object in the session domain |
global session | In the WEB project, the application is in the Portlet environment. If there is no such environment, the global session is equivalent to the session; |
- When the value of scop is singleton:
- Number of instantiated beans: 1;
- Bean instantiation timing: instantiate the configured bean instance when the Spring core file is loaded;
- Bean lifecycle:
- Object creation: when the application loads and creates a container, the object is created;
- Object running: as long as the container is, the object will always be alive;
- Object destruction: when the application is unloaded and the container is destroyed, the object is destroyed;
- When the value of scop is prototy pe:
- Number of instantiations of beans: multiple;
- Instantiation timing of Bean: instantiate Bean when calling getBean() method;
- Bean lifecycle:
- Object creation: create a new object instance when using an object;
- Object running: as long as the object is in use, it will always be alive;
- Object destruction: when an object is not used for a long time, it will be recycled by the java garbage collection mechanism;
1.3 Bean lifecycle configuration
- Init method: Specifies the name of the initialization method in the class;
- Destroy method: Specifies the name of the method destroyed in the class;
1.4 three methods of bean instantiation
-
The parameterless construction method is instantiated, as shown in the following example;
<bean id="userDao" class="com.mec.dao.impl.UserDaoImpl" scope="singleton"></bean>
-
Factory static method instantiation, as shown in the following example;
<bean id="userDao" class="com.mec.dao.factory.StaticFactory" factory-method="getUserDao" ></bean>
-
Factory instance method instantiation, as shown in the following example;
<!-- By creating a factory, and then calling a method through the factory object Bean object--> <bean id="factory" class="com.mec.dao.factory.DynamicFactory"></bean> <bean id="userDao" factory-bean="factory" factory-method="getUserDao"></bean>
1.5 dependency injection analysis of bean
-
At present, both UserService instances and UserDao instances exist in the Spring container. The current practice is to obtain UserService instances and UserDao instances outside the container, and then combine them in the program;
-
Because both UserService and UserDao are in the Spring container, and the final program directly uses UserService, UserDao can be set inside UserService in the Spring container;
1.5. 1 dependency injection
- Dependency injection: it is the concrete implementation of IOC, the core technology of Spring framework.
- When writing the program, the creation of objects is handed over to Spring by controlling flipping, but there can be no dependency in the code. IOC decoupling only reduces their dependencies, but it will not eliminate them.
1.5. 2. Dependency injection method of bean
-
set method injection
-
Mode 1:
//userService depends on userDao, so there must be userDao; <bean id="userDao" class="com.mec.dao.impl.UserDaoImpl"></bean> //Since userDao is a reference class, you have to use ref; <bean id="userService" class="com.mec.dao.service.impl.UserServiceImpl"> //Note the name attribute. Its value is consistent with the variable name <property name="userDao" ref="userDao"></property> </bean>
-
Mode 2:
-
P namespace injection is also set method injection in essence, but it is more convenient than the above set method injection, which is mainly reflected in the configuration file, as follows:
-
First, you need to introduce the P namespace (add it in the outermost beans)
xmlns: p="http://www.springframework.org/schema/p"
-
Secondly, the injection method needs to be modified
//Because userDao is a reference class, the ref class is used <bean id="userService" class="com.mec.service.impl.UserServiceImpl" p:userDao-ref="userDao"/>
-
-
-
Construction method injection
<bean id="userDao" class="com.mec.dao.impl.UserDaoImpl"></bean> //When the parameters of the construction method have cyclic dependencies, the < constructor Arg > tag needs to be used, <bean id="userService" class="com.mec.dao.service.impl.UserServiceImpl" > //Note the name attribute. Its value is consistent with the variable name <constructor-arg name="userDao1" ref="userDao"></constructor-arg> </bean>
1.5. 3. Data type of bean dependency injection
-
All the above are injected reference beans, and common data types and collections can also be referenced;
-
Three types of data injected
-
Common data type
//userService depends on userDao, so there must be userDao; <bean id="userService" class="com.mec.dao.service.impl.UserServiceImpl" > //Note the name attribute. Its value is consistent with the variable name <constructor-arg name="userDao1" ref="userDao"></constructor-arg> </bean> //This userDao is for the above services and injects the object variables of userDao through set injection; Since both variables are basic types, use value <bean id="userDao" class="com.mec.dao.impl.UserDaoImpl"> //Note the name attribute. Its value is consistent with the variable name <property name="stringName" value="zjh"></property> <property name="age" value="22"></property> </bean>
-
Reference data type
-
Collection data type
<bean id="userDao" class="com.mec.dao.impl.UserDaoImpl"> //The generic type is String <property name="list"> <list> <value>aaa</value> <value>bbb</value> <value>ccc</value> </list> </property> //For the injection of Map set, the key value pair is < integer, userdao > <property name="map"> <map> //Note that the map value is a reference type, so use ref; <entry key="u1" value-ref="user1"/> <entry key="u2" value-ref="user2"/> </map> </property> //Injection of the properties collection, <property name="properties"> <props> <prop key="p1">ppp1</prop> <prop key="p2">ppp2</prop> <prop key="p3">ppp3</prop> </props> </property> </bean> //These two are for map; <bean id="user1" class="com.mec.dao.domain.User"> <property name="name" value="zjh"/> <property name="age" value="12"/> </bean> <bean id="user2" class="com.mec.dao.domain.User"> <property name="name" value="ljx"/> <property name="age" value="16"/> </bean>
-
1.5. 4. Introduce other configuration files (sub module development)
-
In the actual development, there are many Spring configurations, which leads to the complexity and large volume of Spring configuration. Therefore, some configurations can be disassembled into other configuration files, and loaded in the Spring main configuration file through the MPO tag;
-
Reference format:
<import resource="applicaytionContext-XXX.xml"/>
1.5. 5 key spring configurations (knowledge summary)
//The attributes of many tags are the same. I haven't summarized them one by one. You can accumulate them when using them; <bean>label: id Properties: in container Bean Unique identifier of the instance. Duplicate is not allowed; class Attributes: to instantiate Bean Fully qualified name of; scope Properties: Bean Scope of action, commonly used is Singletion(Default) and prototype <property>Labels: attribute injection name Attribute: attribute name, consistent with variable name; value Attribute: used when the injected variable type is a common attribute value; ref Attribute: injected object reference value; <list>label: <value>label: <map>label <entry>label: key Attribute: the attribute where the key is stored; <property>label: properties aggregate name Attribute: the name of the stored variable; <prop>label: key Properties: store keys <constructor-arg>Label: injection with construction method; <import>Labels: importing additional Spring Sub documents of the;