开发者

Disable Spring URL resource loading in context file

开发者 https://www.devze.com 2022-12-30 09:23 出处:网络
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 t开发者_StackOverflow中文版he URL to be passed as a String to the bean that I\'

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 t开发者_StackOverflow中文版he 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


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.


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();
}
0

精彩评论

暂无评论...
验证码 换一张
取 消