(Recovered from my old Blog).
This is mentioned in passing in the Spring documentation – but to highlight:
Spring will automatically convert a String in a bean definition into a Resource if required.
If the target Resource is a specific class (e.g. UrlResource) then the passed in string must match the format, or a runtime error will ensue.
For example:
view source
1 2 3 |
<bean id=“settlement” class=“SettlementImpl”> <constructor–arg index=“0” value=“/config/transaction/settlement.xml” /> </bean> |
1 2 3 4 5 6 7 8 9 10 11 |
public SettlementImpl(Resource settlementResource) { SettlementConfig settlementConfig; XStream stream = new XStream(); stream.processAnnotations(SettlementConfig.class); try { settlementConfig = (SettlementConfig) stream.fromXML(settlementResource.getInputStream()); } catch (IOException e) { // Whatever } // Rest of class } |