Access properties file programmatically with Spring?

前端 未结 15 1615
说谎
说谎 2020-11-27 09:37

We use the code below to inject Spring beans with properties from a properties file.



        
相关标签:
15条回答
  • 2020-11-27 10:00

    Create a class like below

        package com.tmghealth.common.util;
    
        import java.util.Properties;
    
        import org.springframework.beans.BeansException;
    
        import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
    
        import org.springframework.beans.factory.config.PropertyPlaceholderConfigurer;
    
        import org.springframework.context.annotation.Configuration;
    
        import org.springframework.context.annotation.PropertySource;
    
        import org.springframework.stereotype.Component;
    
    
        @Component
        @Configuration
        @PropertySource(value = { "classpath:/spring/server-urls.properties" })
        public class PropertiesReader extends PropertyPlaceholderConfigurer {
    
            @Override
            protected void processProperties(
                    ConfigurableListableBeanFactory beanFactory, Properties props)
                    throws BeansException {
                super.processProperties(beanFactory, props);
    
            }
    
        }
    

    Then wherever you want to access a property use

        @Autowired
            private Environment environment;
        and getters and setters then access using 
    
        environment.getProperty(envName
                        + ".letter.fdi.letterdetails.restServiceUrl");
    

    -- write getters and setters in the accessor class

        public Environment getEnvironment() {
                return environment;
            }`enter code here`
    
            public void setEnvironment(Environment environment) {
                this.environment = environment;
            }
    
    0 讨论(0)
  • 2020-11-27 10:01

    You can also use either the spring utils, or load properties via the PropertiesFactoryBean.

    <util:properties id="myProps" location="classpath:com/foo/myprops.properties"/>
    

    or:

    <bean id="myProps" class="org.springframework.beans.factory.config.PropertiesFactoryBean">
      <property name="location" value="classpath:com/foo/myprops.properties"/>
    </bean>
    

    Then you can pick them up in your application with:

    @Resource(name = "myProps")
    private Properties myProps;
    

    and additionally use these properties in your config:

    <context:property-placeholder properties-ref="myProps"/>
    

    This is also in the docs: http://docs.spring.io/spring/docs/current/spring-framework-reference/htmlsingle/#xsd-config-body-schemas-util-properties

    0 讨论(0)
  • 2020-11-27 10:03

    Here is another sample .

    XmlBeanFactory factory = new XmlBeanFactory(new FileSystemResource("beans.xml"));
    PropertyPlaceholderConfigurer cfg = new PropertyPlaceholderConfigurer();
    cfg.setLocation(new FileSystemResource("jdbc.properties"));
    cfg.postProcessBeanFactory(factory);
    
    0 讨论(0)
提交回复
热议问题