spring aop的几种配置方式

一 通过@Aspect注解实现aop

service层方法

public interface EventInfoService{
    public int getTEventInfoByCount();
}
@Service("tEventInfoService")
public class EventInfoServiceImpl implements EventInfoService{
    public int getTEventInfoByCount(){
        return 10000;
    }
}

切面类:

@Aspect
public class MyAspect {
    
    //前置通知
    @Before("execution(* com.wutongyu.service.impl.EventInfoServiceImpl.getTEventInfoByCount(..))")
    public void before(){
        System.out.println("前置通知....");
    }
    
    //后置通知
    //returnVal,切点方法执行后的返回值
    @AfterReturning(value="execution(* com.wutongyu.service.impl.EventInfoServiceImpl.getTEventInfoByCount(..))",returning = "returnVal")
    public void AfterReturning(Object returnVal){
        System.out.println("后置通知....返回值为:"+returnVal);
    }
    
    //环绕通知
    //joinPoint 可用于执行切点的类
    @Around("execution(* com.wutongyu.service.impl.EventInfoServiceImpl.getTEventInfoByCount(..))")
    public Object around(ProceedingJoinPoint joinPoint) throws Throwable {
        System.out.println("环绕通知前....");
        Object obj= (Object) joinPoint.proceed();
        System.out.println("环绕通知后....");
        return obj;
    }
    
    //抛出通知
    @AfterThrowing(value="execution(* com.wutongyu.service.impl.EventInfoServiceImpl.getTEventInfoByCount(..))",throwing = "e")
    public void afterThrowable(Throwable e){
        System.out.println("出现异常:"+e.getMessage());
    }
    
    //无论什么情况下,方法执行完都会执行的方法
    @After(value="execution(* com.wutongyu.service.impl.EventInfoServiceImpl.getTEventInfoByCount(..))")
    public void after(){
        System.out.println("最终通知....");
    }
}

spring-test.xml配置如下:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="
        http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
        http://www.springframework.org/schema/aop
        http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context-4.0.xsd"
        default-lazy-init="true">
    <!-- 引入属性文件 -->
    <context:annotation-config />
    <!-- 自动扫描(自动注入) -->
    <context:component-scan base-package="com.wutongyu" />
    <aop:aspectj-autoproxy />
    <bean id="myAspect" class="com.wutongyu.service.impl.MyAspect"></bean>
</beans>

测试类:

public class MyTestMain {
    private static final Logger log = LoggerFactory.getLogger( MyTestMain.class );
    private static EventInfoService eventInfoService;
    public static void main(String[] args) {
        //spring初始化beans
        ApplicationContext context = new ClassPathXmlApplicationContext( "classpath:spring-test.xml" );
        eventInfoService=(EventInfoService)context.getBean("tEventInfoService");
        int count = eventInfoService.getTEventInfoByCount();
    }
}

执行结果如下:

环绕通知前....
前置通知....
环绕通知后....
最终通知....
后置通知....10000

二 通过@Pointcut注解自定义注解@Retention实现aop

自定义注解:

@Retention(RetentionPolicy.RUNTIME)
public @interface OperationType {
    public String value();
}

声明切面类和方法:

@Aspect
public class MyPointCutAspect {
    //注解其它的service
    @Autowired(required = false)
    private SysOperationLogService sysOperationLogService;
    //配置扫描的包 正则信息
    @Pointcut("execution(* com.wutongyu..*(..))")  
    public void allMethodServiceCall(){}
​
    @Around(value="(allMethodServiceCall()) && @annotation(operationType)")
    public void operationServiceCallCalls( ProceedingJoinPoint joinPoint, OperationType operationType){
        //获取操作方法上的注解信息
        String operationMethod = "";
        if( joinPoint.getSignature() != null ) {
            operationMethod = operationType.value() == null ? operationMethod : operationType.value();
        }
        Object result = null;
        try {
            result = joinPoint.proceed();
            if(result != null){
            if(result.toString().contains( "1" )||result.toString().equals( "true" ) ){
                result="操作成功";
            }else {
                result="操作失败";
            }
        }
        } catch (Throwable throwable) {
            throwable.printStackTrace();
        }
        String operationInfo = "用户进行了[" + operationMethod + "]" + "操作,"+result;
        //操作日志入库
    }
}

spring-test.xml配置如下:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="
        http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
        http://www.springframework.org/schema/aop
        http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context-4.0.xsd"
        default-lazy-init="true">
​
    <!-- 自动扫描(自动注入) -->
    <context:component-scan base-package="com.wutongyu" />
    <aop:aspectj-autoproxy />
    <bean id="myPointCutAspect" class="com.wutongyu.service.MyPointCutAspect"></bean>
</beans>

service层方法

public interface EventInfoService{
    public int deleteTEventInfo(String uuid);
    public int batchDelTEventInfos(List<String> uuidList);
}
public interface AlarmInfoService{
    public int deleteAlarmInfo(String uuid);
}
@Service("tEventInfoService")
public class EventInfoServiceImpl implements EventInfoService{
    @OperationType(value = "删除事件")
    public int deleteTEventInfo(String uuid){
        return tventInfodao.deleteTEventInfo(uuid);
    }
    @OperationType(value = "批量删除事件")
    public int batchDelTEventInfos(List<String> uuidList){
        return tEventInfodao.batchDelTEventInfos(uuidList);
    }
}
@Service("alarmInfoService")
public class AlarmInfoServiceImpl implements AlarmInfoService{
    @OperationType(value = "删除告警")
    public int deleteTEventInfo(String uuid){
        return tventInfodao.deleteTEventInfo(uuid);
    }
}

猜你喜欢

转载自blog.csdn.net/wutongyuWxc/article/details/82354198
今日推荐