[SSM hits the big factory directly] Chapter 2: Detailed explanation of Spring configuration file

content

3. Spring configuration file

3.1 Bean tag basic configuration

3.2 Bean tag scope configuration

3.3 Bean life cycle configuration

3.4 Three ways of Bean instantiation

3.5 Getting Started with Bean Dependency Injection

3.6 Bean Dependency Injection Analysis

3.7 Bean's dependency injection concept

3.8 Bean dependency injection method

3.9 Data Types of Bean Dependency Injection

3.10 Introducing other configuration files (development by modules)

Knowledge points


3. Spring configuration file

3.1 Bean tag basic configuration

The configuration object is created by Spring .

By default, it calls the no-argument constructor in the class . If there is no no-argument constructor, it cannot be created successfully.

Basic properties:

  1. id : the unique identifier of the bean instance in the Spring container
  2. class : the fully qualified name of the bean

3.2 Bean tag scope configuration

scope: refers to the scope of the object, the values ​​are as follows:

Ranges

illustrate

singleton

default, singleton

prototype

Practical

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, it is applied in the Portlet environment. If there is no Portlet environment, the globalSession is equivalent to the session.

1) When the value of scope is singleton

Number of instantiations of beans: 1

Bean instantiation timing: When the Spring core file is loaded, instantiate the configured Bean instance

Bean life cycle:

  1. Object Creation: Objects are created when the application loads and the container is created
  2. Object running: as long as the container exists, the object is alive
  3. Object destruction: When the application is uninstalled and the container is destroyed, the object is destroyed

2) When the value of scope is prototype

Number of instantiations of beans: multiple

Bean instantiation timing: Bean is instantiated when the getBean() method is called

  1. Object creation: when using an object, create a new object instance
  2. Object running: as long as the object is in use, it is alive
  3. Object Destruction: When an object is not used for a long time, it is reclaimed by Java's garbage collector

3.3 Bean life cycle configuration

  1. init-method : specifies the name of the initialization method in the class
  2. destroy-method : the name of the destroy method in the specified class

3.4 Three ways of Bean instantiation

1 ) Instantiate using the no-argument constructor

It will create a class object according to the default no-argument constructor. If there is no default no-argument constructor in the bean, the creation will fail

<bean id="userDao" class="com.project.dao.impl.UserDaoImpl"/>

2) Factory static method instantiation

The static method of the factory returns the Bean instance

Create a factory class

public class StaticFactoryBean {

public static UserDao createUserDao(){

return new UserDaoImpl();

}

}
<bean id="userDao" class="com.project.factory.StaticFactoryBean" 
factory-method="createUserDao" />

3 ) The factory the non-static method of the factory to return the Bean instance

public class DynamicFactoryBean { 
    public UserDao createUserDao(){ 
        return new UserDaoImpl();
    }
}
<bean id="factoryBean" class="com.project.factory.DynamicFactoryBean"/>
<bean id="userDao" factory-bean="factoryBean" factory-method="createUserDao"/>

3.5 Getting Started with Bean Dependency Injection

① Create UserService, UserService internally calls UserDao's save() method

public class UserServiceImpl implements UserService {

@Override public void save() {

ApplicationContext applicationContext = new

ClassPathXmlApplicationContext("applicationContext.xml"); UserDao userDao = (UserDao) applicationContext.getBean("userDao");

userDao.save();

}

}

② Give Spring the right to create UserServiceImpl

<bean id="userService" class="com.project.service.impl.UserServiceImpl"/>

③ Get UserService from Spring container for operation

<bean id="userService" class="com.project.service.impl.UserServiceImpl"/>

3.6 Bean Dependency Injection Analysis

At present , both the UserService instance and the UserDao instance exist in the Spring container . The current practice is to obtain the UserService instance and the UserDao instance outside the container , and then combine them in the program.

Because UserService and UserDao are both in the Spring container, and the final program uses UserService directly, you can set UserDao inside UserService in the Spring container.

3.7 Bean's dependency injection concept

Dependency Injection ( Dependency Injection ): It is a concrete implementation of the core IOC of the Spring framework.

When writing a program, through inversion of control, the creation of objects is handed over to Spring, but it is impossible to have no dependencies in the code.

IOC decoupling only reduces their dependencies, but does not eliminate them. For example: the business layer will still call the methods of the persistence layer.

The dependency between the business layer and the persistence layer is maintained by Spring after using Spring.

Simply put, it is to wait for the framework to pass the persistence layer object into the business layer, instead of obtaining it ourselves.

