Spring|AOP

一、AOP

AOP(Aspect Oriented Programming):面向切面编程,在OOP(Object Oriented Programming)中,关键单元模块度是类,而在AOP中单元模块度是方面。

应用场景:拦截器、声明式事物、日志等。

二、基于XML

 【示例】

package com.my;

/**
 * @Author jyy
 * @Description {}
 * @Date 2018/7/16 11:29
 */
public class Person {

    private String id;
    private String name;
    private Address address;

    public Person() {
    }

    public Person(Address address) {
        this.address = address;
    }

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Address getAddress() {
        return address;
    }

    public void setAddress(Address address) {
        this.address = address;
    }

    public void getAddressDetail() {
        System.out.println("查询用户详细地址");
    }
}
package com.my;

/**
 * @Author jyy
 * @Description {}
 * @Date 2018/8/6 11:28
 */
public class AopTest {

    public void beforeAop() {
        System.out.println("======before aop======");
    }

    public void afterAop() {
        System.out.println("======after aop======");
    }
}
package com.my;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

/**
 * @Author jyy
 * @Description {}
 * @Date 2018/7/13 10:06
 */
public class MainApp {

    public static void main(String[] args) {

        ApplicationContext ctx = new ClassPathXmlApplicationContext("Beans.xml");
        Person person = ctx.getBean(Person.class);
        person.getAddressDetail();
    }
}
<?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:aop="http://www.springframework.org/schema/aop"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
    http://www.springframework.org/schema/aop
    http://www.springframework.org/schema/aop/spring-aop-3.0.xsd ">

    <aop:config>
        <aop:aspect id="aop" ref="aopTest">
            <aop:pointcut id="selectAll"
                          expression="execution(* com.my.*.*(..))"/>
            <aop:before pointcut-ref="selectAll" method="beforeAop"/>
            <aop:after pointcut-ref="selectAll" method="afterAop"/>
        </aop:aspect>
    </aop:config>

    <bean id="person" class="com.my.Person">
        <property name="id" value="1001"/>
        <property name="name" value="张三"/>
        <property name="address" ref="address"/>
    </bean>

    <bean id="address" class="com.my.Address">
        <property name="country" value="中国"/>
        <property name="province" value="江苏省"/>
        <property name="city" value="南京市"/>
    </bean>

    <bean id="aopTest" class="com.my.AopTest"/>
</beans>

输出结果:

======before aop======
查询用户详细地址
======after aop======

上面的示例是对切面的一个简单演示,我们主要分析下配置文件:

<aop:aspect id="aop" ref="aopTest"> 定义切面aop,指向类AopTest
<aop:pointcut id="selectAll" expression="execution(* com.my.*.*(..))"/> 定义切面的切点selectAll,指定切面的作用范围
<aop:before pointcut-ref="selectAll" method="beforeAop"/> 定义方法执行前操作,包括before、after、afterReturning、afterThrowing、around

<aop:after pointcut-ref="selectAll" method="afterAop"/> 定义方法执行后操作,包括before、after、afterReturning、afterThrowing、around
以上只是部分配置,其他配置自行查找

三、基于@AspectJ

开启@AspectJ支持

<aop:aspectj-autoproxy/>

【示例】

package com.my;

/**
 * @Author jyy
 * @Description {}
 * @Date 2018/7/16 11:29
 */
public class Person {

    private String id;
    private String name;
    private Address address;

    public Person() {
    }

    public Person(Address address) {
        this.address = address;
    }

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Address getAddress() {
        return address;
    }

    public void setAddress(Address address) {
        this.address = address;
    }

    public void getAddressDetail() {
        System.out.println("查询用户详细地址");
    }
}
package com.my;

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

/**
 * @Author jyy
 * @Description {}
 * @Date 2018/8/6 11:28
 */
@Aspect
public class AopTest {

    @Pointcut("execution(* com.my.*.*(..))")
    private void aopTest() {}  

    @Before("aopTest()")
    public void beforeAop() {
        System.out.println("======before aop======");
    }

    @After("aopTest()")
    public void afterAop() {
        System.out.println("======after aop======");
    }
}
package com.my;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

/**
 * @Author jyy
 * @Description {}
 * @Date 2018/7/13 10:06
 */
public class MainApp {

    public static void main(String[] args) {

        ApplicationContext ctx = new ClassPathXmlApplicationContext("Beans.xml");
        Person person = ctx.getBean(Person.class);
        person.getAddressDetail();
    }
}
<?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:aop="http://www.springframework.org/schema/aop"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
    http://www.springframework.org/schema/aop
    http://www.springframework.org/schema/aop/spring-aop-3.0.xsd ">

    <aop:aspectj-autoproxy/>

    <bean id="person" class="com.my.Person">
        <property name="id" value="1001"/>
        <property name="name" value="张三"/>
        <property name="address" ref="address"/>
    </bean>

    <bean id="address" class="com.my.Address">
        <property name="country" value="中国"/>
        <property name="province" value="江苏省"/>
        <property name="city" value="南京市"/>
    </bean>

    <bean id="aopTest" class="com.my.AopTest"/>
</beans>

输出结果:

======before aop======
查询用户详细地址
======after aop======

 当调用com.my.包路径下的所有方法时,都会在调用方法之前调用beforeAop()方法,在这里我们可以进行权限验证,实现拦截器的作用。

原文地址:https://www.cnblogs.com/maikucha/p/9317457.html