首先说明一下遇到这个问题的背景,在项目中有一个需求,在设备这个对象传递进方法时要先判断设备其中的一个属性 ip 能不能ping通,能ping通再往下执行,ping不通就不执行,采用aop的方式进行方法增强,将需要的方法加上注解后有的方法不走注解,有的就走了注解,随后仔细检查确认aop的表达式写的无误并且用其他表达式进行匹配,结果都是一样的,最后在百度中找到了答案。
贴图
这是切面类
package com.ubisys.drone.common.aop;
import com.ubisys.drone.common.constant.DroneConstant;
import com.ubisys.drone.common.utils.IpUtils;
import com.ubisys.drone.modules.base.entity.Device;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;
import java.util.concurrent.TimeUnit;
@Slf4j
@Component
@Aspect
public class DeviceIsConnect {
@Autowired
private StringRedisTemplate stringRedisTemplate;
/**
* 自定义aop+注解 无法拦截内部类
*/
@Pointcut(" @annotation(com.ubisys.drone.common.annotation.DeviceIpIsConnect)")
public void controllerAspect() {
}
@Before("controllerAspect()")
public void doBefore(JoinPoint joinPoint) throws Exception {
//获取方法,此处可将signature强转为MethodSignature
MethodSignature signature = (MethodSignature)joinPoint.getSignature();
String name = signature.getName();
log.info(name);
log.info("前置方法执行。。。。。。",name);
Object[] params = joinPoint.getArgs();
for (Object argItem : params) {
if (argItem instanceof Device) {
Device device = (Device) argItem;
if (device.getIp() != null) {
log.info(device.getIp());
if (IpUtils.isPing(device.getIp())) {
log.info("此设备ip可以连接,ip为: " + device.getIp());
} else {
log.error("此设备ip连接不通,ip为: " + device.getIp());
stringRedisTemplate.opsForValue().set(DroneConstant.SITENETSTATUS+device.getSiteId(),"2",7, TimeUnit.SECONDS);
device.setIp(DroneConstant.NOIP);
}
} else {
log.error("此设备ip连接不通,ip为: " + device.getIp());
stringRedisTemplate.opsForValue().set(DroneConstant.SITENETSTATUS+device.getSiteId(),"2",7,TimeUnit.SECONDS);
device.setIp(DroneConstant.NOIP);
}
}
break;
}
}
}
这是自定义注解,简单的可怜
package com.ubisys.drone.common.annotation;
import org.springframework.context.annotation.EnableAspectJAutoProxy;
import java.lang.annotation.*;
/**
* 判断设备是否连接
*/
@Target({ElementType.PARAMETER, ElementType.METHOD})//作用于参数或方法上
@Retention(RetentionPolicy.RUNTIME)
@Documented
@EnableAspectJAutoProxy(proxyTargetClass = true)
public @interface DeviceIpIsConnect {
}
这是所加注解的工具类
@DeviceIpIsConnect
public Integer widebandAttackTurnOff(Device device, String band){
String token=getDeviceLoginToken(device);
}
@DeviceIpIsConnect
public String getDeviceLoginToken(Device device) {
数量太多就贴两个吧!!
看起来没什么区别,由于本人是小白,经过一下午的斗争,发现他们是内部调用,不得已有一次问了度娘,度娘说
Spring AOP是基于动态代理机制实现的,通过动态代理机制生成目标对象的代理对象,当外部调用目标对象的相关方法时,Spring注入的其实是代理对象Proxy,通过调用代理对象的方法执行AOP增强处理,然后回调目标对象的方法
所以我的错误是因为没有通过走spring所以这个方法无法增强,最终解决办法
String token = SpringUtil.getBean(this.getClass()).getDeviceLoginToken(device);
package com.ubisys.drone.common.utils;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;
@Component
public class SpringUtil implements ApplicationContextAware {
private static ApplicationContext applicationContext;
@Override
public void setApplicationContext(ApplicationContext applicationContext)
throws BeansException {
if (SpringUtil.applicationContext == null) {
SpringUtil.applicationContext = applicationContext;
}
}
//获取applicationContext
public static ApplicationContext getApplicationContext() {
return applicationContext;
}
//通过name获取 Bean.
public static Object getBean(String name) {
return getApplicationContext().getBean(name);
}
//通过class获取Bean.
public static <T> T getBean(Class<T> clazz) {
return getApplicationContext().getBean(clazz);
}
//通过name,以及Clazz返回指定的Bean
public static <T> T getBean(String name, Class<T> clazz) {
return getApplicationContext().getBean(name, clazz);
}
}
这样就可以使用aop进行方法增强