Spring Boot application.properties value not populating

后端 未结 8 559
孤街浪徒
孤街浪徒 2020-12-04 11:41

I have a very simple Spring Boot app that I\'m trying to get working with some externalised configuration. I\'ve tried to follow the information on the spring boot documenta

8条回答
  •  无人及你
    2020-12-04 12:34

    follow these steps. 1:- create your configuration class like below you can see

    import org.springframework.context.annotation.Bean;
    import org.springframework.context.annotation.Configuration;
    import org.springframework.beans.factory.annotation.Value;
    
    @Configuration
    public class YourConfiguration{
    
        // passing the key which you set in application.properties
        @Value("${some.pro}")
        private String somePro;
    
       // getting the value from that key which you set in application.properties
        @Bean
        public String getsomePro() {
            return somePro;
        }
    }
    

    2:- when you have a configuration class then inject in the variable from a configuration where you need.

    @Component
    public class YourService {
    
        @Autowired
        private String getsomePro;
    
        // now you have a value in getsomePro variable automatically.
    }
    

提交回复
热议问题