Spring Boot -> AOP -> BeanCreationException

1.9k views Asked by At

I use Spring Boot for my apllication and currently everything works fine. Now I have implemented Spring AOP into this application:

@Aspect
@Component
public class ErrorAspectRestService {

    public ErrorAspectRestService() {
    }

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

When I start the application with the advice above than I get the following exception:


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

If I comment @Configuration than everything works fine but for sure the method logServiceAccess is not invoked.

2

There are 2 answers

2
Sezin Karli On BEST ANSWER

Can you remove Configuration annotation? I don't see the purpose for it. This can fix your error.

0
tmarwen On

Your aspect should be annotated as a Component so it gets wired in the Spring container. You should then drop the useless @Configuration annotation and replace it with @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");
    }
}