• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • paul wheaton
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Liutauras Vilda
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Piet Souris
Bartenders:
  • salvin francis
  • Mikalai Zaikin
  • Himai Minh

example Code from Spring In Action

 
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
I'm trying to run the following code from Spring In Action given under the topic Writing a bean Post Processor.but it doesn't compile: it says "Field" cannot be resolved to a type

package src.java;
import org.springframework.beans.factory.config.*;
import org.springframework.beans.*;
public class Fuddifier implements BeanPostProcessor {
public Object postProcessAfterInitialization(
Object bean, String name) throws BeansException {
Field[] fields = bean.getClass().getDeclaredFields();
try {
for(int i=0; i < fields.length; i++) {
if(fields[i].getType().equals(
java.lang.String.class)) {
fields[i].setAccessible(true);
String original = (String) fields[i].get(bean);
fields[i].set(bean, fuddify(original));
}
}
} catch (IllegalAccessException e) {
e.printStackTrace();
}
return bean;
}
private String fuddify(String orig) {
if(orig == null) return orig;
return orig.replaceAll("(r|l)", "w")
.replaceAll(" (R|L) ", "W");
}
public Object postProcessBeforeInitialization(
Object bean, String name) throws BeansException {
return bean;
}
}

Also "Registering Bean post Processors" topic is not so clear to me.

Please help
Shaila
 
Ranch Hand
Posts: 15304
6
Mac OS X IntelliJ IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
There is no Field class in the Spring API. At least not that I could find looking at the API online. There is, however, a Field class with the standard JDK/JRE.

java.lang.reflect.Field

Looking at what is being done in that class (reflection) I'd assume that would work, so try importing that class and see what happens.
 
Sheriff
Posts: 14691
16
Eclipse IDE VI Editor Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I've tried the samples in the book too.
This Field is, as Gregg said, a java.lang.reflect.Field
 
Shaila Goyal
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks a lot,

Its working now.
In the book - the required imports are not mentioned, that creates problems.

Currently i'm working on Chapter2 and under 2.4.4 Customizing property editors where it gives the below entry to add to configuration file:

<bean id="customEditorConfigurer" class="org.springframework.
beans.factory.config.CustomEditorConfigurer">
<property name="customEditors">
<map>
<entry key="com.springinaction.chapter02.Phone">
<bean id="phoneEditor"
class="com.springinaction.02.PhoneEditor">
</bean>
</entry>
</map>
</property>
</bean>

when i run this code - i get
org.springframework.beans.factory.BeanDefinitionStoreException: Error registering bean with name 'customEditorConfigurer' defined in class path resource [src/java/Hello.xml]: Bean class [org.springframework. beans.factory.config.CustomEditorConfigurer] not found; nested exception is java.lang.ClassNotFoundException: org/springframework/ beans/factory/config/CustomEditorConfigurer

What i'm missing here?

Thanks again,
Shaila
 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE VI Editor Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Do not put a carriage return in

Instead, type the full classname on one line :

[ June 22, 2006: Message edited by: Satou kurinosuke ]
 
Shaila Goyal
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Satou,
Thanks for pointing such a silly mistake.
I'm not clear with this key thing given in my previous code which is

<entry key="src.java.phone">
<bean id="phoneEditor"
class="src.java.PhoneEdito">
</bean>
</entry>


and there is no such class as src.java.phone
becauseof that now i'm getting the exception:
org.springframework.beans.factory.BeanInitializationException: Could not load required type [src.java.phone] for custom editor; nested exception is java.lang.ClassNotFoundException: src.java.phone
 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE VI Editor Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Where is your Phone class then ? Just set this class's fully qualified type to the key of the CustomerEditor. Maybe your want to give it a capital letter :
src.java.Phone.
And check also the spelling of src.java.PhoneEdito[b]r[b].
 
Shaila Goyal
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
the spelling is correct - by mistake i gave this name to my class thats why this entry in XML.
And thats what i wanted to mention - that i couldn't found the phone class in the book(the code for this class is not given in the book also i'm clear that what should go in that class if i want to code it myself)

Regards,
Shaila
 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE VI Editor Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
A mistake in the book. It is not Phone, but PhoneNumber.
You need to understand what the property editor does. You want Spring to understand what a phone number is. So you are mapping a PhoneNumber to a PhoneEditor.

In the book, the contact bean looks like this :


If you look at Contact's phoneNumber member variable, it's a PhoneNumber.
If you don't set a custom editor, Spring cannot understand how to convert "888-555-1212" to PhoneNumber instance. To make the magic happen, you tell Spring to use a PhoneEditor to interpret the value when setting a PhoneNumber.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic