Hello, today I would like to share the Spring framework with you. Please take out your notebook and write it down.

Spring Framework (IOC,DI,AOP)

The Spring framework is a member of the Java open source project, which can improve the development efficiency of the project. There are several modules in the framework, including:

Core container

Spring-AOP

Spring Data Access

The Web module

Send a message

Unit testing

The key things to understand are IOC (Inversion of control) and DI (dependency injection) in the core container and Spring-AOP.

First of all, we need to build the configuration file of the entire framework. After creating a new Maven project, we need to add the following dependencies in the POM.xml file

Also use the following configuration to meet all of our requirements when configuring the spring.xml file

The < beans XMLNS = “www.springframework.org/schema/bean…”

xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xmlns:aop="http://www.springframework.org/schema/aop" 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 http://www.springframework.org/schema/aop https://www.springframework.org/schema/aop/spring-aop.xsd"> <! -- Add the content to be used later -->Copy the code

1. Instantiate the Spring IOC container Bean object

Build the UserDao Javabean class, the StaticFactory class, and the instance factory class UserFactory

Define the UserDao class:

public class UserDao {

private String uname; private String pwd; // Void constructor, parameter constructor, get and set method, toString method is not writtenCopy the code

}

Define a static factory class:

import com.gao.dao.UserDao;

public class StaticFactory {

public static UserDao getUserDao(){

    return new UserDao();

}
Copy the code

}

Define the instance chemical plant class:

import com.gao.dao.UserDao;

public class UserFactory {

public UserDao getUserDao(){

    return new UserDao();

}
Copy the code

}

There are three ways to instantiate an object:

Constructor instantiation:

Static factory instantiation:

Example Chemical plant instantiation:

Test method:

@Test

public void test(){

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

UserDao userDao = (UserDao) app.getBean("userDao");

System.out.println(userDao);

userDao.dao();
Copy the code

}

2. Spring IOC injection

2.1 Manual Injection

Spring supports four types of manual injection: Set injection, constructor injection, static factory injection, and instance-chemical injection

Define the UserDao class:

public class UserDao {

private String uname; private String pwd; // Void constructor, parameter constructor, get and set method, toString method is not writtenCopy the code

}

Define the UserService02 class:

public class UserService02 {

private UserDao userDao; // Void constructor, parameter constructor, get and set method, toString method is not writtenCopy the code

}

Define the TestDao class:

2.1.1 set injection

Set injection is recommended if a property field needs to provide a set method

If the members of the class are common objects and primitive types:

<property name="uname" value="zhangsan"></property>

<property name="pwd" value="123456"></property>
Copy the code

If the class members are Javabeans:

<property name="userDao" ref="userDao"></property>-->
Copy the code

If the members of the class are of collection type:

2.1.2 Constructor injection

Constructor injection is implemented only if a parameterized constructor is provided

If the members of the class are common objects and primitive types:

<constructor-arg index="0" value="lisi"></constructor-arg>

<constructor-arg index="1" value="123"></constructor-arg>
Copy the code

If the class members are Javabeans:

<constructor-arg index="0" ref="userDao"></constructor-arg>
Copy the code

Note: If there is a circular dependency where two classes have each other as members, use set injection

2.1.3 Static factory injection

A static factory is used to instantiate the bean object members of the class, using set injection

<property name="userDao" ref="userDao"></property>-->
Copy the code

2.1.4 Example chemical plant injection

Instantiate the bean object members of the class using the instance chemical, using set injection

<property name="userDao" ref="userDao"></property>-->
Copy the code

2.2 Automatic Injection

Define the UserDao class:

public class UserDao {

private String uname; private String pwd; // Void constructor, parameter constructor, get and set method, toString method is not writtenCopy the code

}

Define the UserService class:

public class UserService {

private UserDao userDao; // Void constructor, parameter constructor, get and set method, toString method is not writtenCopy the code

}

Enable automatic injection:

context:annotation-config/–>

–>

–>

2.2.1 @ Resource annotation

The @Resource annotation is recommended

By default, the corresponding bean object is found based on the property field name (the name of the property field is equal to the id attribute value of the bean label)

If the property field name is not found, it is looked up by type (Class type)

Property may or may not provide a set method

Annotations can be declared on properties or set methods

You can set the name attribute, whose value must be the same as the ID attribute of the bean label. If the name attribute value is set, only the bean object is looked up by the name attribute value

When an interface is injected, it is instantiated normally if the interface has only one implementation; If there are multiple implementations of the interface, you need to specify the bean object to be instantiated using the Name attribute

Usage:

public class UserService {

@resource (name = "userDao") private userDao userDao; // Void constructor, parameter constructor, get and set method, toString method is not writtenCopy the code

}

2.2.2 the @autowired annotation

By default, bean objects are found by type (Class type), regardless of the name of the property field

Property may or may not provide a set method

Annotations can be declared on properties or set methods

You can use it in combination with @Qualifier to find bean objects using the value attribute value (value must be set and match the id attribute value of the bean tag).

Usage:

public class UserService {

@autowired @Qualifier(value="userDao") private userDao userDao; // Void constructor, parameter constructor, get and set method, toString method is not writtenCopy the code

}

2.3 IOC scanner

After using IOC scanner, no longer need to set bean label, set automatic scanning range:

<context:component-scan base-package=”com.gao”/>

There are several kinds of annotations:

@repository (Dao layer)

@service (Service layer)

@controller (Controller layer)

@Component (arbitrary layer)

3. Spring AOP

AOP is section-oriented programming, used to intercept the function of the whole side, mainly used for logging, performance statistics, security control, transaction processing and other aspects, to achieve the reuse of common functions.

AOP implementation: Dynamic Proxy (JDK+CGLIB)

3.1 Basic concepts of AOP

Joinpoint: Every method intercepted in Spring

Pointcut: Matching rules define which methods to intercept and which methods to process

Advice: What to do after intercepting a method, including:

Pre-notification: before Notifies a method before execution

Return notification: An afterReturn notification after the afterReturn method completes its normal return

An exception is thrown to notify afterThrow

Final notification: after This notification is executed regardless of whether the method has an exception

Circular notification: Around can complete custom behavior before and after method calls, and can choose whether to continue with join points or simply return their own return values or throw an exception to end execution

Aspect: A combination of pointcuts and advice that, like a class, is an abstraction of crosscutting concerns

Target: The Target object to be proxied

Weave: The process of applying facets to target objects and generating proxy objects

Introduction: The process of dynamically adding methods or fields to a class at runtime without modifying the source code

3.2 Annotations implement AOP

Introduction to AOP pointcut expressions:

Execution of any public method: Execution (public *(…) )

Execute an arbitrary set method: Execution (* set*(…)) )

Execute any method of any class in the com.xxxx.service package: Execution (* com.xxxx.service.. (…). )

Execution of any method of the com.xxxx.service package and any class in its subpackage: Execution (* com.xxxx.service… . (…). )

Note: The first * in the expression is a method modifier

Configuration file:

aop:aspectj-autoproxy/

Method 1 for defining a section:

Method two for defining the cut (combining the cut point with the advice) :

3.3 XML implements AOP

Define section:

Configure the XML file:

Well, that’s all for today’s article, hoping to help those of you who are confused in front of the screen