spring中读不到配置文件问题

最近碰到一个问题,明明已经配置了<context:property-placeholder location="classpath:bieyang-config.properties" />

启动spring还是报找不到${bieyang.host}类似这种错误,查了一天,各种试验。得出如下结论:

1,首先<context:property-placeholder location="classpath:bieyang-config.properties" />这个配置项目中只能用一次。

2.<context:property-placeholder location="classpath:bieyang-config.properties" />这个配置的意思是把属性加载到context中了,那么问题来了,一般我们的项目中会有2个以上servletcontext,一个是通过listener创建

<context-param>
		<param-name>contextConfigLocation</param-name>
		<param-value>classpath*:bean/spring-web.xml</param-value>
	</context-param>
<listener>
		<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
	</listener>
还有就是通过servlet方式创建

<servlet>
		<servlet-name>webapp</servlet-name>
		<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
		<init-param>  
    <param-name>contextConfigLocation</param-name>  
    <param-value>classpath:bean/webapp-servlet.xml</param-value>  
  </init-param>  
		<load-on-startup>3</load-on-startup>
	</servlet>
如果你把 <context:property-placeholder location="classpath:bieyang-config.properties" />写到其中一个context的xml中,而你又在两个context中的xml中都要通过属性文件读取属性值,这个是不行的。你只能把需要读取属性文件的配置写到同一个context的xml中。

这里顺便提一下,

    <!-- 启用缓存注解功能,这个是必须的,否则注解不会生效,另外,该注解一定要声明在spring主配置文件中才会生效 -->   
<!--      需要注意的是<cache:annotation-driven/>只会去寻找定义在同一个ApplicationContext下的@Cacheable等缓存注解。  -->
<!--  proxy-target-class="true" 看情况要不要用-->
<!--     <cache:annotation-driven cache-manager="cacheManager"  />     -->
<cache:annotation-driven/>  
      
    

	<!-- 启动Spring MVC的注解功能,完成请求和注解POJO的映射 -->
	<mvc:annotation-driven />
这种启用注解功能的配置必须放到
DispatcherServlet对应的servletcontext的xml中才能生效,因为注解比如@cacheable它的作用范围也和servletcontext有关,本人还比较菜没有深入去了解。



  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

不写版本的都是耍流氓

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值