AOP(面向切面编程)初识Demo

刚学习了AOP的前值增强和后置增强,个人感觉就是在调用一些方法前,或调用一些方法后绑定一个方法,让这些方法被调用之前或者调用结束后执行这个方法。

例子:

MyAdvice类:存放调用service方法前或后需要执行的方法:

public class MyAdvice {
    /*
     * 
     * @param jp 表示一个方法调用
     */
    public void beforeLog(JoinPoint jp) {
        // jp.getTarget() 表示连接点所属的对象
        // jp.getSignature().getName() 获取连接点名称
        // jp.getArgs() 获取参数

        System.out.println(jp.getTarget() + ":" + jp.getSignature().getName()
                + "方法被调用,传入参数为" + Arrays.toString(jp.getArgs()));
    }
}

Service包:存放被绑定的方法:

public class HouseService {

    public void addHouse() {

        System.out.print("HouseServie的addHouse方法被调用");
    }

    public void findHouse() {
        System.out.print("HouseServie的findHouse方法被调用");
    }
}
public class UserService {

    public void addUser() {

        System.out.print("UserServie的addUser方法被调用");
    }

    public void findUser() {
        System.out.print("UserServie的findUser方法被调用");
    }
}

applicationContex.xml:配置AOP

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context"
    xmlns:jee="http://www.springframework.org/schema/jee" xmlns:tx="http://www.springframework.org/schema/tx"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.springframework.org/schema/aop 
    http://www.springframework.org/schema/aop/spring-aop-3.0.xsd   
    http://www.springframework.org/schema/beans 
    http://www.springframework.org/schema/beans/spring-beans-3.0.xsd   
    http://www.springframework.org/schema/context 
    http://www.springframework.org/schema/context/spring-context-3.0.xsd   
    http://www.springframework.org/schema/jee 
    http://www.springframework.org/schema/jee/spring-jee-3.0.xsd   
    http://www.springframework.org/schema/tx 
    http://www.springframework.org/schema/tx/spring-tx-3.0.xsd">
    <!-- 产生增强对象 -->
    <bean id="myadvice" class="com.lwq.advice.MyAdvice"></bean>
    
    <!-- 目标 -->
    <bean id="houseService" class="com.lwq.service.HouseService"></bean>
    <bean id="userService" class="com.lwq.service.UserService"></bean>
    
    <aop:config>
        <!-- 定义一个切入点 -->
        <aop:pointcut expression="execution(public void add*())"
            id="pt" />
        <!-- 配置一个切面,在addHouse方法调用前织入myadvice的beforeLog方法 -->
        <aop:aspect ref="myadvice">
            <aop:before method="beforeLog" pointcut-ref="pt" />
        </aop:aspect>
    </aop:config>
</beans>

这里需要注意的是aop:after-returning 需要给一个returning属性,这里给出了前置增强的配置和方法(后置没写,因为懒)~

Tset:用于调用方法测试

public class test extends TestCase {

    protected void setUp() throws Exception {
        super.setUp();
    }

    public void test() {
        ApplicationContext context = new ClassPathXmlApplicationContext(
                "classpath:applicationContext.xml");   //读取applicationContext.xml
        HouseService hs=(HouseService)context.getBean("houseService");
        hs.addHouse();
        UserService us=(UserService)context.getBean("userService");
        us.addUser();
    }
}

附上定义切入点时的表达式匹配符案例:

猜你喜欢

转载自www.cnblogs.com/MonkeyJava/p/10817424.html
今日推荐