在Spring引导版本更新到2.0.1之后,Bean初始化条件失败

时间:2018-04-11 10:18:39

标签: spring spring-mvc spring-boot spring-bean spring-framework-beans

无法启动项目spring-boot-starter-parent版本从 1.5.9.RELEASE 更新为 2.0.1.RELEASE 。我们发现当我们有一个条件bean初始化方法时,应用程序无法启动。

的pom.xml

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <!-- Previous version -->
    <!-- <version>1.5.9.RELEASE</version> -->
    <!-- Updated version -->
    <version>2.0.1.RELEASE</version>
    <relativePath />
</parent>

application.properties

host.enabled=false 

DemoApplication.java

@SpringBootApplication
public class DemoApplication {

    public static void main(String[] args) {
        SpringApplication.run(DemoApplication.class, args);
    }
}

DemoClass.java

@Component
@EnableScheduling
public class DemoClass {

    @Value("${host.enabled}")
    private boolean enableHostName;

    @Bean(name = "hostName")
    public String getName() {
        try {
            if(enableHostName)
                return InetAddress.getLocalHost().getHostName();
        } catch (UnknownHostException e) {
            return "Unknown";
        }
        return null;
        //return "";
    }

    @Autowired
    @Qualifier("hostName")
    String hostName;

    @Scheduled(fixedRate = 3000)
    public void print(){
        System.out.println(hostName);
    }
}

控制台日志:

2018-04-11 17:59:32.584  WARN 24120 --- [           main] ConfigServletWebServerApplicationContext : Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'demoClass': Unsatisfied dependency expressed through field 'hostName'; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'java.lang.String' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true), @org.springframework.beans.factory.annotation.Qualifier(value=hostName)}
2018-04-11 17:59:32.592  INFO 24120 --- [           main] o.apache.catalina.core.StandardService   : Stopping service [Tomcat]
2018-04-11 17:59:32.620  INFO 24120 --- [           main] ConditionEvaluationReportLoggingListener : 

Error starting ApplicationContext. To display the conditions report re-run your application with 'debug' enabled.
2018-04-11 17:59:32.917 ERROR 24120 --- [           main] o.s.b.d.LoggingFailureAnalysisReporter   : 

***************************
APPLICATION FAILED TO START
***************************
Description:
Field hostName in com.vjs.demo.DemoClass required a bean of type 'java.lang.String' that could not be found.

Action:
Consider defining a bean of type 'java.lang.String' in your configuration.

1 个答案:

答案 0 :(得分:1)

在spring boot 2.01中看到这里并不接受自动装配Null bean

因为在您的属性文件中,该值设置为false,因此bean @Bean(name = "hostName")将始终返回null String,

将返回值更改为空或sipmle值将解决问题

喜欢

@Bean(name = "hostName")
public String getName() {
    try {
        if(enableHostName)
            return InetAddress.getLocalHost().getHostName();
    } catch (UnknownHostException e) {
        return "Unknown";
    }
    return "Host name not enabled";
}