0

在 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>

应用程序属性

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.
4

1 回答 1

0

请参阅此处在 spring boot 2.01 中不接受自动装配 Null bean

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

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

喜欢

@Bean(name = "hostName")
public String getName() {
    try {
        if(enableHostName)
            return InetAddress.getLocalHost().getHostName();
    } catch (UnknownHostException e) {
        return "Unknown";
    }
    return "Host name not enabled";
}
于 2018-04-23T09:42:53.190 回答