springboot 从属性文件读取问题

springboot issues reading from properties file

我正在尝试读取 Util class 中的 属性 个值。 我尝试了 Environment@Value。但是它获得了这些字段的 Null 值。

@SpringBootApplication
@EnableJpaAuditing
public class EmployeeApplication {

    @PostConstruct
    private void init() {
        System.out.println("AppInitializator initialization logic ...");
        new Util().getTime();
    }

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

public class Util {

    @Autowired
    private Environment env;

    @Value("${defaultTimeFormat}")
    private String defaultTimeFormat;

    public void getTime(){
        System.out.println(defaultTimeFormat);
        System.out.println(env.getProperty("defaultTimeFormat"));
        // method logic here
    }
}

我通过将 Util 设置为 @Service class 并将其依赖项注入 EmployeeApplication 来解决问题,如下所示。

@SpringBootApplication
@EnableJpaAuditing
public class EmployeeApplication {
    @Autowired
    Util util;

    @PostConstruct
    private void init() {
        System.out.println("AppInitializator initialization logic ...");
        util.getTime();
    }

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


@Service
public class Util {

    @Autowired
    private Environment env;

    @Value("${defaultTimeFormat}")
    private String defaultTimeFormat;

    public void getTime(){
        System.out.println(defaultTimeFormat);
        System.out.println(env.getProperty("defaultTimeFormat"));
        // method logic here
    }
}

但是spring-boot中的方法是正确的吗?我需要让它 Service/Component/Repository 来读取 属性 文件吗?

是的,您需要使用 @Component 或其他注释之一来注释 class,以便它被 Spring Boot 实例化。

只有生命周期由 Spring 控制的对象才能获得由 Spring 注入的值,如果您添加 @Value 注释,就会发生这种情况。

在应用程序启动期间 Spring 使用您已注释的 classes 的单例实例填充应用程序上下文。 只有这些 classes 将获得通过 @Autowired@Value.

注入的值

对于初学者来说,不鼓励使用 @Value 直接将 属性 值注入所需的 class,因为它会降低可维护性。随着注入值数量的增加,跟踪它们变得很困难。而不是使用 AppConfiguration class 将这些值映射到最初是 appreciated.This 意味着您可以将所有配置累积在一个地方并稍后在您的代码中需要的地方使用它。

正如@fap 所说,为了使用 @Value 注释从 yml/property 自动装配或注入一个值,class 的 bean 创建需要由 Spring 。在你的 Util class 中,即使你正在自动装配 bean 并使用 @Value 注释来注入 defaultTimeFormat 的值,你也没有提供 spring为 class 创建 bean 的控件。

Spring 在 bean 创建阶段处理依赖注入,因此由于未向 spring 提供该控制,因此该值未得到解析。您可以做的是使用 @Component 注释或 @Bean 注释为 Util class 创建一个 bean,以便 spring 可以控制 bean 创建,例如:

方法一:

@Configuration
class GeneralConfiguration {

@Bean
public Util returnUtilBean() {
  return new Util();
}

}

或者@fab 建议使用 @Component 注释,以便 spring 自动为你的 Util 创建一个 bean class :

方法二:

@Component
public class Util {

    @Autowired
    private Environment env;

    @Value("${defaultTimeFormat}")
    private String defaultTimeFormat;

    public void getTime(){
        System.out.println(defaultTimeFormat);
        System.out.println(env.getProperty("defaultTimeFormat"));
        // method logic here
    }
}

@Service 注释是 @Component 注释的 刻板化专业化 ,这就是 它对您有用的原因。

Property values can be injected directly into your beans by using the @Value annotation, accessed through Spring’s Environment abstraction, or be bound to structured objects through @ConfigurationProperties.