3.8 Bean dependency injection method

How to inject UserDao into UserService?

1. Construction method

2.set method

1) set method injection

Add setUserDao method in UserServiceImpl

public class UserServiceImpl implements UserService { private UserDao userDao;

public void setUserDao(UserDao userDao) {

this.userDao = userDao;

}

@Override

public void save() {

userDao.save();

}

}

Configure the Spring container to call the set method for injection

<bean id="userDao" class="com.project.dao.impl.UserDaoImpl"/>

<bean id="userService" class="com.project.service.impl.UserServiceImpl">

<property name="userDao" ref="userDao"/>

</bean>

The essence of P namespace injection is also set method injection, but it is more convenient than the above set method injection, which is mainly reflected in the configuration file, as follows: First, the P namespace needs to be introduced:

xmlns:p="http://www.springframework.org/schema/p"

Second, the injection method needs to be modified

<bean id="userService" class="com.project.service.impl.UserServiceImpl" p:userDaoref="userDao"/>

2) Constructor injection creates a parameterized structure

public class UserServiceImpl implements UserService {

@Override public void save() {

ApplicationContext applicationContext = new

ClassPathXmlApplicationContext("applicationContext.xml"); UserDao userDao = (UserDao) applicationContext.getBean("userDao");

userDao.save();

}

}

Configure the Spring container to inject when calling the parameterized construction

<bean id="userDao" class="com.project.dao.impl.UserDaoImpl"/>

<bean id="userService" class="com.project.service.impl.UserServiceImpl">

<constructor-arg name="userDao" ref="userDao"></constructor-arg>

</bean>

3.9 Data Types of Bean Dependency Injection

The above operations are all injected reference beans. The reference of the object can be injected. Common data types, collections, etc. can be injected in the container.

Three data types for injecting data

  1. common data type
  2. reference data type collection data type

The reference data type will not be repeated here. The previous operations are all injected into the reference of the UserDao object. The following will take the set method injection as an example to demonstrate the injection of common data types and collection data types.

1) Injection of common data types

public class UserDaoImpl implements UserDao { private String company; private int age;

public void setCompany(String company) {

this.company = company;

}

public void setAge(int age) {

this.age = age;

} public void save() {

System.out.println(company+"==="+age);

System.out.println("UserDao save method running....");

}

}
<bean id="userDao" class="com.project.dao.impl.UserDaoImpl">

<property name="company" value="小明"></property>

<property name="age" value="15"></property>

</bean>

2) Injection of collection data type ( List<String> )

public class UserDaoImpl implements UserDao { private List<String> strList;

public void setStrList(List<String> strList) {

this.strList = strList;

}

public void save() {

System.out.println(strList);

System.out.println("UserDao save method running....");

}

}

<bean id="userDao" class="com.project.dao.impl.UserDaoImpl">

<property name="strList">

<list>

<value>aaa</value> <value>bbb</value>

<value>ccc</value>

</list>

</property>

</bean>

4) Injection of collection data type ( Map<String,User> )

public class UserDaoImpl implements UserDao { private Map<String,User> userMap;

public void setUserMap(Map<String, User> userMap) {

this.userMap = userMap;

}

public void save() {

System.out.println(userMap);

System.out.println("UserDao save method running....");

}

}

<bean id="u1" class="com.project.domain.User"/> <bean id="u2" class="com.project.domain.User"/>

<bean id="userDao" class="com.project.dao.impl.UserDaoImpl">

<property name="userMap">

<map>

<entry key="user1" value-ref="u1"/>

<entry key="user2" value-ref="u2"/>

</map>

</property>

</bean>

3.10 Introducing other configuration files (development by modules)

In actual development, Spring has a lot of configuration content, which makes Spring configuration very complicated and bulky. Therefore, part of the configuration can be disassembled into other configuration files, and the Spring main configuration file is loaded through the import tag.

<import resource="applicationContext-xxx.xml"/>

Knowledge points

Spring key configuration

<bean> tag

id attribute: the unique identifier of the Bean instance in the container, no repetition is allowed class attribute: the fully qualified name of the bean to be instantiated scope attribute: the scope of the bean, commonly used are Singleton (default) and prototype

<property> tag: property injection name property: property name value property: injected normal property value ref property: injected object reference value

<list> tag

<map> tag

<properties> tag

<constructor-arg> tag

<import> tag: import other Spring sub-files

Guess you like

Origin blog.csdn.net/qq_52360069/article/details/123555303