Spring AOP方面没有执行

时间:2015-04-13 11:39:15

标签: java spring aop aspectj

我想记录我的服务方法的执行时间 我认为AOP是一种简单的方法,所以我写了一个方面:

@Aspect
public class ServiceLogAdviceAspect {
    private static Logger LOG = LoggerFactory.getLogger(ServiceLogAdviceAspect.class);

    @Around("execution(* com.j1.**.service.*(..))")
    public Object doBasicProfilingTime(ProceedingJoinPoint joinPoint) throws Throwable {
        String methodName = joinPoint.getSignature().getName();
        Object target = joinPoint.getTarget();
        long start = System.currentTimeMillis();
        Object retVal = joinPoint.proceed();
        long end = System.currentTimeMillis();
        LOG.error(String.format("Invoke [%s$%s] Takes %d ms", target.getClass().getCanonicalName(), methodName, (end - start)));
        return retVal;
    }
}

和Spring配置:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:aop="http://www.springframework.org/schema/aop"
    xsi:schemaLocation="
        http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
        http://www.springframework.org/schema/aop
        http://www.springframework.org/schema/aop/spring-aop-3.0.xsd"
        default-autowire="byName">
    <aop:aspectj-autoproxy/>
    <bean id="logAdviceAspect" 
        class="com.j1.soa.common.aspect.ServiceLogAdviceAspect"></bean>
</beans>

但是当我调用方法时 public ServiceMessage<GoodsDetailDto> getGoodDetail(GoodsDetailDto goodsDetailDto)

我既没有错误输出也没有输入断点。


修改

getGoodDetail在班级中定义 com.j1.soa.resource.item.service.GoodsDetailServiceImpl

我用Hessian RPC调用它,

首先在application-context-rpc.xml

中定义了Remote Service的Spring bean
<?xml version="1.0" encoding="UTF-8"?>
<beans>
    <bean id="goodsDetailService" class="org.springframework.remoting.caucho.HessianProxyFactoryBean">
        <property name="serviceUrl" value="http://api.soa.item.j1.com/hessian/goodsDetailService" />
        <property name="serviceInterface" value="com.j1.soa.resource.item.api.GoodsDetailService" />
        <property name="readTimeout" value="6000"/>
    </bean>
</beans>

然后在客户端

中定义它
@Autowired
private GoodsDetailService goodsDetailService;

使用它

GoodsDetailDto goodsDetailDto = new GoodsDetailDto();
goodsDetailDto.setGoodsId(NumericUtil.parseLong(goodsId));
goodsDetailDto.setProductId(NumericUtil.parseInt(productId));
goodsDetailDto.setSiteType(SiteType.MOBILE);

ServiceMessage<GoodsDetailDto> detailResult = goodsDetailService
                .getGoodDetail(goodsDetailDto);

1 个答案:

答案 0 :(得分:0)

您应该在logAdviceAspect

中添加<aop:aspectj-autoproxy/>
<aop:aspectj-autoproxy>
    <aop:include name="logAdviceAspect"/>
</aop:aspectj-autoproxy>