Static proxy of Spring AOP and Spring AOP
I. SpringAOP:
Except AOP:Aspect Oriented Programming is designed for cross-section Programming. It separates core services from non-core services, allowing the core class to only serve as the core business, and the proxy class to only serve as the non-core business.
BytesPurpose:
(1,Separate core and non-core services
(2,Turn multiple non-core business processing (points) into one aspect (the previous non-core business code needs to be written multiple times to be written only once .)
Requirement: it is reasonable to use AOP only when multiple services in the core class need to be processed. If only one service needs to be processed, no need to use AOP.
Ii. Static Proxy:
1,You need to know which class is the core class (the proxy class) and what method is there.
2,Non-core code needs to be repeatedly written multiple times, and the code structure is bloated, forming code redundancy.
3,Non-core classes (proxies) need to implement interfaces implemented by core classes (proxies), that is, they need to implement common interfaces, but the interfaces implemented by core classes (proxies) prevail.
3. Dynamic Proxy:
1,You do not need to know what the core class (the proxy class) is.
2,Non-core classes (proxies) need to implement the InvocationHandler interface.
Iv. Static proxy example:
1. business introduction:
Assume that senior 3 students are the core class and their families are agents. Students need to do the following core services: studyAtHome and studyAtHome. The non-core services that family members need to do are: prepare meals (buy, wash, fry, and cook ). Prepare supplements (purchase, optimize, and add condiments ).
2. Specific Code:
(1) create an interface for senior 3 students: G3Student
Package aop_001;
// First define an interface for high school students to implement, // declare a method for studying at home and learning at school in this interface
Public interface G3Student {public void studyAtHome (String core); public void studyAtSchool (String core );}
(2) create a high school student implementation class: G3Student_boy, and this class implements the high school student interface (G3Student)
Package aop_001; // defines a class (target object, core object, and target object) for senior 3 students, and implements interfaces for senior 3 students. Public class G3Student_boy implements G3Student {// The first business process of senior 3 boys: studying public void studyAtHome (String core ){
// Learn the core tasks at home. System. out. println (core + "learn at HOME");} // second business process of senior 3 boys: public void studyAtSchool (String core) {// core things are learned at home. System. out. println (core + "Learning in SCHOOOL ");}}
(3) create a proxy class for senior 3 students: G3Student_proxy, andImplemented interfaces for senior 3 students: G3Student. Red font needs to be understood
Package aop_001;/** the proxy object (G3Student_proxy) aims to help the core object (G3Student_boy/G3Student_girl) to do non-core tasks. * However, the proxy object (G3Student_proxy) must implement a common interface with the core object (G3Student_boy/G3Student_girl. */Public class G3Student_proxy implements G3Student {// define a high school student interface as an attribute, the purpose is to call the core services required by senior 3 boys or senior 3 girls after handling what the proxy needs to do, // but not the proxy to do these core tasks, only call them. Private G3Student G3S; // The constructor used to create a proxy is the constructor of the high school student interface. If the passed string is boy, an instance of a high school boy is constructed, if the input parameter is girl, a high school girl instance is constructed. Public G3Student_proxy (String sex) {if ("boy ". equals (sex) {G3S = new G3Student_boy ();} else if ("girl ". equals (sex) {G3S = new G3Student_girl ();}}
Public void studyAtHome (String core) {// This is the process that the agent (G3Student_proxy) needs to prepare meals: System. out. println ("AGENT: food preparation"); System. out. println ("Agent: Prepare for dish washing"); System. out. println ("AGENT: Cooking"); System. out. println ("Agent: Prepare for cooking"); System. out. println ("-----------------"); // call the method G3S of the specific instance by defining attributes and creating the corresponding instance by calling the constructor with parameters of G3Student_proxy. studyAtHome (core );
// This is the process that the agent (G3Student_proxy) needs to do to prepare the makeup: System. out. println ("-----------------"); System. out. println ("AGENT: purchasing supplements"); System. out. println ("AGENT: optimize parts"); System. out. println ("Agent: Add seasoning"); System. out. println ();} public void studyAtSchool (String core) {// This is the process for the agent to prepare (G3Student_proxy) Meals: System. out. println ("AGENT: food preparation"); System. out. println ("Agent: Prepare for dish washing"); System. out. println ("AGENT: Cooking"); System. out. println ("Agent: Prepare for cooking"); System. out. println ("-----------------"); // call the method G3S of the specific instance by defining attributes and creating the corresponding instance by calling the constructor with parameters of G3Student_proxy. studyAtSchool (core );
System. out. println ("-----------------"); // This is the process for the agent (G3Student_proxy) to prepare the makeup: System. out. println ("AGENT: purchasing supplements"); System. out. println ("AGENT: optimize parts"); System. out. println ("Agent: Add seasoning"); System. out. println ();}}
(4) conduct a Test: Define a Test class
Package aop_001; public class Test {public static void main (String [] args) {// create a proxy object, and input the corresponding parameters to construct the specific instance G3Student B = new G3Student_proxy ("boy "); // execute related methods through this proxy object (the proxy object and the core object have a common interface) B. studyAtHome ("James"); B. studyAtSchool ("James ");}}
Test results:
3. Summary:
In the preceding example, we can simulate a static proxy instance. However, we find that the agent is doing more things because of the large number of core object services, in addition, these agents do the same thing without changing:
In addition, if we want to modify a process in these processes, we will find that there are multiple changes and the code is the same, so this problem can be easily solved by using dynamic proxy.
Thank you!