In Spring2.0-applicationContext. xml, the el expression is used to assign a value to the object class attribute as a string-,

Source: Internet
Author: User

In Spring2.0-applicationContext. xml, the el expression is used to assign a value to the object class attribute as a string-,
(-1) Preface

In the past two days, when I read "Return of the King of javaweb development" and learned Spring's PropertyPlaceholderConfigurer, a problem occurs. I have already paid $ {jdbc. name} is assigned to the attributes in bean. After the main method is used for testing, the output attribute value is still $ {jdbc. name }.

(0) how to solve the problem

A. I first thought about whether it was wrong, so I checked it carefully and found that the book said PropertyOverrideConfigurer, but in fact the configuration file is written in PropertyPlaceholderConfigurer, it is useless in exchange for a few times.

B. I asked the teacher. I suddenly got inspiration when the teacher went back to read the materials. I thought I had found a solution.

C. I wonder if the jar package or the spring version is not imported, and the el expression cannot be parsed. At the beginning, I decided one by one. Later, I still did not export all the data that can be imported,

D. I am constantly changing the description method to go to Baidu. Someone is leaving at night. I have a meal in advance, and I have to go to lol for dinner. I haven't found Baidu for a while, as a result, I have not received a reply when I asked questions or asked questions. The League of legends lost for one night and the power was cut off in the middle.

C. in the morning, check whether the main method is used for testing. Deploy the project to tomcat and deploy the project in the index. in jsp, I thought about whether the method is correct. I also thought about how to use the bean of PropertyPlaceholderConfigurer $ {jdbc. the bean of name} is associated with each other, so the use of Baidu PropertyPlaceholderConfigurer is started, and the solution is indirectly found.

(1) specific solutions

In fact, I think there are not many people who have encountered this problem ..., Maybe this is the first time I learned spring to read this book. Here is one of the following methods:

Configuration File

<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 Method

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 = \ u738B \ u65ED

Jdbc. age = 21

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.