How to read data from java properties file using Spring Boot

前端 未结 4 1410
青春惊慌失措
青春惊慌失措 2020-12-01 07:30

I have a spring boot application and I want to read some variable from my application.properties file. In fact below codes do that. But I think there is a good

相关标签:
4条回答
  • 2020-12-01 08:16

    I have created following class

    ConfigUtility.java

    @Configuration
    public class ConfigUtility {
    
        @Autowired
        private Environment env;
    
        public String getProperty(String pPropertyKey) {
            return env.getProperty(pPropertyKey);
        }
    } 
    

    and called as follow to get application.properties value

    myclass.java

    @Autowired
    private ConfigUtility configUtil;
    
    public AppResponse getDetails() {
    
      AppResponse response = new AppResponse();
        String email = configUtil.getProperty("emailid");
        return response;        
    }
    

    application.properties

    emailid=sunny@domain.com

    unit tested, working as expected...

    0 讨论(0)
  • 2020-12-01 08:23

    We can read properties file in spring boot using 3 way

    1. Read value from application.properties Using @Value

    map key as

    public class EmailService {
    
     @Value("${email.username}")
     private String username;
    

    }

    2. Read value from application.properties Using @ConfigurationProperties

    In this we will map prefix of key using ConfigurationProperties and key name is same as field of class

      @Component
       @ConfigurationProperties("email")
        public class EmailConfig {
    
            private String   username;
        }
    

    3. Read application.properties Using using Environment object

    public class EmailController {

    @Autowired
    private Environment env;
    
    @GetMapping("/sendmail")
    public void sendMail(){     
        System.out.println("reading value from application properties file  using Environment ");
        System.out.println("username ="+ env.getProperty("email.username"));
        System.out.println("pwd ="+ env.getProperty("email.pwd"));
    }
    

    Reference : how to read value from application.properties in spring boot

    0 讨论(0)
  • 2020-12-01 08:30

    You can use @PropertySource to externalize your configuration to a properties file. There is number of way to do get properties:

    1. Assign the property values to fields by using @Value with PropertySourcesPlaceholderConfigurer to resolve ${} in @Value:

    @Configuration
    @PropertySource("file:config.properties")
    public class ApplicationConfiguration {
    
        @Value("${gMapReportUrl}")
        private String gMapReportUrl;
    
        @Bean
        public static PropertySourcesPlaceholderConfigurer propertyConfigInDev() {
            return new PropertySourcesPlaceholderConfigurer();
        }
    
    }
    

    2. Get the property values by using Environment:

    @Configuration
    @PropertySource("file:config.properties")
    public class ApplicationConfiguration {
    
        @Autowired
        private Environment env;
    
        public void foo() {
            env.getProperty("gMapReportUrl");
        }
    
    }
    

    Hope this can help

    0 讨论(0)
  • 2020-12-01 08:34

    i would suggest the following way:

    @PropertySource(ignoreResourceNotFound = true, value = "classpath:otherprops.properties")
    @Controller
    public class ClassA {
        @Value("${myName}")
        private String name;
    
        @RequestMapping(value = "/xyz")
        @ResponseBody
        public void getName(){
            System.out.println(name);
        }
    }
    

    Here your new properties file name is "otherprops.properties" and the property name is "myName". This is the simplest implementation to access properties file in spring boot version 1.5.8.

    0 讨论(0)
提交回复
热议问题