Reading values from application.properties Spring Boot

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

My Spring boot app has this application structure:

  • src
    • main
      • java
      • resources
        • application.properties
      <
10条回答
  •  太阳男子
    2021-01-18 11:28

    It can be achieved in multiple ways, refer below.

    @Configuration
    @PropertySource("classpath:application.properties")
    public class EntityManager {
    
        @Value("${language}")
        private static String newLang;
    
        @Bean
        public static PropertySourcesPlaceholderConfigurer propertyConfigInDev() {
            return new PropertySourcesPlaceholderConfigurer();
        }
    
    }
    

    OR

    @Configuration
    @PropertySource("classpath:application.properties")
    public class EntityManager {
    
        @Autowired
        private Environment env;
    
        public void readProperty() {
            env.getProperty("language");
        }
    
    }
    

提交回复
热议问题