为基于java注释的配置自定义@PropertyResource处理行为

时间:2012-01-27 16:08:12

标签: spring annotations

我希望自定义属性源的处理,同时使用基于初始化Spring应用程序的java注释。

@Configuration
@PropertySource("ldap.properties")
@Repository
public class LdapDao {
...
    @Autowired
    public void setEnv(Environment env) throws NamingException {
    this.url = env.getProperty("url").trim();
    this.user = env.getProperty("user").trim();
    this.password = env.getProperty("password).trim();

    this.initializeLdapContext();
    }
...
}

在这种情况下,spring将在类路径中查找属性源。如果属性源声明为:

@PropertySource("file:/${conf.dir}/ldap.properties")

在系统属性“conf.dir”指定的目录下搜索ldap.properties。

我需要首先在系统属性“conf.dir”指定的目录下搜索属性资源的行为。如果在那里找不到它,它的位置默认为classpath。

有关如何实现此行为的任何建议?

1 个答案:

答案 0 :(得分:3)

使用此

@PropertySource({"ldap.properties", "file:/${conf.dir}/ldap.properties"})

你会得到最后一个,只需添加你的propConfig代码

PropertySourcesPlaceholderConfigurer propConfig = new PropertySourcesPlaceholderConfigurer();
//.....
propConfig.setIgnoreResourceNotFound(true);
propConfig.setIgnoreUnresolvablePlaceholders(false);
propConfig.setLocalOverride(true);
//....
相关问题