代理对象中存在target属性
因为代理对象没有进行依赖注入,获取到的代理对象,代理对象中的属性没有赋值。
当执行到方法内部后,即进入对象中方法时,则属性不为空了。因为执行到内部,实际是执行了target.xxx(),此刻已经是原始对象,而不再是代理对象.
利用cglib进行AOP的大致流程
1. 生成代理类UserServiceProxy,代理类继承UserService2. 代理类中重写了父类的方法,比如UserService中的test()方法3. **代理类中还会有一个target属性**,该属性的值为被代理对象(也就是通过UserService类推断构造方法实例化出来的对象,进行了依赖注入、初始化等步骤的对象)4. 代理类中的test()方法被执行时的逻辑如下:1. 执行切面逻辑(@Before)2. 调用target.test()3. 执行切面逻辑(@After)
验证target属性存在
1.定义SenderConfig注解
2.SendConfigAspect服务利用切面,对使用@SenderConfig注解的方法进行后置处理
3.在WafSecurityPolicyServiceImpl服务方法使用@SenderConfig注解
4.测试效果
/*** 标记配置文件发送mq*/@Target({ ElementType.METHOD })@Retention(RetentionPolicy.RUNTIME)@Documented@Inheritedpublic @interface SenderConfig {String tableName() default "";String operate() default"";}@Aspect@Component@Slf4jpublic class SendConfigAspect {@Autowiredprivate RabbitSender rabbitSender;@AfterReturning(value = "@annotation(senderConfig)", returning = "result")public void doAroundMethod(JoinPoint joinPoint, SenderConfig senderConfig, Object result) {log.info("{}表,{}操作,配置信息同步", senderConfig.tableName(), senderConfig.operate());ConfigTypeEnum configTypeEnum = ConfigTypeEnum.forName(senderConfig.tableName());ActionTypeEnum actionTypeEnum = ActionTypeEnum.forDesc(senderConfig.operate());Object o = joinPoint.getArgs()[0];if (o != null && configTypeEnum != null && actionTypeEnum != null) {Content<Object> content = Content.builder().configType(configTypeEnum.value()).actionType(actionTypeEnum.value()).configContent(hump2Underline(o)).build();String finalJson = hump2Underline(content);rabbitSender.send(finalJson);log.info("配置信息同步成功:{}", finalJson);}}/*** 将驼峰转换成下划线,进行json序列化** @param content 原驼峰实例对象* @return 下划线json字符串*/private String hump2Underline(Object content) {SerializeConfig config = new SerializeConfig();config.propertyNamingStrategy = PropertyNamingStrategy.SnakeCase;return JSON.toJSONString(content, config);}@Slf4j@Servicepublic class WafSecurityPolicyServiceImpl implements WafSecurityPolicyService {@Resourceprivate WafPolicyMapper wafPolicyMapper;@Override@SenderConfig(tableName = "ctwaf_policy", operate = "新增")public int addSecurityPolicy(WafSecurityPolicyDto wafSecurityPolicyDto) {WafPolicyDO wafPolicyDO = new WafPolicyDO();BeanUtils.copyProperties(wafSecurityPolicyDto, wafPolicyDO);wafPolicyDO.setCreateTime(new Date());wafPolicyDO.setUpdateTime(new Date());return wafPolicyMapper.insert(wafPolicyDO);}}

图一,验证AOP中代理对象中存在target属性存在,且target为被代理对象,代理对象中wafPolicyMapper属性没有进行属性注入。
当执行到具体方法中时,代理对象执行target.addSecuritryPolicy,所以图二展示的是实际对象调用效果
