来自 Spring 的 BeanCreationException,带有注释“@Context”和“@Loggable”

BeanCreationException from Spring with annotation "@Context" and "@Loggable"

我有使用 Spring、Apache CXF 和 Jetty 的 Web 应用程序。 Spring 不喜欢在我的 class 之一的方法中添加一个名为 @Loggable 的注释。

首先,注释 @Loggable 链接到 class LogAspect,它记录请求和响应消息:

@Aspect
@Component
public class LogAspect {

     ...
     ...
        @Around("execution(* * (..)) && @annotation(Loggable)")
        public Object logAround(ProceedingJoinPoint joinPoint) throws Throwable {

          ...
          if ( returnValue != null) {
                logger.infoRequestResponse(clazz, name, joinPoint.getArgs(), returnValue);
          } else {
                logger.infoRequestResponse(clazz, name, joinPoint.getArgs(), "void");
          }
          return returnValue;
        }
}

我猜它与 @Context-注释有关,以及什么时候被注入。

class 是 RESTful 服务的实现。私有成员request上面有注解@Context

@org.springframework.stereotype.Service(value = "resellerService")
public class ResellerServiceImpl implements ResellerService {

    @Context
    private HttpServletRequest request;
...
...
    @Loggable
    @Override
    public Response isUserLoggedIn() {
        Optional<ResellerSession> maybeSession = getSessionFromContext();
        return Response.ok(maybeSession.isPresent()).build();
    }

isUserLoggedIn 方法上添加 @Loggable-注释会出现以下异常:

        WARN  org.eclipse.jetty.webapp.WebAppContext - Failed startup of context o.e.j.w.WebAppContext@79a9e51e{/,file:/home/lars/intellij_wspace/app/app-node/trunk/src/main/webapp/,STARTING}{src/main/webapp/}
        org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'resellerServiceServer': Invocation of init method failed; nested exception is org.apache.cxf.service.factory.ServiceConstructionException
            at [SNIP]
    org.eclipse.jetty.webapp.WebAppContext.startWebapp(WebAppContext.java:1349) ~[jetty-webapp-9.2.10.v20150310.jar:9.2.10.v20150310]
            at ]
[SNIP]
        Caused by: org.apache.cxf.service.factory.ServiceConstructionException: null
            at org.apache.cxf.jaxrs.JAXRSServerFactoryBean.create(JAXRSServerFactoryBean.java:219) ~[cxf-rt-frontend-jaxrs-3.1.0.jar:3.1.0]
            at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.7.0_80]
            at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57) ~[na:1.7.0_80]
            at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.7.0_80]
            at java.lang.reflect.Method.invoke(Method.java:606) ~[na:1.7.0_80]
            at 
    [SNIP]
            ... 33 common frames omitted
        Caused by: java.lang.IllegalArgumentException: Can not set javax.servlet.http.HttpServletRequest field com.app.service.impl.reseller.ResellerServiceImpl.request to com.sun.proxy.$Proxy109
            at sun.reflect.UnsafeFieldAccessorImpl.throwSetIllegalArgumentException(UnsafeFieldAccessorImpl.java:164) ~[na:1.7.0_80]
            at sun.reflect.UnsafeFieldAccessorImpl.throwSetIllegalArgumentException(UnsafeFieldAccessorImpl.java:168) ~[na:1.7.0_80]
            at sun.reflect.UnsafeFieldAccessorImpl.ensureObj(UnsafeFieldAccessorImpl.java:55) ~[na:1.7.0_80]
            at sun.reflect.UnsafeObjectFieldAccessorImpl.set(UnsafeObjectFieldAccessorImpl.java:75) ~[na:1.7.0_80]
            at java.lang.reflect.Field.set(Field.java:741) ~[na:1.7.0_80]
            at org.apache.cxf.jaxrs.utils.InjectionUtils.run(InjectionUtils.java:192) ~[cxf-rt-frontend-jaxrs-3.1.0.jar:3.1.0]
            at java.security.AccessController.doPrivileged(Native Method) ~[na:1.7.0_80]
            at org.apache.cxf.jaxrs.utils.InjectionUtils.injectFieldValue(InjectionUtils.java:188) ~[cxf-rt-frontend-jaxrs-3.1.0.jar:3.1.0]
            at org.apache.cxf.jaxrs.utils.InjectionUtils.injectContextProxiesAndApplication(InjectionUtils.java:1058) ~[cxf-rt-frontend-jaxrs-3.1.0.jar:3.1.0]
            at org.apache.cxf.jaxrs.JAXRSServerFactoryBean.injectContexts(JAXRSServerFactoryBean.java:405) ~[cxf-rt-frontend-jaxrs-3.1.0.jar:3.1.0]
            at org.apache.cxf.jaxrs.JAXRSServerFactoryBean.updateClassResourceProviders(JAXRSServerFactoryBean.java:429) ~[cxf-rt-frontend-jaxrs-3.1.0.jar:3.1.0]
            at org.apache.cxf.jaxrs.JAXRSServerFactoryBean.create(JAXRSServerFactoryBean.java:162) ~[cxf-rt-frontend-jaxrs-3.1.0.jar:3.1.0]

