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

J2EE 码拜 8年前 (2016-03-16) 4222次浏览
今天在用@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等对应的方法
请高手帮忙查查,是什么原因?

解决方案

10

1. component-scan 定义了没
2. 试试 @Before(“execution(* com.hexin.pettyLoan.*.service.impl.*.*(..))”)

20

引用:

问题找到了
result = new JsonResult(1, null, flexkeyService.redisTest(1));
这个方法里的flexkeyService对象假如使用autowared注入,无法启动aspect,
但是
flexkeyService = ctx.getBean(“xxxxx”)获取,是可以启用aspect的
这是什么原因?

new JsonResult: 直接 new 的对象不行,这个时候 flexkeyService 并不是注入进来的,即使有 @Autowired 注解,这时的注解没有任何作用。
只有 Spring 生成的对象才有 AOP 功能,原因是 Spring 生成的代理对象才有 AOP 功能。

10

引用:
Quote: 引用:

问题找到了
result = new JsonResult(1, null, flexkeyService.redisTest(1));
这个方法里的flexkeyService对象假如使用autowared注入,无法启动aspect,
但是
flexkeyService = ctx.getBean(“xxxxx”)获取,是可以启用aspect的
这是什么原因?

new JsonResult: 直接 new 的对象不行,这个时候 flexkeyService 并不是注入进来的,即使有 @Autowired 注解,这时的注解没有任何作用。
只有 Spring 生成的对象才有 AOP 功能,原因是 Spring 生成的代理对象才有 AOP 功能。

问一下在什么情况下,会切入不进去。本人也遇到了这样的问题,困扰了好久了。


CodeBye 版权所有丨如未注明 , 均为原创丨本网站采用BY-NC-SA协议进行授权 , 转载请注明Spring aop @aspect无法执行切入方法
喜欢 (0)
[1034331897@qq.com]
分享 (0)