How to pass a Map with application.properties

后端 未结 4 1326
失恋的感觉
失恋的感觉 2020-12-14 08:29

I have implemented some authorization in a webservice that needs be configured.

Currently the user/password combo is hardcoded into the bean configuration. I would l

相关标签:
4条回答
  • 2020-12-14 08:49

    A java.util.Properties object is already a Map, actually a HashTable which in turn implements Map.

    So when you create a properties file (lets name it users.properties) you should be able to load it using a PropertiesFactoryBean or <util:properties /> and inject it into your class.

    test1=test1
    test2=test2
    

    Then do something like

    <util:properties location="classpath:users.properties" id="users" />
    
    <bean id="BasicAuthorizationInterceptor" class="com.test.BasicAuthAuthorizationInterceptor">
        <property name="users" ref="users" />
    </bean>
    

    Although if you have a Map<String, String> as a type of the users property it might fail... I wouldn't put them in the application.properties file. But that might just be me..

    0 讨论(0)
  • 2020-12-14 08:58

    you can use @Value.

    Properties file:

    users={test1:'test1',test2:'test2'}
    

    Java code:

    @Value("#{${users}}")
    private Map<String,String> users;
    
    0 讨论(0)
  • 2020-12-14 08:59

    I think you are looking for something similar

    http://www.codejava.net/frameworks/spring/reading-properties-files-in-spring-with-propertyplaceholderconfigurer-bean

    You can pick values from .properties similarly and assign it to your map.

    0 讨论(0)
  • 2020-12-14 09:02

    You can use @ConfigurationProperties to have values from application.properties bound into a bean. To do so you annotate your @Bean method that creates the bean:

    @Bean
    @ConfigurationProperties
    public BasicAuthAuthorizationInterceptor interceptor() {
        return new BasicAuthAuthorizationInterceptor();
    }
    

    As part of the bean's initialisation, any property on BasicAuthAuthorizationInterceptor will be set based on the application's environment. For example, if this is your bean's class:

    public class BasicAuthAuthorizationInterceptor {
    
        private Map<String, String> users = new HashMap<String, String>();
    
        public Map<String, String> getUsers() {
            return this.users;
        }
    }
    

    And this is your application.properties:

    users.alice=alpha
    users.bob=bravo
    

    Then the users map will be populated with two entries: alice:alpha and bob:bravo.

    Here's a small sample app that puts this all together:

    import java.util.HashMap;
    import java.util.Map;
    
    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
    import org.springframework.boot.context.properties.ConfigurationProperties;
    import org.springframework.boot.context.properties.EnableConfigurationProperties;
    import org.springframework.context.annotation.Bean;
    import org.springframework.context.annotation.Configuration;
    
    @Configuration
    @EnableAutoConfiguration
    @EnableConfigurationProperties
    public class Application {
    
        public static void main(String[] args) throws Exception {
            System.out.println(SpringApplication.run(Application.class, args)
                    .getBean(BasicAuthAuthorizationInterceptor.class).getUsers());
        }
    
        @Bean
        @ConfigurationProperties
        public BasicAuthAuthorizationInterceptor interceptor() {
            return new BasicAuthAuthorizationInterceptor();
        }
    
        public static class BasicAuthAuthorizationInterceptor {
    
            private Map<String, String> users = new HashMap<String, String>();
    
            public Map<String, String> getUsers() {
                return this.users;
            }
        }
    }
    

    Take a look at the javadoc for ConfigurationProperties for more information on its various configuration options. For example, you can set a prefix to divide your configuration into a number of different namespaces:

    @ConfigurationProperties(prefix="foo")
    

    For the binding to work, you'd then have to use the same prefix on the properties declared in application.properties:

    foo.users.alice=alpha
    foo.users.bob=bravo
    
    0 讨论(0)
提交回复
热议问题