Spring JavaConfig for java.util.Properties field

后端 未结 6 2062
面向向阳花
面向向阳花 2021-02-13 02:07


Can you please tell me how to use Spring Javaconfig to directly load/autowire a properties file to a java.util.Properties field?

Thanks!

Later edit - st

6条回答
  •  心在旅途
    2021-02-13 02:46

    declare a PropertiesFactoryBean.

    @Bean
    public PropertiesFactoryBean mailProperties() {
        PropertiesFactoryBean bean = new PropertiesFactoryBean();
        bean.setLocation(new ClassPathResource("mail.properties"));
        return bean;
    }
    

    Legacy code had following config

    
      
    
    

    Converting that to Java config is super easy as shown above.

提交回复
热议问题