我有使用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)都可以正常工作。
可能是由於AOP完成的基本代理。我的意思是在拋出的第一個錯誤中清晰可見。 –
你可以添加你的pom.xml以及這個問題 –