Reading values from application.properties Spring Boot

后端 未结 10 1689
无人及你
无人及你 2021-01-18 10:46

My Spring boot app has this application structure:

  • src
    • main
      • java
      • resources
        • application.properties
      <
10条回答
  •  隐瞒了意图╮
    2021-01-18 11:42

    create you beans

    @Configuration
    @PropertySource("classpath:application.properties")
    public class ApplicationBeansConfig {
    
        @Autowired
        Environment env;
    
        @Bean
        public IApplicationBeanService getService(){
            return new ApplicationBeansService(env);
        }
    }
    

    and then in service costructor

    @Service
    public class ApplicationBeansService implements IApplicationBeanService {
    ...
       public ApplicationBeansService(Environment env){
          ...
          String value = env.getProperty("your.value")
          ...
       }
    ...
    }
    

    don't forget fill your application.properties:

    your.value = some-string-value
    

提交回复
热议问题