所以我尝试删除请求中的 @Context,并将其添加到 setter 方法中:

private HttpServletRequest request;

@Context
public void setRequest(HttpServletRequest request) { this.request = request; }

这给了我这个例外:

 WARN  org.eclipse.jetty.webapp.WebAppContext - Failed startup of context o.e.j.w.WebAppContext@25ddbf05{/,file:/home/lars/intellij_wspace/app/app-node/trunk/src/main/webapp/,STARTING}{src/main/webapp/}
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'resellerServiceServer': Invocation of init method failed; nested exception is org.apache.cxf.service.factory.ServiceConstructionException
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1574) ~[spring-beans-4.1.6.RELEASE.jar:4.1.6.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:539) ~[spring-beans-4.1.6.RELEASE.jar:4.1.6.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:476) ~[spring-beans-4.1.6.RELEASE.jar:4.1.6.RELEASE]
at org.springframework.beans.factory.support.AbstractBeanFactory.getObject(AbstractBeanFactory.java:303) ~[spring-beans-4.1.6.RELEASE.jar:4.1.6.RELEASE]
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230) ~[spring-beans-4.1.6.RELEASE.jar:4.1.6.RELEASE]
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:299) ~[spring-beans-4.1.6.RELEASE.jar:4.1.6.RELEASE]
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:194) ~[spring-beans-4.1.6.RELEASE.jar:4.1.6.RELEASE]
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:755) ~[spring-beans-4.1.6.RELEASE.jar:4.1.6.RELEASE]
    at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:757) ~[spring-context-4.1.6.RELEASE.jar:4.1.6.RELEASE]
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:480) ~[spring-context-4.1.6.RELEASE.jar:4.1.6.RELEASE]
    at org.springframework.web.context.ContextLoader.configureAndRefreshWebApplicationContext(ContextLoader.java:403) ~[spring-web-4.1.6.RELEASE.jar:4.1.6.RELEASE]
    at org.springframework.web.context.ContextLoader.initWebApplicationContext(ContextLoader.java:306) ~[spring-web-4.1.6.RELEASE.jar:4.1.6.RELEASE]
    ...
    ...
Caused by: org.apache.cxf.service.factory.ServiceConstructionException: null
    at org.apache.cxf.jaxrs.JAXRSServerFactoryBean.create(JAXRSServerFactoryBean.java:219) ~[cxf-rt-frontend-jaxrs-3.1.0.jar:3.1.0]
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.7.0_80]
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57) ~[na:1.7.0_80]
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.7.0_80]
    at java.lang.reflect.Method.invoke(Method.java:606) ~[na:1.7.0_80]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeCustomInitMethod(AbstractAutowireCapableBeanFactory.java:1702) ~[spring-beans-4.1.6.RELEASE.jar:4.1.6.RELEASE]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1641) ~[spring-beans-4.1.6.RELEASE.jar:4.1.6.RELEASE]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1570) ~[spring-beans-4.1.6.RELEASE.jar:4.1.6.RELEASE]
    ... 33 common frames omitted
