Return 除 null 以外的任何内容

Return anything other than null

在添加 sslEnabled 配置之前,我的代码一直有效。由于我将 "false" 作为 sslEnabled 的默认值,它将 return null.

那个 null 导致我出现以下情况:

Caused by: org.springframework.beans.factory.BeanNotOfRequiredTypeException: Bean named 'servletContainer' is expected to be of type 'org.springframework.boot.web.servlet.server.ServletWebServerFactory' but was actually of type '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,我想跳过该配置

也许最简单的事情就是声明一个包装器 class 并注入那个 class。就像创建以下 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 或更高级别。