File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Other Application Frameworks and the fly likes example Code from Spring In Action Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Frameworks » Other Application Frameworks
Bookmark "example Code from Spring In Action" Watch "example Code from Spring In Action" New topic
Author

example Code from Spring In Action

Shaila Goyal
Greenhorn

Joined: Apr 26, 2006
Posts: 16
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
Gregg Bolinger
GenRocket Founder
Ranch Hand

Joined: Jul 11, 2001
Posts: 15299
    
    6

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.


GenRocket - Experts at Building Test Data
Christophe Verré
Sheriff

Joined: Nov 24, 2005
Posts: 14688
    
  16

I've tried the samples in the book too.
This Field is, as Gregg said, a java.lang.reflect.Field


[My Blog]
All roads lead to JavaRanch
Shaila Goyal
Greenhorn

Joined: Apr 26, 2006
Posts: 16
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

Joined: Nov 24, 2005
Posts: 14688
    
  16

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

Joined: Apr 26, 2006
Posts: 16
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

Joined: Nov 24, 2005
Posts: 14688
    
  16

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

Joined: Apr 26, 2006
Posts: 16
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

Joined: Nov 24, 2005
Posts: 14688
    
  16

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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: example Code from Spring In Action