Build strings with Spring?
I got to know Spring and looked at dependency injection and IoC the way I did before.
If I want to build a string, say for a file name, and I already have a Spring bean that contains a directory, what is the best way to add the file name?
Writing a bean to do this seems pretty trivial, but I think Spring may already be able to do this somewhere, though its API. If possible, how?
Just for kicks, here's a pretty simple bean implementation ...
public class MySimpleStringAppender {
private final StringBuffer myString = new StringBuffer();
public MySimpleStringAppender(List<String> myStrings) {
for (String string : myStrings) {
myString.append(string);
}
}
public String getMySimpleString() {
return myString.toString();
}
}
and configured with ...
<bean id="filename" class="MySimpleStringAppender">
<constructor-arg ref="filenameStrings"/>
</bean>
<util:list id="filenameStrings">
<ref bean="directory"/>
<value>filename.txt</value>
</util:list>
<bean id="directory" class="java.lang.String">
<constructor-arg value="C:/myDirectory/"/>
</bean>
So while this isn't very much work or code, I think there will be something available so I wouldn't need to write this at all.
+1
a source to share
2 answers
No, I've never seen such a thing. You can also simplify your XML by combining it all into one:
<bean id="filename" class="MySimpleStringAppender">
<constructor-arg>
<list>
<value>C:/myDirectory</value>
<value>filename.txt</value>
</list>
</constructor-arg>
</bean>
But you probably already knew that.
+1
a source to share