Introduction to Spring 3 - Spring's Focus on AOP

AOP, the core concept of Spring, is also the core concept of Spring framework.
It uses a technology called "crosscutting" to decompose encapsulated objects and encapsulate common behaviors that affect multiple classes into a reusable module, which is named "Aspect", or aspect. The so-called "aspect" simply means that those logic or responsibility which are not related to business but are called by business modules are encapsulated. It is easy to reduce the repetitive code of the system, reduce the coupling between modules, and be conducive to future operability and maintainability.
What is aspect and what is a common module? Let's say less about the concept and see what AOP is through an example.
Demand:
Now we have a table User, and then we need to add and delete the User table in the program.
Requirements: Both add and delete operations must open transactions, and submit transactions after completion of operations.
User.java

package com.dn.aop.one;

public class User {
	private int uid;
	private String uname;
	
	public int getUid() {
		return uid;
	}
	public void setUid(int uid) {
		this.uid = uid;
	}
	public String getUname() {
		return uname;
	}
	public void setUname(String uname) {
		this.uname = uname;
	}
}

Solution 1: Using static proxies
Step 1: Create the UserService interface

package com.dn.aop.one;

public interface UserService {
	//Add user
	public void addUser(User user);
	//Delete user
	public void deleteUser(int uid);
}

Step 2: Create the implementation class of UserService

package com.dn.aop.one;

public class UserServiceImpl implements UserService {

	@Override
	public void addUser(User user) {
		System.out.println("increase User");
	}
	@Override
	public void deleteUser(int uid) {
		System.out.println("delete User");
	}
}

Step 3: Create the transaction class MyTransaction

package com.dn.aop.one;
//Create transaction classes
public class MyTransaction {
	//Open a transaction
	public void before(){
		System.out.println("Open a transaction");
	}
	//Submission transaction
	public void after(){
		System.out.println("Submission transaction");
	}
}

Step 4: Create the proxy class ProxyUser.java and test it

package com.dn.aop.one;

import org.testng.annotations.Test;

public class ProxyUser implements UserService {
	//Real class
	private UserService userService;
	//Transaction class
	private MyTransaction transaction;
	//Instantiation using constructors
	public ProxyUser(UserService userService, MyTransaction transaction){
		this.userService = userService;
		this.transaction = transaction;
	}
	@Override
	public void addUser(User user) {
		transaction.before();
		userService.addUser(user);
		transaction.after();

	}

	@Override
	public void deleteUser(int uid) {
		transaction.before();
		userService.deleteUser(uid);
		transaction.after();
	}
	
	public static void testOne(){
		MyTransaction transaction = new MyTransaction();
		UserService userService = new UserServiceImpl();
		//Generating static proxy objects
		ProxyUser proxy = new ProxyUser(userService, transaction);
		proxy.addUser(null);
		proxy.deleteUser(0);
	}
	public static void main(String[] args) {
		ProxyUser.testOne();
	}

}

This is a very basic static proxy. Business class UserService Impl only needs to pay attention to business logic itself to ensure the reusability of business. This is also the advantage of proxy class. There is nothing to say. We mainly talk about the shortcomings of this writing:

(1) An interface of proxy object only serves one type of object. If there are many methods to proxy, it is necessary to proxy for each method. Static proxy is not competent when the program is a little larger.

(2) If the interface adds a method, such as UserService adds a modified updateUser() method, then all proxy classes need to implement this method in addition to all implementation classes. Increased complexity of code maintenance.

Keywords: Spring Java less

Added by XxDeadmanxX on Wed, 28 Aug 2019 14:01:24 +0300