Caused by: javax.ws.rs.InternalServerErrorException: HTTP 500 Internal Server Error
    at org.apache.cxf.jaxrs.utils.SpecExceptions.toInternalServerErrorException(SpecExceptions.java:79) ~[cxf-rt-frontend-jaxrs-3.1.0.jar:3.1.0]
    at org.apache.cxf.jaxrs.utils.ExceptionUtils.toInternalServerErrorException(ExceptionUtils.java:106) ~[cxf-rt-frontend-jaxrs-3.1.0.jar:3.1.0]
    at org.apache.cxf.jaxrs.utils.InjectionUtils.reportServerError(InjectionUtils.java:472) ~[cxf-rt-frontend-jaxrs-3.1.0.jar:3.1.0]
    at org.apache.cxf.jaxrs.utils.InjectionUtils.reportServerError(InjectionUtils.java:458) ~[cxf-rt-frontend-jaxrs-3.1.0.jar:3.1.0]
    at org.apache.cxf.jaxrs.utils.InjectionUtils.injectThroughMethod(InjectionUtils.java:314) ~[cxf-rt-frontend-jaxrs-3.1.0.jar:3.1.0]
    at org.apache.cxf.jaxrs.utils.InjectionUtils.injectThroughMethod(InjectionUtils.java:294) ~[cxf-rt-frontend-jaxrs-3.1.0.jar:3.1.0]
    at org.apache.cxf.jaxrs.utils.InjectionUtils.injectContextProxiesAndApplication(InjectionUtils.java:1046) ~[cxf-rt-frontend-jaxrs-3.1.0.jar:3.1.0]
    at org.apache.cxf.jaxrs.JAXRSServerFactoryBean.injectContexts(JAXRSServerFactoryBean.java:405) ~[cxf-rt-frontend-jaxrs-3.1.0.jar:3.1.0]
    at org.apache.cxf.jaxrs.JAXRSServerFactoryBean.updateClassResourceProviders(JAXRSServerFactoryBean.java:429) ~[cxf-rt-frontend-jaxrs-3.1.0.jar:3.1.0]
    at org.apache.cxf.jaxrs.JAXRSServerFactoryBean.create(JAXRSServerFactoryBean.java:162) ~[cxf-rt-frontend-jaxrs-3.1.0.jar:3.1.0]
    ... 40 common frames omitted

有人知道如何解决这个问题吗?

编辑

我无法显示整个 pom.xml,但我确实有这一行 4.1.6.RELEASE

构建看起来和工作正常。 @Autowired 等所有其他注释都可以正常工作。

问题出在 spring-beans-4.1.6.RELEASE.jar.

希望您已添加 Spring-beans-4.1.6.jar 文件 仍然 Spring 有时由于某些问题无法获取 jar。在这种情况下

从您的服务器中删除现有的项目。

右键单击您的服务器,

点击清理,

右键单击您的服务器,

单击清除工作目录。

Spring MVC也可以通过这种方式获取jar文件,有效

右键单击您的 Spring MVC 项目,

构建路径 -> 配置构建路径

将出现一个标签,

Select 添加外部 JAR 按钮并添加您想要的所有 jar。

之后,点击部署程序集,

点击添加按钮,

单击 Java 构建路径条目,

点击下一步,

Select 所有出现在 window 上的罐子。

单击“完成”。

刷新您的项目,

运行 项目。

截至目前,它将以正确的方式获取 spring-beans-4.1.6.RELEASE.jar。希望对你有帮助。

我现在已经收到有关如何解决此问题的帮助。简而言之,必须将 @Context 注释移动到 class 实现的接口方法中。

解决方法如下:

  1. 删除了私有成员 HttpServletRequest request 及其 @Context 注释。
  2. HttpServletRequest 作为参数添加到使用它的方法中。 示例:

    public 响应 registerUser(HttpServletRequest 请求,...) {

  3. 改为将 @Context 注释添加到 REST 接口。 示例:

    @Path("register/user")
    @POST
    @Consumes("application/json")

    响应注册用户(@Context HttpServletRequest httpServletRequest, ..);