AOP

Posted 囧雪

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了AOP相关的知识,希望对你有一定的参考价值。

方法一(Annotation):

1.新建接口(IHuman),两个类(American和Chinese)继承该接口

package maya.entities;
import org.springframework.stereotype.Component;

public interface IHuman {
    public void eat();
    public void sleep();
}

@Component
public class American implements IHuman {

    @Override
    public void eat() {
        System.out.println("美国人在吃饭");        
    }

    @Override
    public void sleep() {
        System.out.println("美国人在睡觉");        
    }    
}

@Component
public class Chinese implements IHuman {

    @Override
    public void eat() {
        System.out.println("中国人正在吃饭");        
    }
    @Override
    public void sleep() {
        System.out.println("中国人正在睡觉");        
    }
}

2.建立切面类

package maya.aop;

import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.springframework.stereotype.Component;

@Aspect
@Component
public class HumanAOP {
    @Before("execution(* maya.entities.*.eat(..))")
    public void beforeEat() {
        System.out.println("饭前洗手");
    }
    @Before("execution(* maya.entities.*.sleep(..))")
    public void beforeSleep() {
        System.out.println("洗澡后睡觉");
    }
}

3.配置beans.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:aop="http://www.springframework.org/schema/aop"
    xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="http://www.springframework.org/schema/beans 
        http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/aop 
        http://www.springframework.org/schema/aop/spring-aop-4.3.xsd
        http://www.springframework.org/schema/context 
        http://www.springframework.org/schema/context/spring-context-4.3.xsd">

    <context:component-scan base-package="maya.entities,maya.aop" ></context:component-scan>
    
    <!-- 启动第三方的aspectjweaver.jar中的功能 -->
    <aop:aspectj-autoproxy proxy-target-class="true"></aop:aspectj-autoproxy>
    
</beans>

4.建立测试类,运行

package maya.test;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import maya.entities.American;
import maya.entities.Chinese;

public class Test {
    public static void main(String[] args) {
        ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
        
        Chinese c = (Chinese)context.getBean("chinese");
        c.eat();
        c.sleep();
        
        American a = context.getBean(American.class);
        a.eat();
        a.sleep();
    }
}

运行结果:

饭前洗手
中国人正在吃饭
洗澡后睡觉
中国人正在睡觉
饭前洗手
美国人在吃饭
洗澡后睡觉
美国人在睡觉

 

 

方法二xml(推荐):

1.新建接口(IHuman),两个类(American和Chinese)继承该接口

package maya.entities;

public interface IHuman {
    public void eat();
    public void sleep();
}
public class American implements IHuman { @Override public void eat() { System.out.println("美国人要吃饭了"); } @Override public void sleep() { System.out.println("美国人要睡觉了"); } }
public class Chinese implements IHuman { @Override public void eat() { System.out.println("中国人要吃饭了"); } @Override public void sleep() { System.out.println("中国人要睡觉了"); } }

2.建立切面类

package maya.aop;

public class HumanAOP {
    public void beforeEat() {
        System.out.println("饭前洗手");
    }
    public void afterEat() {
        System.out.println("饭后漱口");
    }
}

3.配置beans.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:aop="http://www.springframework.org/schema/aop"
    xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="http://www.springframework.org/schema/beans 
        http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/aop 
        http://www.springframework.org/schema/aop/spring-aop-4.3.xsd
        http://www.springframework.org/schema/context 
        http://www.springframework.org/schema/context/spring-context-4.3.xsd"
        default-autowire="byName"    
        >
    
    <bean id="chinese" class="maya.entities.Chinese"></bean>
    <bean id="american" class="maya.entities.American"></bean>
    <bean id="humanAOP" class="maya.aop.HumanAOP"></bean>
    
    <aop:config>
        <aop:pointcut expression="execution(* maya.entities.*.eat(..))" id="beforeEat"></aop:pointcut>
        <aop:pointcut expression="execution(* maya.entities.*.eat(..))" id="afterEat"></aop:pointcut>
        
        <aop:aspect id="ha" ref="humanAOP">
            <aop:before method="beforeEat()" pointcut-ref="beforeEat"></aop:before>
            <aop:after method="afterEat()" pointcut-ref="afterEat"></aop:after>
        </aop:aspect>
    </aop:config>
</beans>

4.建立测试类,运行

package maya.test;

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

import maya.entities.IHuman;

public class Test {
    public static void main(String[] args) {
        ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
        IHuman c = (IHuman)context.getBean("american");
        c.eat();
    }
}

运行结果:

饭前洗手
美国人要吃饭了
饭后漱口

 

以上是关于AOP的主要内容,如果未能解决你的问题,请参考以下文章

阿里四面:你知道Spring AOP创建Proxy的过程吗?

AOP框架Dora.Interception 3.0 [3]: 拦截器设计

小议webpack下的AOP式无侵入注入

Spring AOP

AOP之AspectJ - 代码注入

AOP之AspectJ - 代码注入