Spring的Aop 注解配置

1,导包

2,准备目标对象

package com.songyan.anno;

public interface UserService {
     void save();
     void delete();
     void find( );
     void update();
}
package com.songyan.anno;

public class UserServiceImpl implements UserService {
    public void save()
    {
        System.out.println("添加用户");
    }
    public void delete()
    {
        System.out.println("删除用户");
    }
    
    public void find( )
    {
        System.out.println("查询用户");
    }
    
    public void update()
    {
        System.out.println("更新用户信息");
    }
}
<!--配置目标对象  -->
<bean id="userservice" class="com.songyan.anno.UserServiceImpl"></bean>

 

3,准备通知

<!--配置通知  -->
<bean id="myadvice" class="com.songyan.anno.Myadvice"></bean>
package com.songyan.anno;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;

/**
 * 通知类
 * @author sy
 */
public class Myadvice {
public void before()
    {
        System.out.println("前置通知");
    }
    public void after_returning()
    {
        System.out.println("后置通知");
    }
    public Object around(ProceedingJoinPoint pjp) throws Throwable {
        System.out.println("这是环绕通知之前的部分!!");
        Object proceed = pjp.proceed();//调用目标方法
        System.out.println("这是环绕通知之后的部分!!");
        return proceed;
    }
    public void  after_throuble()
    {
        System.out.println("异常后通知");
    }
    public void after()
    {
        System.out.println("后置通知(finally)");
    }
}

 

4,开启使用注解完成注入

s<!--开启使用注解完成织入  -->
<aop:aspectj-autoproxy></aop:aspectj-autoproxy>

完整xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context"
    xmlns:aop="http://www.springframework.org/schema/aop"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.2.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.2.xsd ">
    <!--配置目标对象 -->
    <bean id="userservice" class="com.songyan.anno.UserServiceImpl"></bean>
    <!--配置通知 -->
    <bean id="myadvice" class="com.songyan.anno.Myadvice"></bean>
    <!--开启使用注完成织入 -->
    <aop:aspectj-autoproxy></aop:aspectj-autoproxy>
</beans>

5,设置注解

package com.songyan.anno;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;

/**
 * 通知类
 * @author sy
 */
@Aspect//该注解表示这是一个通知类
public class Myadvice {

    //该注解表示该方法为前置注解,参数是对应的方法
    @Before("execution(* com.songyan.anno.*ServiceImpl.*(..))")
    public void before()
    {
        System.out.println("前置通知");
    }
    
    @AfterReturning("execution(* com.songyan.anno.*ServiceImpl.*(..))")
    public void after_returning()
    {
        System.out.println("后置通知");
    }
    
    @Around("execution(* com.songyan.anno.*ServiceImpl.*(..))")
    public Object around(ProceedingJoinPoint pjp) throws Throwable {
        System.out.println("这是环绕通知之前的部分!!");
        Object proceed = pjp.proceed();//调用目标方法
        System.out.println("这是环绕通知之后的部分!!");
        return proceed;
    }
    
    @AfterThrowing("execution(* com.songyan.anno.*ServiceImpl.*(..))")
    public void  after_throuble()
    {
        System.out.println("异常后通知");
    }
    
    @After("execution(* com.songyan.anno.*ServiceImpl.*(..))")
    public void after()
    {
        System.out.println("后置通知(finally)");
    }
}

 

6,测试类

package com.songyan.anno;

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

public class Client {
    public static void main(String[] args) {
        ApplicationContext applicationContext=new ClassPathXmlApplicationContext("com/songyan/anno/beans.xml");
        UserService u=(UserService)applicationContext.getBean("userservice");
        u.save();
    }

}

 以上代码还存在不足:

execution(* com.songyan.anno.*ServiceImpl.*(..))此内容重复出现多次
因此:
@Aspect//该注解表示这是一个通知类
public class Myadvice {
    @Pointcut("execution(* com.songyan.anno.*ServiceImpl.*(..))")
    public  void pc(){};
    
    //该注解表示该方法为前置注解,参数是对应的方法
    @Before("Myadvice.pc()")
    public void before()
    {
        System.out.println("前置通知");
    }

以这种方式声明一次,多次调用。

猜你喜欢

转载自www.cnblogs.com/excellencesy/p/9134100.html