Spring Boot - > AOP - > BeanCreationException

时间:2015-06-10 09:30:07

标签: spring spring-boot aop

我使用Spring Boot作为我的应用程序,目前一切正常。 现在我已将Spring AOP实现到此应用程序中:

@Aspect
@Component
public class ErrorAspectRestService {

    public ErrorAspectRestService() {
    }

    @Before("execution(* com....security.xauth.XAuthTokenFilter.testMethod())")
    public void logServiceAccess() {
        System.out.println("method invoked");
    }
}

当我使用上面的建议启动应用程序时,我得到以下异常:


2015-06-10 11:17:19.301 ERROR 3140 --- [ost-startStop-1] o.a.c.c.C.[Tomcat].[localhost].[/]       : Exception starting filter XAuthTokenFilter

java.lang.NullPointerException: null
    at org.springframework.web.filter.GenericFilterBean.init(GenericFilterBean.java:176)
    at org.apache.catalina.core.ApplicationFilterConfig.initFilter(ApplicationFilterConfig.java:279)
    at org.apache.catalina.core.ApplicationFilterConfig.(ApplicationFilterConfig.java:109)
    at org.apache.catalina.core.StandardContext.filterStart(StandardContext.java:4573)
    at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5188)
    at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
    at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1409)
    at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1399)
    at java.util.concurrent.FutureTask.run(Unknown Source)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
    at java.lang.Thread.run(Unknown Source)

2015-06-10 11:17:19.302 ERROR 3140 --- [ost-startStop-1] o.apache.catalina.core.StandardContext   : Error filterStart
2015-06-10 11:17:19.303 ERROR 3140 --- [ost-startStop-1] o.apache.catalina.core.StandardContext   : Context [] startup failed due to previous errors
2015-06-10 11:17:19.317  WARN 3140 --- [ost-startStop-1] o.a.c.loader.WebappClassLoaderBase       : The web application [ROOT] appears to have started a thread named [cluster-1-127.0.0.1:27017] but has failed to stop it. This is very likely to create a memory leak. Stack trace of thread:
 sun.misc.Unsafe.park(Native Method)
 java.util.concurrent.locks.LockSupport.parkNanos(Unknown Source)
 java.util.concurrent.locks.AbstractQueuedSynchronizer$ConditionObject.awaitNanos(Unknown Source)
 com.mongodb.ServerMonitor$ServerMonitorRunnable.waitForSignalOrTimeout(ServerMonitor.java:176)
 com.mongodb.ServerMonitor$ServerMonitorRunnable.waitForNext(ServerMonitor.java:157)
 com.mongodb.ServerMonitor$ServerMonitorRunnable.run(ServerMonitor.java:123)
 java.lang.Thread.run(Unknown Source)
2015-06-10 11:17:19.318  WARN 3140 --- [ost-startStop-1] o.a.c.loader.WebappClassLoaderBase       : The web application [ROOT] appears to have started a thread named [MongoCleaner1470989805] but has failed to stop it. This is very likely to create a memory leak. Stack trace of thread:
 java.lang.Thread.sleep(Native Method)
 com.mongodb.Mongo$CursorCleanerThread.run(Mongo.java:820)
2015-06-10 11:17:19.322 DEBUG 3140 --- [           main] o.s.w.c.s.StandardServletEnvironment     : Replacing [servletContextInitParams] PropertySource with [servletContextInitParams]
...
2015-06-10 11:17:20.045  WARN 3140 --- [           main] ationConfigEmbeddedWebApplicationContext : Exception encountered during context initialization - cancelling refresh attempt

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'viewControllerHandlerMapping' defined in class path resource [org/springframework/boot/autoconfigure/web/WebMvcAutoConfiguration$EnableWebMvcConfiguration.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.web.servlet.HandlerMapping]: Factory method 'viewControllerHandlerMapping' threw exception; nested exception is java.lang.IllegalStateException: The resources may not be accessed if they are not currently started
    at org.springframework.beans.factory.support.ConstructorResolver.instantiateUsingFactoryMethod(ConstructorResolver.java:599)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateUsingFactoryMethod(AbstractAutowireCapableBeanFactory.java:1119)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1014)
    ...
Caused by: org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.web.servlet.HandlerMapping]: Factory method 'viewControllerHandlerMapping' threw exception; nested exception is java.lang.IllegalStateException: The resources may not be accessed if they are not currently started
    at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:189)
    at org.springframework.beans.factory.support.ConstructorResolver.instantiateUsingFactoryMethod(ConstructorResolver.java:588)
    ... 18 common frames omitted
Caused by: java.lang.IllegalStateException: The resources may not be accessed if they are not currently started
    at org.apache.catalina.webresources.StandardRoot.validate(StandardRoot.java:245)
    at org.apache.catalina.webresources.StandardRoot.getResource(StandardRoot.java:212)
    ...
    at java.lang.reflect.Method.invoke(Unknown Source)
    at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:162)
    ... 19 common frames omitted

2015-06-10 11:17:20.058  INFO 3140 --- [           main] o.apache.catalina.core.StandardService   : Stopping service Tomcat

2015-06-10 11:17:20.085  INFO 3140 --- [           main] utoConfigurationReportLoggingInitializer : 

Error starting ApplicationContext. To display the auto-configuration report enabled debug logging (start with --debug)

2015-06-10 11:17:20.086 ERROR 3140 --- [           main] o.s.boot.SpringApplication               : Application startup failed

如果我评论@Configuration,那么一切正常,但肯定不会调用logServiceAccess方法。

2 个答案:

答案 0 :(得分:1)

您可以删除Configuration注释吗?我没有看到它的目的。 这可以解决您的错误。

答案 1 :(得分:0)

您的方面应注释为Component,以便它在Spring容器中连接。然后,您应该删除无用的@Configuration注释,并将其替换为@Component

@Aspect
@Component
public class ErrorAspectRestService {

    public ErrorAspectRestService() {
    }

    @Before("execution(* com.smartinnotec.aposoft.security.xauth.XAuthTokenFilter.testMethod())")
    public void logServiceAccess() {
        System.out.println("method invoked");
    }
}