【问题标题】:Reading values from property files in Spring MVC using java based anotations使用基于 java 的注解从 Spring MVC 中的属性文件中读取值
【发布时间】:2015-10-16 08:40:48
【问题描述】:

我在我的 Spring MVC 项目中使用 Spring 3.1+ 版本。我已经阅读了一堆关于如何在代码中使用属性文件的文章,我尝试了一些解决方案,但没有一个是有效的。例如这样的http://websystique.com/spring/spring-propertysource-value-annotations-example/

以下是位于 src/main/resources 中的通信属性

doniiesuser.url=localhost
doniiesuser.port=8080

配置类,我用 spring mvc 配置重用了这个类,我尝试只为属性源添加一个不同的类。

<!-- language: java -->
package com.doniies.doniiesweb.config;

import org.springframework.context.annotation.Bean;

@Configuration
@EnableWebMvc
@ComponentScan(basePackages = {"com.doniies.doniiesweb.controller", "com.doniies.doniiesweb.services"})
@PropertySource(value = {"classpath:communication.properties"})
public class MVCConfig extends WebMvcConfigurerAdapter{

 // more beans and code

    /**
     * Ensures that placeholders are replaced with property values
     */
    @Bean
    public static PropertySourcesPlaceholderConfigurer propertyPlaceHolderConfigurer() {
        return new PropertySourcesPlaceholderConfigurer();
    }
}

添加属性文件读取的新更改是 @PropertySource 和 PropertySourcesPlaceholderConfigurer 方法

最后,我想在 @Service 或 @Controller 类中使用此属性(尝试 2 种解决方案,使用占位符 ${key} 和 Environment 类)

@Service
public class UserRepositoryUserDetailsService implements UserDetailsService{

    private static Logger logger = LoggerFactory.getLogger(UserRepositoryUserDetailsService.class);

    @Value( "${doniiesuser.url}" )
    private String doniiesuserUrl;

    @Value( "${doniiesuser.port}" )
    private String doniiesuserPort;

    @Autowired
    private Environment environment;

    //More things...

    @Override
    public UserDetails loadUserByUsername(String username)
            throws UsernameNotFoundException {

        System.out.println("WEB - placeholder @value: " + doniiesuserUrl);
        System.out.println("WEB - using env @value: " + environment.getProperty("doniiesuser.port"));

        logger.info("WEB - placeholder @value: " + doniiesuserUrl);
        logger.info("WEB - using env @value: " + environment.getProperty("doniiesuser.port"));

    }

    //....
}

当我在本地 tomcat 服务器上运行它时,日志会显示以下内容:

WEB - placeholder @value: ${doniiesuser.url}
WEB - using env @value: null
INFO - WEB - placeholder @value: ${doniiesuser.url}
INFO - WEB - using env @value: null

我错过了什么吗?有什么不明白的地方吗?

谢谢

【问题讨论】:

    标签: java spring spring-mvc config


    【解决方案1】:

    我猜想在 5 个月后你一定已经以某种方式解决了这个问题。

    我建议您检查您的“WebAppInitializer”是否正在加载您的所有配置文件,您可以这样做:

    AnnotationConfigWebApplicationContext rootContext = new AnnotationConfigWebApplicationContext();
    rootContext.scan("com.doniies.doniiesweb.config");
    

    要检查的另一件事是您的“PropertiesConfiguration”文件,以防它引起一些冲突。

    我已经使用 Spring 4.2 测试了一个配置,类似于您发布的文章中的配置,它工作正常。

    希望这会有所帮助!

    【讨论】:

      猜你喜欢
      • 2015-05-03
      • 2018-08-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-07-09
      • 1970-01-01
      • 2014-01-19
      • 2018-05-24
      相关资源
      最近更新 更多