About me

我的照片
目前就职于杭州某电子商务公司,工作兴趣包括高并发分布式架构,JVM性能优化等方面。

2013年12月19日星期四

Configuration outof jar/war

<bean id="myth-web_properties" class="org.springframework.beans.factory.config.PropertiesFactoryBean">
<property name="locations">
<list>
<value>classpath:META-INF/mythcore-dal.properties</value>
<value>classpath:META-INF/mythcore-framework.properties</value>
<value>classpath:META-INF/mythcore-biz.properties</value>
<value>classpath:META-INF/myth-biz.properties</value>
<value>classpath:META-INF/myth-web-shared.properties</value>
<value>classpath:META-INF/myth-web.properties</value>
<value>classpath:maven.properties</value>
<value>classpath:META-INF/mythcore-loan.properties</value>
<value>classpath:META-INF/mythcore-batch.properties</value>
</list>
</property>
</bean>

PropertiesFactoryBean从指定的Resource加载properties文件,生成Properties对象作为spring配置中占位符的数据来源。

上面例子中的配置数据来自classpath,web应用的classpath路径是WEB-INF/lib下的所有jar,还有WEB-INF/classes目录。也就是说配置数据都是来自jar包和classes目录内。

spring除了ClassPathResource外还有FileSystemResource。我们可以利用它来加载在war包外的配置数据。

另外,指定资源路径的时候还可以使用pattern方式,也就是在资源路径中使用通配符。

    public static void main(String[] args) throws IOException {
       
        PathMatchingResourcePatternResolver patternResolver = new PathMatchingResourcePatternResolver();
       
        Resource[] res = patternResolver.getResources("file:/d:/program/**/*.zip");
       
    }


没有评论:

发表评论