2015-05-18 51 views
0

我有使用Spring,Apache CXF和Jetty的Web應用程序。 Spring不喜歡在我的一個類中添加一個名爲@Loggable的註釋。來自Spring的BeanCreationException,帶有註解「@Context」和「@Loggable」

首先,註釋@Loggable鏈接到類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 -annotation,什麼被注射時。

該類是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(); 
    } 

的另外@Loggable -annotation上的isUserLoggedIn方法給了我以下異常:

 WARN org.eclipse.jetty.webapp.WebAppContext - Failed startup of context [email protected]{/,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$1.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並將其添加到設置器方法中:

private HttpServletRequest request; 

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

這給我這個例外:

WARN org.eclipse.jetty.webapp.WebAppContext - Failed startup of context [email protected]{/,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$1.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)都可以正常工作。

+0

可能是由於AOP完成的基本代理。我的意思是在拋出的第一個錯誤中清晰可見。 –

+0

你可以添加你的pom.xml以及這個問題 –

回答

0

我現在收到了關於如何解決此問題的幫助。簡而言之,必須將@Context註釋移到該類所實現的接口的方法中。

的解決方案如下:

  1. 刪除私有成員HttpServletRequest request及其@Context註釋。
  2. HttpServletRequest作爲參數添加到使用它的方法中。 實施例:

    公共響應registerUser(HttpServletRequest的請求,...){

  3. @Context註釋添加到REST接口代替。 實施例:

    @Path( 「註冊/用戶」)
    @POST
    @Consumes( 「應用程序/ JSON」)

    響應registerUser(@Context HttpServletRequest的HttpServletRequest的,..);

0

問題出在spring-beans-4.1.6.RELEASE.jar。

希望你已經添加了Spring-beans-4.1.6.jar文件由於某些問題,Still Spring有時無法獲取jar。在這種情況下,

從服務器中刪除已存在的項目。

右鍵單擊您的服務器,

點擊乾淨,

右鍵單擊您的服務器,

單擊清除工作目錄。

Spring MVC的也可以獲取jar文件以這種方式,它的工作原理

右鍵點擊你的Spring MVC項目,

構建路徑 - >配置構建路徑

一個標籤就會出現,

選擇添加外部JAR的按鈕並添加所需的所有罐子。

之後,單擊部署大會,

,單擊Add按鈕,

單擊Java構建路徑條目,

單擊下一步,

選擇所有正在出現的這個窗口上的罐子。

單擊完成。

刷新您的項目,

運行該項目。

截至現在它會以正確的方式獲取spring-beans-4.1.6.RELEASE.jar。希望能幫助到你。

+0

謝謝你的答案,但我不認爲這是問題的原因。我的項目庫中有「Maven:org.springframework:spring-beans:4.1.6.RELEASE」。 –

+0

這與maven工作正常..如果你的問題還沒有解決..你可以試試看。 –

相關問題