tags:

views:

38

answers:

2

I have the need to specify a URL in a Spring context file but don't want Spring to turn it into a resource, i.e. I want the URL to be passed as a String to the bean that I'm creating in the context file rather than it being converted into a Resource object.

How can I avoid Spring creating a Resource?

Thanks,

Andrew

+1  A: 

Spring will coerce the value into the type of the property, whatever that happens to be. If your bean has a Resource property of that name, then Spring will attempt to convert the value into a Resource. If you just want to the value as a String, then give your bean a property of type String, and Spring will pass it in as-is.

skaffman
A: 

Use a setter that accepts a Resource (enabling spring magic) and stores the resulting URL, not the resource object

private String resourceUrl;

public Resource setResource(Resource resource) {
    this.resourceUrl = resource.getURL().toExternalForm();
}
seanizer