Starting from "0", self-study the spring framework - springIOC foundation

catalogue

1, Load profile

1.1 relative position

2. Absolute position

3. Multiple configuration files - transfer multiple file names

4. Multiple configuration files - the total configuration file is import ed

2, Instantiation of bean objects

2.1 constructor instantiation

2. Static factory instantiation

3. Example chemical plant

3, IOC injection

1. set mode (manual)

2. Constructor injection (manual)

3. Factory injection (manual)

4. Annotation @ Resource injection (automatic)

5. Annotation @ autowritten injection (automatic)

IV. IOC scanner

1, Load profile

1.1 relative position

1.1.1 preparation: create a maven project and modify POM XML environment, adding dependencies

<dependency>
  <groupId>org.springframework</groupId>
  <artifactId>spring-context</artifactId>
  <version>5.2.4.RELEASE</version>
</dependency>

Create two new test methods, a resource directory resources, and a new spring XML configuration file and copy the paste specification from the official website.

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
        https://www.springframework.org/schema/beans/spring-beans.xsd">

        <bean id="userService" class="com.wode.service.UserService" />
        <bean id="userDao" class="com.wode.dao.UserDao" />


</beans>

1.1.2 relative position

ApplicationContext ac = new ClassPathXmlApplicationContext("spring.xml");

2. Absolute position

1.2.1. Get the absolute location of xml configuration file

1.2.2. Use the FileSystemXmlApplicationContext class to receive the absolute address

ApplicationContext ac = new FileSystemXmlApplicationContext("D:\\IDEA\\spring11\\src\\main\\resources\\spring.xml");

3. Multiple configuration files - transfer multiple file names

1.3.1 create two xml configuration files under the resource directory and add specifications and bean tags

1.3.2. Still use ClassPathXmlApplicationContext, but add multiple configuration files separated by commas

Disadvantages: manual input is required for each use, which is prone to errors, so it is recommended to use a general configuration file and import

ApplicationContext ac = new ClassPathXmlApplicationContext("dao.xml","service.xml");

4. Multiple configuration files - the total configuration file is import ed

1.4.1 create a new general configuration file, add the import tag, and import two configuration files

1.4.2. Use relative path to load configuration file

ApplicationContext ac = new ClassPathXmlApplicationContext("sum.xml");

2, Instantiation of bean objects

2.1 constructor instantiation

(the preliminary preparations are the same as "1.1.1".)

2.1.1 constructor instantiation is to create objects using the default method or the construction method defined in the class

2.1.2 test

Note: the constructor method must have a default constructor, that is, an empty constructor. If there is no custom constructor, you can not write it (default), but if there is a custom constructor, you must write an empty constructor.

2. Static factory instantiation

2.2.1 create a factory package, create a static factory class in the package, and write static methods to create UserService objects

2.2.2. Write bean Tags

Although it is a created UserService object, it is implemented through a static factory, so the class path should be modified to the path of the static factory, and the method used should also be filled in the bean tag through "factory method"

2.2.3 test

3. Example chemical plant

2.3.1. Write common methods in the factory ("StaticFactory" name has not been changed, but it is not a static factory)

2.3.2. Write bean Tags

As we all know, static methods can be called directly, but non static methods need to first create a new object and call it by adding "." to the object. Therefore, first instance the chemical plant object, and then use the factory object to complete the instantiation when instantiating the UserService object

2.3.2 test

3, IOC injection

(the preparation is the same as 1.1.1...)

1. set mode (manual)

3.1.1. Write the set method in UserService

3.1.2. Write bean tag injection

1. Use the property tag to inject

2.name: attribute name

3.ref: id of bean object

4.value: value

2. Constructor injection (manual)

3.2.1. Edit constructors and tests

3.2.2. Write bean Tags

1. Inject with constructor Arg tag

2.name: attribute name

3.ref: id of bean object

4.value: value

5.index: subscript, starting from 0

3. Factory injection (manual)

3.3.1 static factory injection

3.3.1.1. Create a factory package, a static factory class, and a static method

3.3.1.2 create construction method (or set method) in UserService

3.3.1.3. Edit the bean tag, instantiate the userDao object using the static factory, and inject (or property tag)

3.3.2 example chemical plant injection

3.3.2.1. Create a common method

3.3.2.2. Create set method

3.3.2.3. Use the instance factory to instantiate the UserDao object and inject

4. Annotation @ Resource injection (automatic)

(reset to 1.1.1...)

3.4.1,spring. Create environment in XML configuration file (underlined part)

<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
        https://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context.xsd">

3.4.2 turn on automatic injection

<context:annotation-config></context:annotation-config>

3.4.3. Edit UserService and add notes

Annotation finds the object corresponding to the bean tag in three ways - name (id), attribute name (id), and class name (class)

1,name:

@Resource(name="userDao")

When using name, the attribute name and class name are not found. Where name = id of bean tag

2. Property name:

@Resource

private UserDao userDao;

When there is no name, userDao (attribute name) is consistent with the id in the bean tag, and the class name is not searched

3. Class name:

@Resource

private UserDao us;

There is no name, and the attribute name is inconsistent with the id in the bean tag. Find the corresponding class through the class in the bean

You can use set

@Resource
private UserDao userDao;
public void setUserDao(UserDao userDao) {
    this.userDao = userDao;
}

or

private UserDao userDao;
@Resource
public void setUserDao(UserDao userDao) {
    this.userDao = userDao;
}

3.4.4 test

5. Annotation @ autowritten injection (automatic)

3.5.1 the environment is the same as above

3.5.2 use autowritten annotation and test

@Instead of finding the corresponding id through the attribute name, the autowritten annotation directly uses the class method (type = class)

@ Autowired+@Qualifier(value = "") and @ Resource(name = "") have the same effect, and the value must be consistent with the id in the bean tag

IV. IOC scanner

(reset to 1.1.1...)

4.1,spring. Create environment in XML configuration file (underlined part)

<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
        https://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context.xsd">

4.2. Turn on the scanning range of the scanner, do not write bean tags, and automatically generate them through the scanner

<context:component-scan base-package="com.wode"/>

4.3. Automatically generate bean s through tags. Note injection can be done without turning on automatic injection

dao layer

@Repository

service layer

@Service

controller layer

@Controller

Arbitrary layer

@Component

The four annotations have the same function and represent different levels by different names. The default id is type initial lowercase

4.4 test

Keywords: Java Maven Spring IDEA

Added by blen on Sat, 15 Jan 2022 05:56:35 +0200