2016-12-15 1 views
0

친애하는 StackOverflow 전문가.Autowiring ApplicationListener as null

우리는 현재 스프린트에서 흥미로운 문제를 해결하고 있으며 Spring 워크 플로우에 대한 우리의 이해는 해결책을 찾을 수 없습니다. 다음 문제에 대한 생각을 공유해 주시기 바랍니다.

지금 수정할 수없는 라이브러리에서 수업을 가져와야합니다. 우리가 가져와야하는 클래스는 "ApplicationListener"를 구현하는 클래스의 Autowired 종속성을가집니다. 코드에서이 종속성을 사용하지 않습니다 (호출하지 않을 메서드에서 사용됨).

사용하지 않는 모든 종속성을 null로 설정하려고합니다. 그러나 우리가 널 (null)이 클래스를 설정할 때 우리는 예외가 얻을 : 분명히

Caused by: java.lang.IllegalArgumentException: Delegate listener must not be null 

, 우리는 우리의 응용 프로그램에서 만약 ApplicationListener 클래스를 인스턴스화하지 않으려합니다.

Application 구성 :

@Configuration 
public class TestApplicationConfiguration { 
    @Bean 
    public ApplicationListener<ContextRefreshedEvent> applicationListener() { 
     return null; 
    } 
} 

테스트 클래스 : 아래의 예외의

@ContextConfiguration(classes = TestApplicationConfiguration.class) 
public class ConfigurationManagerTests extends  AbstractTestNGSpringContextTests { 
    @Autowired 
    private ApplicationListener<ContextRefreshedEvent> applicationListener; 

    @Test 
    public void dummyTest() { 
     Assert.assertNull(applicationListener); 
    } 
} 

Full 스택 트레이스를

우리는 간단한 단위 테스트를 만드는데 필요한 상황을 재현 할 수 있습니다.

java.lang.IllegalStateException: Failed to load ApplicationContext 

    at org.springframework.test.context.cache.DefaultCacheAwareContextLoaderDelegate.loadContext(DefaultCacheAwareContextLoaderDelegate.java:124) 
    at org.springframework.test.context.support.DefaultTestContext.getApplicationContext(DefaultTestContext.java:83) 
    at org.springframework.test.context.support.DependencyInjectionTestExecutionListener.injectDependencies(DependencyInjectionTestExecutionListener.java:117) 
    at org.springframework.test.context.support.DependencyInjectionTestExecutionListener.prepareTestInstance(DependencyInjectionTestExecutionListener.java:83) 
    at org.springframework.test.context.TestContextManager.prepareTestInstance(TestContextManager.java:230) 
    at org.springframework.test.context.testng.AbstractTestNGSpringContextTests.springTestContextPrepareTestInstance(AbstractTestNGSpringContextTests.java:149) 
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) 
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) 
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) 
    at java.lang.reflect.Method.invoke(Method.java:483) 
    at org.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:86) 
    at org.testng.internal.Invoker.invokeConfigurationMethod(Invoker.java:514) 
    at org.testng.internal.Invoker.invokeConfigurations(Invoker.java:215) 
    at org.testng.internal.Invoker.invokeConfigurations(Invoker.java:142) 
    at org.testng.internal.TestMethodWorker.invokeBeforeClassMethods(TestMethodWorker.java:178) 
    at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:108) 
    at org.testng.TestRunner.privateRun(TestRunner.java:782) 
    at org.testng.TestRunner.run(TestRunner.java:632) 
    at org.testng.SuiteRunner.runTest(SuiteRunner.java:366) 
    at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:361) 
    at org.testng.SuiteRunner.privateRun(SuiteRunner.java:319) 
    at org.testng.SuiteRunner.run(SuiteRunner.java:268) 
    at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:52) 
    at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:86) 
    at org.testng.TestNG.runSuitesSequentially(TestNG.java:1244) 
    at org.testng.TestNG.runSuitesLocally(TestNG.java:1169) 
    at org.testng.TestNG.run(TestNG.java:1064) 
    at org.testng.IDEARemoteTestNG.run(IDEARemoteTestNG.java:72) 
    at org.testng.RemoteTestNGStarter.main(RemoteTestNGStarter.java:124) 
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) 
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) 
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) 
    at java.lang.reflect.Method.invoke(Method.java:483) 
    at com.intellij.rt.execution.application.AppMain.main(AppMain.java:147) 
Caused by: java.lang.IllegalArgumentException: Delegate listener must not be null 
    at org.springframework.util.Assert.notNull(Assert.java:115) 
    at org.springframework.context.event.GenericApplicationListenerAdapter.<init>(GenericApplicationListenerAdapter.java:48) 
    at org.springframework.context.event.AbstractApplicationEventMulticaster.supportsEvent(AbstractApplicationEventMulticaster.java:280) 
    at org.springframework.context.event.AbstractApplicationEventMulticaster.retrieveApplicationListeners(AbstractApplicationEventMulticaster.java:229) 
    at org.springframework.context.event.AbstractApplicationEventMulticaster.getApplicationListeners(AbstractApplicationEventMulticaster.java:185) 
    at org.springframework.context.event.SimpleApplicationEventMulticaster.multicastEvent(SimpleApplicationEventMulticaster.java:127) 
    at org.springframework.context.support.AbstractApplicationContext.publishEvent(AbstractApplicationContext.java:382) 
    at org.springframework.context.support.AbstractApplicationContext.publishEvent(AbstractApplicationContext.java:336) 
    at org.springframework.context.support.AbstractApplicationContext.finishRefresh(AbstractApplicationContext.java:877) 
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:544) 
    at org.springframework.test.context.support.AbstractGenericContextLoader.loadContext(AbstractGenericContextLoader.java:128) 
    at org.springframework.test.context.support.AbstractGenericContextLoader.loadContext(AbstractGenericContextLoader.java:60) 
    at org.springframework.test.context.support.AbstractDelegatingSmartContextLoader.delegateLoading(AbstractDelegatingSmartContextLoader.java:108) 
    at org.springframework.test.context.support.AbstractDelegatingSmartContextLoader.loadContext(AbstractDelegatingSmartContextLoader.java:251) 
    at org.springframework.test.context.cache.DefaultCacheAwareContextLoaderDelegate.loadContextInternal(DefaultCacheAwareContextLoaderDelegate.java:98) 
    at org.springframework.test.context.cache.DefaultCacheAwareContextLoaderDelegate.loadContext(DefaultCacheAwareContextLoaderDelegate.java:116) 
... 33 more 

감사합니다.

답변

0

스프링 자체가 빈을 null로 허용하지 않는 것처럼 보이므로 아무 것도하지 않는 더미를 만들지 않는 이유는 무엇입니까?

@Bean 
public ApplicationListener<ContextRefreshedEvent> applicationListener() { 
    return new ApplicationListener<ContextRefreshedEvent>() { 
    @Override 
    public void onApplicationEvent(ContextRefreshedEvent event) { 
     // Do nothing 
    } 
    }; 
} 
+0

감사합니다. Adam! ContentRefreshedEvents에 아무 것도하지 않는 ApplicationListener를 구현했습니다. – Sruthi