복붙노트

[SPRING] 스프링을 사용하여 프로그래밍 방식으로 속성 파일에 액세스 하시겠습니까?

SPRING

스프링을 사용하여 프로그래밍 방식으로 속성 파일에 액세스 하시겠습니까?

아래의 코드를 사용하여 프로퍼티 파일의 프로퍼티와 함께 ​​Spring 빈을 삽입한다.

<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
    <property name="locations" value="classpath:/my.properties"/>
</bean>

<bean id="blah" class="abc">
    <property name="path" value="${the.path}"/>
</bean>

프로그래밍 방식으로 속성에 액세스 할 수있는 방법이 있습니까? 의존성 주입없이 코드를 작성하려고합니다. 그래서 난 이런 식으로 몇 가지 코드를 갖고 싶습니다 :

PropertyPlaceholderConfigurer props = new PropertyPlaceholderConfigurer();
props.load("classpath:/my.properties");
props.get("path");

해결법

  1. ==============================

    1.PropertiesLoaderUtils는 어떻습니까?

    PropertiesLoaderUtils는 어떻습니까?

    Resource resource = new ClassPathResource("/my.properties");
    Properties props = PropertiesLoaderUtils.loadProperties(resource);
    
  2. ==============================

    2.CREDIT : 속성 파일을 다시 읽지 않고 Spring의 속성에 프로그래밍 방식으로 액세스

    CREDIT : 속성 파일을 다시 읽지 않고 Spring의 속성에 프로그래밍 방식으로 액세스

    스프링이 이미로드 한 것과 동일한 속성을 다시로드하지 않고도 봄에 프로그래밍 방식으로 속성에 액세스하는 멋진 구현을 발견했습니다. [또한 소스의 속성 파일 위치를 하드 코드 할 필요가 없습니다.]

    이러한 변경으로 코드는보다 깨끗하고 유지 보수가 용이 해 보입니다.

    개념은 매우 간단합니다. 스프링 기본 속성 자리 표시 자 (PropertyPlaceholderConfigurer)를 확장하고 로컬 변수에로드하는 속성을 캡처하기 만하면됩니다

    public class SpringPropertiesUtil extends PropertyPlaceholderConfigurer {
    
        private static Map<String, String> propertiesMap;
        // Default as in PropertyPlaceholderConfigurer
        private int springSystemPropertiesMode = SYSTEM_PROPERTIES_MODE_FALLBACK;
    
        @Override
        public void setSystemPropertiesMode(int systemPropertiesMode) {
            super.setSystemPropertiesMode(systemPropertiesMode);
            springSystemPropertiesMode = systemPropertiesMode;
        }
    
        @Override
        protected void processProperties(ConfigurableListableBeanFactory beanFactory, Properties props) throws BeansException {
            super.processProperties(beanFactory, props);
    
            propertiesMap = new HashMap<String, String>();
            for (Object key : props.keySet()) {
                String keyStr = key.toString();
                String valueStr = resolvePlaceholder(keyStr, props, springSystemPropertiesMode);
                propertiesMap.put(keyStr, valueStr);
            }
        }
    
        public static String getProperty(String name) {
            return propertiesMap.get(name).toString();
        }
    
    }
    

    사용 예제

    SpringPropertiesUtil.getProperty("myProperty")
    

    스프링 구성 변경

    <bean id="placeholderConfigMM" class="SpringPropertiesUtil">
        <property name="systemPropertiesModeName" value="SYSTEM_PROPERTIES_MODE_OVERRIDE"/>
        <property name="locations">
        <list>
            <value>classpath:myproperties.properties</value>
        </list>
        </property>
    </bean>
    

    희망이 당신이 가진 문제를 해결하는 데 도움이

  3. ==============================

    3.코드에서 자리 표시 자 값에 액세스하려는 경우 @Value 주석이 있습니다.

    코드에서 자리 표시 자 값에 액세스하려는 경우 @Value 주석이 있습니다.

    @Value("${settings.some.property}")
    String someValue;
    

    자리 표시 자에 액세스하려면 SPEL에서 다음 구문을 사용합니다.

    #('${settings.some.property}')
    

    SPEL이 꺼져있는보기에 구성을 표시하려면 다음 트릭을 사용할 수 있습니다.

    package com.my.app;
    
    import java.util.Collection;
    import java.util.Map;
    import java.util.Set;
    
    import org.springframework.beans.factory.BeanFactory;
    import org.springframework.beans.factory.BeanFactoryAware;
    import org.springframework.beans.factory.config.ConfigurableBeanFactory;
    import org.springframework.stereotype.Component;
    
    @Component
    public class PropertyPlaceholderExposer implements Map<String, String>, BeanFactoryAware {  
        ConfigurableBeanFactory beanFactory; 
    
        @Override
        public void setBeanFactory(BeanFactory beanFactory) {
            this.beanFactory = (ConfigurableBeanFactory) beanFactory;
        }
    
        protected String resolveProperty(String name) {
            String rv = beanFactory.resolveEmbeddedValue("${" + name + "}");
    
            return rv;
        }
    
        @Override
        public String get(Object key) {
            return resolveProperty(key.toString());
        }
    
        @Override
        public boolean containsKey(Object key) {
            try {
                resolveProperty(key.toString());
                return true;
            }
            catch(Exception e) {
                return false;
            }
        }
    
        @Override public boolean isEmpty() { return false; }
        @Override public Set<String> keySet() { throw new UnsupportedOperationException(); }
        @Override public Set<java.util.Map.Entry<String, String>> entrySet() { throw new UnsupportedOperationException(); }
        @Override public Collection<String> values() { throw new UnsupportedOperationException(); }
        @Override public int size() { throw new UnsupportedOperationException(); }
        @Override public boolean containsValue(Object value) { throw new UnsupportedOperationException(); }
        @Override public void clear() { throw new UnsupportedOperationException(); }
        @Override public String put(String key, String value) { throw new UnsupportedOperationException(); }
        @Override public String remove(Object key) { throw new UnsupportedOperationException(); }
        @Override public void putAll(Map<? extends String, ? extends String> t) { throw new UnsupportedOperationException(); }
    }
    

    노출을 사용하여 속성을보기에 표시합니다.

    <bean class="org.springframework.web.servlet.view.UrlBasedViewResolver" id="tilesViewResolver">
        <property name="viewClass" value="org.springframework.web.servlet.view.tiles2.TilesView"/>
        <property name="attributesMap">
            <map>
                <entry key="config">
                    <bean class="com.my.app.PropertyPlaceholderExposer" />
                </entry>
            </map>
        </property>
    </bean>
    

    그런 다음보기에서 다음과 같은 노출 된 속성을 사용하십시오.

    ${config['settings.some.property']}
    

    이 솔루션은 표준 자리 표시 자에 의존 할 수 있다는 이점이 있습니다. 문맥에 의해 삽입 된 구현 : property-placeholder 태그.

    이제 최종 메모로 모든 자리 표시 자 속성과 값을 캡처해야하는 경우 StringValueResolver를 통해 파이프를 통과시켜 자리 표시자가 속성 값 내에서 예상대로 작동하는지 확인해야합니다. 다음 코드는 그렇게 할 것입니다.

    package com.my.app;
    
    import java.util.Collection;
    import java.util.HashMap;
    import java.util.Map;
    import java.util.Properties;
    import java.util.Set;
    
    import org.springframework.beans.BeansException;
    import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
    import org.springframework.beans.factory.config.PropertyPlaceholderConfigurer;
    import org.springframework.util.StringValueResolver;
    
    public class AppConfig extends PropertyPlaceholderConfigurer implements Map<String, String> {
    
        Map<String, String> props = new HashMap<String, String>();
    
        @Override
        protected void processProperties(ConfigurableListableBeanFactory beanFactory, Properties props)
                throws BeansException {
    
            this.props.clear();
            for (Entry<Object, Object> e: props.entrySet())
                this.props.put(e.getKey().toString(), e.getValue().toString());
    
            super.processProperties(beanFactory, props);
        }
    
        @Override
        protected void doProcessProperties(ConfigurableListableBeanFactory beanFactoryToProcess,
                StringValueResolver valueResolver) {
    
            super.doProcessProperties(beanFactoryToProcess, valueResolver);
    
            for(Entry<String, String> e: props.entrySet())
                e.setValue(valueResolver.resolveStringValue(e.getValue()));
        }
    
        // Implement map interface to access stored properties
        @Override public Set<String> keySet() { return props.keySet(); }
        @Override public Set<java.util.Map.Entry<String, String>> entrySet() { return props.entrySet(); }
        @Override public Collection<String> values() { return props.values(); }
        @Override public int size() { return props.size(); }
        @Override public boolean isEmpty() { return props.isEmpty(); }
        @Override public boolean containsValue(Object value) { return props.containsValue(value); }
        @Override public boolean containsKey(Object key) { return props.containsKey(key); }
        @Override public String get(Object key) { return props.get(key); }
        @Override public void clear() { throw new UnsupportedOperationException(); }
        @Override public String put(String key, String value) { throw new UnsupportedOperationException(); }
        @Override public String remove(Object key) { throw new UnsupportedOperationException(); }
        @Override public void putAll(Map<? extends String, ? extends String> t) { throw new UnsupportedOperationException(); }
    }
    
  4. ==============================

    4.나는 이것을했고 그것은 효과가 있었다.

    나는 이것을했고 그것은 효과가 있었다.

    Properties props = PropertiesLoaderUtils.loadAllProperties("my.properties");
    PropertyPlaceholderConfigurer props2 = new PropertyPlaceholderConfigurer();
    props2.setProperties(props);
    

    그게 효과가있다.

  5. ==============================

    5.PropertiesFactoryBean을 통해 spring utils 또는 load 속성을 사용할 수도 있습니다.

    PropertiesFactoryBean을 통해 spring utils 또는 load 속성을 사용할 수도 있습니다.

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

    또는:

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

    그런 다음 응용 프로그램에서 다음을 사용하여 선택할 수 있습니다.

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

    추가적으로 당신의 설정에서 다음 속성들을 사용하십시오 :

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

    이것은 또한 문서에 있습니다 : http://docs.spring.io/spring/docs/current/spring-framework-reference/htmlsingle/#xsd-config-body-schemas-util-properties

  6. ==============================

    6.아래와 같은 클래스 만들기

    아래와 같은 클래스 만들기

        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);
    
            }
    
        }
    

    그런 다음 속성 사용에 액세스하려는 모든 곳

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

    접근 자 클래스에 getter 및 setter 작성

        public Environment getEnvironment() {
                return environment;
            }`enter code here`
    
            public void setEnvironment(Environment environment) {
                this.environment = environment;
            }
    
  7. ==============================

    7.다음은 또 다른 샘플입니다.

    다음은 또 다른 샘플입니다.

    XmlBeanFactory factory = new XmlBeanFactory(new FileSystemResource("beans.xml"));
    PropertyPlaceholderConfigurer cfg = new PropertyPlaceholderConfigurer();
    cfg.setLocation(new FileSystemResource("jdbc.properties"));
    cfg.postProcessBeanFactory(factory);
    
  8. ==============================

    8.이 도움이 :

    이 도움이 :

    ApplicationContextUtils.getApplicationContext().getEnvironment()
    
  9. ==============================

    9.아시다시피 Spring의 최신 버전은 PropertyPlaceholderConfigurer를 사용하지 않고 PropertySourcesPlaceholderConfigurer라는 또 다른 악몽 같은 구조를 사용합니다. 코드에서 해결 된 속성을 얻으 려하고 Spring 팀이 오래 전에이 작업을 수행해 주셨으면 좋겠다면이 게시물에 투표하십시오! ... 이것은 당신이 새로운 방식으로하는 방법이기 때문에 :

    아시다시피 Spring의 최신 버전은 PropertyPlaceholderConfigurer를 사용하지 않고 PropertySourcesPlaceholderConfigurer라는 또 다른 악몽 같은 구조를 사용합니다. 코드에서 해결 된 속성을 얻으 려하고 Spring 팀이 오래 전에이 작업을 수행해 주셨으면 좋겠다면이 게시물에 투표하십시오! ... 이것은 당신이 새로운 방식으로하는 방법이기 때문에 :

    PropertySourcesPlaceholderConfigurer 서브 클래스 :

    public class SpringPropertyExposer extends PropertySourcesPlaceholderConfigurer {
    
        private ConfigurableListableBeanFactory factory;
    
        /**
         * Save off the bean factory so we can use it later to resolve properties
         */
        @Override
        protected void processProperties(ConfigurableListableBeanFactory beanFactoryToProcess,
                final ConfigurablePropertyResolver propertyResolver) throws BeansException {
            super.processProperties(beanFactoryToProcess, propertyResolver);
    
            if (beanFactoryToProcess.hasEmbeddedValueResolver()) {
                logger.debug("Value resolver exists.");
                factory = beanFactoryToProcess;
            }
            else {
                logger.error("No existing embedded value resolver.");
            }
        }
    
        public String getProperty(String name) {
            Object propertyValue = factory.resolveEmbeddedValue(this.placeholderPrefix + name + this.placeholderSuffix);
            return propertyValue.toString();
        }
    }
    

    이를 사용하려면 @Configuration에서 하위 클래스를 사용하고 나중에 참조 할 수 있도록 하위 참조를 저장하십시오.

    @Configuration
    @ComponentScan
    public class PropertiesConfig {
    
        public static SpringPropertyExposer commonEnvConfig;
    
        @Bean(name="commonConfig")
        public static PropertySourcesPlaceholderConfigurer commonConfig() throws IOException {
            commonEnvConfig = new SpringPropertyExposer(); //This is a subclass of the return type.
            PropertiesFactoryBean commonConfig = new PropertiesFactoryBean();
            commonConfig.setLocation(new ClassPathResource("META-INF/spring/config.properties"));
            try {
                commonConfig.afterPropertiesSet();
            }
            catch (IOException e) {
                e.printStackTrace();
                throw e;
            }
            commonEnvConfig.setProperties(commonConfig.getObject());
            return commonEnvConfig;
        }
    }
    

    용법:

    Object value = PropertiesConfig.commonEnvConfig.getProperty("key.subkey");
    
  10. ==============================

    10.그러면 중첩 된 속성이 해결됩니다.

    그러면 중첩 된 속성이 해결됩니다.

    public class Environment extends PropertyPlaceholderConfigurer {
    
    /**
     * Map that hold all the properties.
     */
    private Map<String, String> propertiesMap; 
    
    /**
     * Iterate through all the Property keys and build a Map, resolve all the nested values before building the map.
     */
    @Override
    protected void processProperties(ConfigurableListableBeanFactory beanFactory, Properties props) throws BeansException {
        super.processProperties(beanFactory, props);
    
        propertiesMap = new HashMap<String, String>();
        for (Object key : props.keySet()) {
            String keyStr = key.toString();
            String valueStr = beanFactory.resolveEmbeddedValue(placeholderPrefix + keyStr.trim() + DEFAULT_PLACEHOLDER_SUFFIX);
            propertiesMap.put(keyStr, valueStr);
        }
    } 
    
    /**
     * This method gets the String value for a given String key for the property files.
     * 
     * @param name - Key for which the value needs to be retrieved.
     * @return Value
     */
    public String getProperty(String name) {
        return propertiesMap.get(name).toString();
    }
    
  11. ==============================

    11.이 게시물은 또한 속성에 액세스하는 방법을 설명합니다. http://maciej-miklas.blogspot.de/2013/07/spring-31-programmatic-access-to.html

    이 게시물은 또한 속성에 액세스하는 방법을 설명합니다. http://maciej-miklas.blogspot.de/2013/07/spring-31-programmatic-access-to.html

    Spring bean을 통해 spring 속성 - 자리 표시 자에 의해로드 된 속성에 액세스 할 수 있습니다.

    @Named
    public class PropertiesAccessor {
    
        private final AbstractBeanFactory beanFactory;
    
        private final Map<String,String> cache = new ConcurrentHashMap<>();
    
        @Inject
        protected PropertiesAccessor(AbstractBeanFactory beanFactory) {
            this.beanFactory = beanFactory;
        }
    
        public  String getProperty(String key) {
            if(cache.containsKey(key)){
                return cache.get(key);
            }
    
            String foundProp = null;
            try {
                foundProp = beanFactory.resolveEmbeddedValue("${" + key.trim() + "}");
                cache.put(key,foundProp);
            } catch (IllegalArgumentException ex) {
               // ok - property was not found
            }
    
            return foundProp;
        }
    }
    
  12. ==============================

    12.

    create .properties file in classpath of your project and add path configuration in xml`<context:property-placeholder location="classpath*:/*.properties" />`
    

    서블릿 -create.xml에서 그 파일을 사방에 직접 사용할 수있다.

  13. ==============================

    13.스프링 구성 파일의 아래 코드를 사용하여 응용 프로그램의 클래스 경로에서 파일을로드하십시오.

    스프링 구성 파일의 아래 코드를 사용하여 응용 프로그램의 클래스 경로에서 파일을로드하십시오.

     <context:property-placeholder
        ignore-unresolvable="true" ignore-resource-not-found="false" location="classpath:property-file-name" />
    
  14. from https://stackoverflow.com/questions/1771166/access-properties-file-programmatically-with-spring by cc-by-sa and MIT license