Spring2.0-applicationContext.xml中使用el表达式给实体类属性赋值被当成字符串-遁地龙卷风

(-1)写在前面

这两天读《javaweb开发王者归来》,学到Spring的PropertyPlaceholderConfigurer时出现一个问题,我已${jdbc.name}的形式赋值给bean中的属性,用main方法测试后,输出属性的值仍然是${jdbc.name}。

(0)解决问题之路

a.我先想到是不是打错了,于是和书上仔细的对照了一下,发现书上说的是PropertyOverrideConfigurer,但实际上配置文件中写的是PropertyPlaceholderConfigurer,换来换去几次没用。

b.问了老师,在老师回去看资料的一瞬间突然有了灵感,我百度了一下,觉得找到了解决方案。

c.我想是不是没导入jar包、spring版本低,导致el表达式无法解析,一开始还一个个判定,后来把能导入的都导了还是不行,

d.不断的换描述方式去百度我的问题,晚上有人要走了,提前一起吃一顿饭,吃晚饭去打lol,又百度了一会没有找到,于是在博问、问问上提问,到现在也没收到回复,英雄联盟输了一晚上,中途还断电了。

c.早上的时候想是不是main方法测试的原因,把项目部署到tomcat,在index.jsp中使用还是那样,于是乎我想到了是不是使用方式不对呢,之前也想到过PropertyPlaceholderConfigurer的bean是怎么和使用${jdbc.name}的bean关联到一起的,于是开始百度PropertyPlaceholderConfigurer的使用方法,间接的找到了解决方案。

(1)具体解决方案

其实我觉得这个问题碰到的人真不多…,可能都是第一次学spring就读了这本书的人吧..,下面是其中一种方式

配置文件

<bean  class="service.IDaoImp" id="iDaoImp" >

      <property name="name" value="${jdbc.name}"></property>

</bean>

<bean id="property" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">  

   <property name="location">    

      <value>classpath:jdbc.properties</value>  

   </property>  

  </bean>

main方法

XmlBeanFactory beanFactory = new XmlBeanFactory(new ClassPathResource("applicationContext.xml"));

PropertyPlaceholderConfigurer propertyPostProcessor = (PropertyPlaceholderConfigurer)beanFactory.getBean("property");

propertyPostProcessor.postProcessBeanFactory(beanFactory);          

   IDaoImp  imp = (IDaoImp)beanFactory.getBean("iDaoImp");

  System.out.println(imp.getName());

IDaoImp

public class IDaoImp

{

      private String name;

      public String getName() {

           return name;

      }

      public void setName(String name) {

           this.name = name;

      }

     

}

jdbc.properties

jdbc.name=u738Bu65ED

jdbc.age=21

原文地址:https://www.cnblogs.com/resolvent/p/5916554.html