Spring aop @aspect无法执行切入方法

aihu0307 aihu0307 墨衍会员 · AI 创作全网分发 墨衍智能分发 通过墨衍一键同步至各平台 可同步平台 CSDN微信公众号微博知乎掘金百家号博客园抖音小红书 你的文章也可以这样分发 墨衍会员 ¥399起/年,写一次发全网 我也要 + 加入社区
今天在用@aspect的时候碰到一个问题,代码如下:
先定义一个@aspect的方法

package com.hexin.pettyLoan.common.aop;

import java.util.Date;

import javax.annotation.Resource;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.AfterThrowing;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;


import com.hexin.core.annotation.AnnotationUtil;
import com.hexin.core.annotation.RedisRead;
import com.hexin.core.util.JSONUtil;
import com.hexin.core.util.cache.ShardedRedisUtil;


@Component
@Aspect
public class AspectJRedisRead {
@Pointcut("execution(* com.hexin.pettyLoan.*.service.impl.*.*(..))")
private void pointCut(){}
public AspectJRedisRead(){
System.out.println("aaaaaa");
}
// @Resource(name="shardedRedisUtil")
// ShardedRedisUtil redisUtil;

@Before("pointCut()") //spring中Before通知
public void readBefore() {
System.out.println("readBefore:现在时间是:"+new Date());
}

@After("pointCut()") //spring中After通知
public void readAfter() {
System.out.println("readAfter:现在时间是:"+new Date());
}
@AfterReturning("pointCut()")
public void readAfterReturning(){
System.out.println("readAfterReturning:现在时间是:"+new Date());
}
@AfterThrowing("pointCut()")
public void readAfterThrowing(){
System.out.println("readAfterThrowing:现在时间是:"+new Date());
}

@Around("pointCut()") //spring中Around通知
public Object readAround(ProceedingJoinPoint joinPoint) {
Object result = null;
try {
System.out.println("readAround开始:现在时间是:"+new Date());
result = joinPoint.proceed(joinPoint.getArgs());
System.out.println("readAround结束:现在时间是:"+new Date());
} catch (Throwable e) {
e.printStackTrace();
}
return result;
}
}


配置文件已加入aspectj-autoproxy 如下:

<context:annotation-config></context:annotation-config>
<!-- 启用aop -->
<aop:aspectj-autoproxy proxy-target-class="true" />

service实现类

@Service("flexkeyService")
public class FlexkeyServiceImpl implements FlexkeyService {
@Override
public String redisTest(Integer id){
return "this is from function";
}
}

controller调用

@RequestMapping("/redistest.do")
public @ResponseBody String redistest(String callback){
JsonResult result = new JsonResult();
try{
result = new JsonResult(1, null, flexkeyService.redisTest(1));
}
catch(ErrorCodeException ex){
result = new JsonResult(-1, ex.toMessage(), null);
logger.error(ex.toMessage(), ex);
}
String json =JSONUtil.toJsonpString(result, callback);
return json;
}


但是,在执行了redisTest方法的时候,它并没有去执行@After,@Around等对应的方法
请大神帮忙查查,是什么原因?
...全文

相关推荐

评论 28
收藏 已收藏
分享
返回顶部
aihu0307
Java EE 67,535 成员 225,843 内容