内部Spring配置中的PostConstruct未被调用

时间:2017-10-10 06:22:05

标签: java spring spring-security postconstruct

我试图在Spring Security中设置多个过滤器链,并遇到一个奇怪的问题。

@EnableWebSecurity
@EnableGlobalMethodSecurity(prePostEnabled = true, securedEnabled = true)
public class SecurityConfiguration {

    @Configuration("apiWebSecurityConfigurationAdapter")
    public static class ApiWebSecurityConfigurationAdapter extends WebSecurityConfigurerAdapter {
        ....
    }


    @Configuration("ssoWebSecurityConfigurerAdapter")
    @Order(1)
    public static class SSOWebSecurityConfigurerAdapter extends WebSecurityConfigurerAdapter {
        private Timer backgroundTaskTimer;
        private MultiThreadedHttpConnectionManager multiThreadedHttpConnectionManager;

        @PostConstruct
        public void init() {
            this.backgroundTaskTimer = new Timer(true);
            this.multiThreadedHttpConnectionManager = new MultiThreadedHttpConnectionManager();
        }

        @PreDestroy
        public void destroy() {
            this.backgroundTaskTimer.purge();
            this.backgroundTaskTimer.cancel();
            this.multiThreadedHttpConnectionManager.shutdown();
        }

        @Bean
        @Qualifier("idp-test")
        public ExtendedMetadataDelegate openiamExtendedMetadataProvider()
                throws MetadataProviderException, ResourceException {
            DefaultResourceLoader loader = new DefaultResourceLoader();
            String metadataLocation = "classpath:/sso/idp-test.xml";
            ResourceBackedMetadataProvider resourceBackedMetadataProvider =
                    new ResourceBackedMetadataProvider(backgroundTaskTimer,
                            new SpringResourceWrapperOpenSAMLResource(loader.getResource(metadataLocation.trim()))); // backgroundTaskTimer is null

            resourceBackedMetadataProvider.setParserPool(parserPool());
            ExtendedMetadataDelegate extendedMetadataDelegate =
                    new ExtendedMetadataDelegate(resourceBackedMetadataProvider, extendedMetadata());
            extendedMetadataDelegate.setMetadataTrustCheck(true);
            extendedMetadataDelegate.setMetadataRequireSignature(false);
            backgroundTaskTimer.purge();
            return extendedMetadataDelegate;
        }

        .....
    }
}

导致错误,

Caused by: org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.security.saml.metadata.ExtendedMetadataDelegate]: Circular reference involving containing bean 'ssoWebSecurityConfigurerAdapter' - consider declaring the factory method as static for independence from its containing instance. Factory method 'testExtendedMetadataProvider' threw exception; nested exception is java.lang.IllegalArgumentException: Task timer may not be null
    at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:189)
    at org.springframework.beans.factory.support.ConstructorResolver.instantiateUsingFactoryMethod(ConstructorResolver.java:588)
    ... 114 common frames omitted
Caused by: java.lang.IllegalArgumentException: Task timer may not be null
    at org.opensaml.saml2.metadata.provider.AbstractReloadingMetadataProvider.<init>(AbstractReloadingMetadataProvider.java:105)
    at org.opensaml.saml2.metadata.provider.ResourceBackedMetadataProvider.<init>(ResourceBackedMetadataProvider.java:76)

为什么上述案例中的backgroundTaskTimer不在PostConstruct

中设置

0 个答案:

没有答案