启用 Spring AOP 时出现异常(创建 RESTful 服务时)
Exception in enabling Spring AOP (while creating RESTful service)
我正在学习 Spring 并且正在创建 RESTful 服务。我正在尝试使用 AOP 找出所有 public 方法的执行时间。但是,在创建 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
中删除时)
您的切入点表达式过于笼统。这将为所有 Spring 托管 bean(包括基础结构 bean)创建代理。尝试更具体(类似于 execution(* com.yourcompany.*.*(..))
)仅代理您的组件 类.
我正在学习 Spring 并且正在创建 RESTful 服务。我正在尝试使用 AOP 找出所有 public 方法的执行时间。但是,在创建 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
中删除时)
您的切入点表达式过于笼统。这将为所有 Spring 托管 bean(包括基础结构 bean)创建代理。尝试更具体(类似于 execution(* com.yourcompany.*.*(..))
)仅代理您的组件 类.