启用S​​pring AOP时的异常(同时创建RESTful服务)

时间:2015-07-30 02:48:19

标签: java spring spring-mvc spring-aop

我正在学习Spring&正在创建RESTful服务。我正在尝试使用AOP来查找所有公共方法的执行时间。但是,在创建Servlet期间获得异常。

以下是我的代码。

src/main/webapp/WEB-INF/springmvc-servlet.xml

<?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:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.0.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-4.0.xsd"
>

    <context:component-scan base-package="com" />
    <context:annotation-config />
    <mvc:annotation-driven />
    <aop:aspectj-autoproxy/>
</beans>

com.aop.ExecutionTimeLoggingSpringAOP

@Component
@Aspect
public class ExecutionTimeLoggingSpringAOP {

    final static Logger logger = Logger.getLogger(ExecutionTimeLoggingSpringAOP
.class);

    @Around("execution(public * *(..))")
    public Object profile(ProceedingJoinPoint pjp) throws Throwable {
        long startTime = System.nanoTime();
        String className = pjp.getTarget().getClass().getCanonicalName();
        String methodName = pjp.getSignature().getName();

        Object output = pjp.proceed();

        long elapsedTime = System.nanoTime() - startTime;

        logger.debug("Execution of " + className + "#" + methodName
                + " ended in " + new BigDecimal(elapsedTime).divide(
        new BigDecimal(1000000)) + " milliseconds");

        return output;
    }
}

以下是我得到的例外情况,

javax.servlet.ServletException: Servlet.init() for servlet springmvc threw exception
...
java.lang.Thread.run(Thread.java:745)
root cause

org.springframework.beans.factory.BeanCreationException: Error creating bean with name
'org.springframework.web.servlet.mvc.method.
annotation.RequestMappingHandlerMapping#0': 
Initialization of bean failed; nested exception is org.springframework.beans.
ConversionNotSupportedException: ....

我在这里错过了什么吗?

PS:REST API在不使用AOP时起作用(即从aop:aspectj-autoproxy移除springmvc-servlet.xml时)

1 个答案:

答案 0 :(得分:2)

您的切入点表达式过于通用。这将为您的所有Spring托管bean创建代理,包括基础结构bean。尝试更具体(类似execution(* com.yourcompany.*.*(..)))以仅代理组件类。