返回null以外的任何内容

时间:2019-01-07 09:52:42

标签: java spring spring-config

在添加sslEnabled配置之前,我的代码一直有效。由于我将“ false”作为sslEnabled的默认值,因此它将返回null。

这个空值导致我以下情况:

  

原因:org.springframework.beans.factory.BeanNotOfRequiredTypeException:名为“ servletContainer”的Bean的类型应为“ org.springframework.boot.web.servlet.server.ServletWebServerFactory”,但实际上是“ org”类型。 springframework.beans.factory.support.NullBean'

我尝试return new ServletWebServerFactory();,但说无法实例化ServletWebServerFactory类型

@Configuration
public class ConnectorConfig {

@Value("${security.ssl.enabled}")
private boolean sslEnabled;

/**
 * Servlet container.
 *
 * @return the servlet web server factory
 */
@Bean
public ServletWebServerFactory servletContainer() {
    if(sslEnabled) {
        TomcatServletWebServerFactory tomcat = new TomcatServletWebServerFactory() {
            @Override
            protected void postProcessContext(Context context) {
                SecurityConstraint securityConstraint = new SecurityConstraint();
                securityConstraint.setUserConstraint("CONFIDENTIAL");
                SecurityCollection collection = new SecurityCollection();
                collection.addPattern("/*");
                securityConstraint.addCollection(collection);
                context.addConstraint(securityConstraint);
            }
        };
        tomcat.addAdditionalTomcatConnectors(redirectConnector());
        return tomcat;
    }
    return null;
}
....

基本上,如果标志sslEnabled为假,我不想启用SSL,并且我想跳过该配置

1 个答案:

答案 0 :(得分:2)

也许最简单的方法是声明一个包装类并注入该类。就像创建以下bean:

public class ServletWebServerFactoryWrapper {

    private ServletWebServerFactory servletWebServerFactory;

    public ServletWebServerFactoryWrapper(ServletWebServerFactory servletWebServerFactory){
        this.servletWebServerFactory = servletWebServerFactory;
    }

    public static ServletWebServerFactoryWrapper getWrapper(ServletWebServerFactory servletWebServerFactory){
        return new ServletWebServerFactoryWrapper(servletWebServerFactory);
    }

    public ServletWebServerFactory getFactory(){
        return servletWebServerFactory;
    }
}

并在您的代码中返回它:

@Bean
public ServletWebServerFactoryWrapper servletContainer() {
    if(sslEnabled) {
        TomcatServletWebServerFactory tomcat = new TomcatServletWebServerFactory() {
            @Override
            protected void postProcessContext(Context context) {
                SecurityConstraint securityConstraint = new SecurityConstraint();
                securityConstraint.setUserConstraint("CONFIDENTIAL");
                SecurityCollection collection = new SecurityCollection();
                collection.addPattern("/*");
                securityConstraint.addCollection(collection);
                context.addConstraint(securityConstraint);
            }
        };
        tomcat.addAdditionalTomcatConnectors(redirectConnector());
        return ServletWebServerFactoryWrapper.getWrapper(tomcat);
    }
    return ServletWebServerFactoryWrapper.getWrapper(null);
}

我不知道您是否使用Optional<ServletWebServerFactory>(Java 8),但是您可以尝试,它应该与此相当。您的方法应如下:

@Bean
public Optional<ServletWebServerFactory> servletContainer() {
    if(sslEnabled) {
        TomcatServletWebServerFactory tomcat = new TomcatServletWebServerFactory() {
            @Override
            protected void postProcessContext(Context context) {
                SecurityConstraint securityConstraint = new SecurityConstraint();
                securityConstraint.setUserConstraint("CONFIDENTIAL");
                SecurityCollection collection = new SecurityCollection();
                collection.addPattern("/*");
                securityConstraint.addCollection(collection);
                context.addConstraint(securityConstraint);
            }
        };
        tomcat.addAdditionalTomcatConnectors(redirectConnector());
        return Optional.of(tomcat);
    }
    return Optional.empty();
}

请记住,这最后一种选择仅适用于Java 8或更高版本。