The outstanding advantages of Java Spring---------------AOP (Aspect-Oriented Programming) with small examples (absolutely easy to understand, suitable for beginners to learn and watch)

As we all know, the two prominent advantages of JavaSpring are IOC (Inversion of Control) and AOP (Aspect Oriented Programming).

I also only started to learn this framework in the past few days. If there is something wrong, please correct me.

Today I will talk about AOP, not much nonsense, and see the dry goods.

AOP introduction background:

Take an example word. I want to write a management system, of course, the stage of user verification is indispensable. However, if I use the AOP idea, I can directly start the system writing work after login verification without considering the problem of login verification first. After most of the work is done, in turn, in the corresponding location, add the verification function. The function of adding the verification function to the system I made is that as long as you log in to the system, you must verify your identity. Of course, we can add verification modules to the login class, but the coupling will increase. Also, if there are multiple functions If identity verification is required, it will also increase the complexity of the code and the workload of maintenance. Aspect-oriented programming ideas solve this problem very well, and you can add code wherever you want. This kind of programming idea that dynamically cuts the code into the specified method and position of the class at runtime is aspect-oriented programming. 

Generally speaking, the code fragments that we cut into the specified methods of the specified classes are called aspects, and which classes and methods we cut into are called entry points. With AOP, we can extract the code shared by several classes into a slice, and then cut into the object when needed, thereby changing its original behavior.
In this way, AOP is actually just a supplement to OOP. OOP distinguishes each class from the horizontal, while AOP adds specific code to the object vertically. With AOP, OOP becomes three-dimensional. If the time dimension is added, AOP makes OOP change from two-dimensional to three-dimensional, and from plane to three-dimensional. Technically, AOP is basically implemented through a proxy mechanism.



AOP, you must first know a few concepts. What is a slice? What is the entry point, these two concepts can be said to be the most important. ,

Aspect: The code fragment that we cut into the specified method of the specified class is called the aspect;

Entry point: Which classes and methods are entered into is called entry point, which is the location of entry, that is, the execution point;


Then, I use my own small example to show you:

Method 1: Annotated version:

The project structure is as follows:


1. First write the class service1 where the entry point is located:

package service;


public class service1 {
public void name1() {
System.out.println("Execute one;;;;;;;;;;;;;;;");
}
public void name2() {
System.out .println("Execute two;;;;;;;;;;;;;;;;");
}
public service1() { } public void name3() { System.out.println("Execute three;;;; ;;;;;;;;;;;"); } public void name4() { System.out.println("Execute four;;;;;;;;;;;;;;;;"); } public void name5() { System.out.println("Execute five;;;;;;;;;;;;;;;;"); }public void name6() { System.out.println("Execute six;; ;;;;;;;;;;;;;;"); } }
















2. Write the aspect class aspect to be inserted:

package entity;


import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;


//Set the aspect and apply the annotation method
@Aspect
public class aspect {


//Before the execution point is executed, execute the aspect method
@Before("execution(* service.service1.name1(..))") //This annotation directly links the aspect and the execution point
public void before1(){
System.out .println("Execution before execution point, before1");
}
@Before("execution(* service.service1.name2(..))") //This annotation directly links the aspect and the execution point
public void before2( ){
System.out.println("Execute before execution point, before2");
}
@Before("execution(* service.service1.name3(..))") //This annotation directly links the aspect to the execution point up
public void before3(){
System.out.println("Execute before execution point, before3");
}
@Before("execution(* service.service1.name4(..))") //This annotation directly links the aspect to the execution point
public void before4(){
System.out.println("Execute before the execution point is executed , before4");
}
@Before("execution(* service.service1.name5(..))") //This annotation directly links the aspect to the execution point
public void before5(){
System.out.println(" Execute before the execution point is executed, before5");
}
@Before("execution(* service.service1.name6(..))") //This annotation directly links the aspect and the execution point
public void before6(){
System. out.println("Execute before execution point, before6");
}
//After execution point, execute the aspect method
@After("execution(* service.service1.name1(..))")    
public void after1() {
System.out.println("Execute after execution point, after1");
}
@After("    execution(* service.service1.name2(..))")    
public void after2(){
System.out.println("Execute after execution point, after2");
} @After("execution(* service.service1.name3(..))")     public void after3(){ System .out.println("Execute after execution point, after3"); } @After("execution(* service.service1.name4(..))")     public void after4(){ System.out.println("Execute Execute after point execution, after4"); } @After("execution(* service.service1.name5(..))")     public void after5(){ System.out.println("Execute after execution point, after5" ); } @After("execution(* service.service1.name6(..))")     public void after6(){ System.out.println("Execute after execution point, after6"); }


















}


3. Test class:


package text;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;


import service.service1;
public class test {


@Test
public void test1() {
//Pass Container, inject the relationship between the two into their respective javabeans
ApplicationContext ac= new ClassPathXmlApplicationContext("applicationContext.xml");
service1 s = ac.getBean("s1",service1.class);//
s.name1(); //When this method is executed, it will automatically find the slice method with this method as the execution point (the annotation has been identified), and execute the relevant slice method according to the relevant order.
System.out.println();
s.name2();
System.out.println();
s.name3();
System.out.println();
s.name4();
System.out.println();
s.name5();
System.out.println();
s.name6();}}





4. The configuration file is as follows applicationContext.xml:


<?xml version="1.0" encoding="UTF-8"?>
<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" xmlns:util="http://www.springframework.org/schema/util"  
    xmlns:jee="http://www.springframework.org/schema/jee" xmlns:tx="http://www.springframework.org/schema/tx"
    xmlns:aop="http://www.springframework.org/schema/aop" 
    xmlns:jpa="http://www.springframework.org/schema/data/jpa" xmlns:mvc="http://www.springframework.org/schema/mvc"
    xsi:schemaLocation="
        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd
        http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.2.xsd
        http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.2.xsd
        http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd
        http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.2.xsd
        http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa-1.3.xsd
        http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd">
<bean id="s1" class="service.service1"></bean>
    <bean id="as" class="entity.aspect"></bean>
<aop:aspectj-autoproxy  proxy-target-class="true"></aop:aspectj-autoproxy>

</beans>


5. Running result:


Note, you need to add the relevant jar package, contact me if you need it.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324812505&siteId=291194637