Spring aop @aspect无法执行切入方法
今天在用@aspect的时候碰到一个问题,代码如下:
先定义一个@aspect的方法
配置文件已加入aspectj-autoproxy 如下:
service实现类
controller调用
但是,在执行了redisTest方法的时候,它并没有去执行@After,@Around等对应的方法
请大神帮忙查查,是什么原因?
先定义一个@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等对应的方法
请大神帮忙查查,是什么原因?
...全文
相关推荐
举例讲解Java的Spring框架中AOP程序设计方式的使用
主要介绍了Java的Spring框架中AOP程序设计方式的使用讲解,文中举的AOP下抛出异常的例子非常实用,需要的朋友可以参考下
Spring 4 AOP与@Aspect注解深入解析
Aspect要定义一个切入点以匹配特定方法,您可以按照以下步骤操作:指定返回类型,如表示匹配返回类型为任意的方法。指定方法名称,如表示匹配所有名为method的方法。使用通配符来匹配任意类型的参数,如表示匹配任意方法。如果需要匹配特定类型的参数,可以指定参数类型,如表示匹配所有接受一个String和一个int类型参数的方法。代码示例:@Aspect@Component在这个例子中,切入点将匹配包中所有类的所有方法。@Before。
使用切入点匹配方法实现Spring AOP环绕通知
NULL 博文链接:https://free9277.iteye.com/blog/1935492
Spring通过注解实现AOP
Spring通过注解实现AOP,Spring通过注解实现AOP,Spring通过注解实现AOPSpring通过注解实现AOP,Spring通过注解实现AOP,Spring通过注解实现AOP
Spring Aop Advise方法(增强方法) 中获取目标方法的参数
NULL 博文链接:https://jackyin5918.iteye.com/blog/1918076


