Spring 配置

引用:property-placeholder/override
   读取property文件的方式


Context

1
2
3
4
5
6
7
8
<!--注解支持-->
<context:annotation-config />
<!--扫描的包-->
<context:component-scan base-package="cn.ws.test"/>
<context:component-scan base-package="cn.ws.handle">
<context:exclude-filter type="regex" expression=".."/>
<context:include-filter type="annotation" expression="cn.ws.annotation.AccessCheck"/>
</context:component-scan>

这里component-scan中包含两个属性,一个是exclude-filter不包含
include-filter包含
里面的type属性对应有四种

  • annotation
    表示过滤方式为注解,含有后面expression注解的都会被过滤掉
    expression示例:cn.ws.annotation.AccessCheck
  • regex
    以正则表达式的方式过滤
    expression示例:com.\ws\..*
  • assignable
    以继承和实现的方式过滤,所有继承或者实现了后面expression的都会被过滤
    expression示例:java.util.HashMap
  • aspectj
    以Aspectj表达式的方式过滤
    expression示例:
  • custom
    自定义TypeFilter过滤规则,写一个类,这个类必须实现org.springframework.core.type.TypeFilter接口
    expression示例:cn.ws.MyTypeFilter
    1
    2
    <context:property-override file-encoding="utf-8" ignore-unresolvable="true" location="classpath:spring/test.properties"/>
    <context:property-placeholder location="classpath:spring/test.properties" file-encoding="utf-8" ignore-unresolvable="true"/>
    property-placeholder可以读取properties文件
    另外通过下面的方式也可以读取
    1
    2
    3
    4
    5
    6
    7
    8
    9
    <bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
    <property name="ignoreUnresolvablePlaceholders" value="true"/>
    <property name="locations">
    <list>
    <value>classpath:test.properties</value>
    <value>classpath:test1.properties</value>
    </list>
    </property>
    </bean>

注意:这种方式下,如果你在spring-mvc.xml文件中有如下配置,则一定不能缺少下面的红色部分,原因

详细参考:spring获取property

文章作者: C.c
文章链接: https://liquidcat.cc/spring-配置.html
版权声明: 本博客所有文章除特别声明外,均采用 CC BY-NC-SA 4.0 许可协议。转载请注明来自 Me