我正在使用Spring框架,我不知道如何做这个简单的事情:我想为bean提供一个String,字符串是多个部分,一些固定和其他变量串联的结果
例如,它可能是这样的:
"myReportFile_20102101_1832.txt"
- 第一部分是固定部分 - 第二部分是具有当前日期时间的时间戳 - 最后一部分是另一个固定部分
如何用最简单的方法实现这一目标?
非常感谢.
这听起来像是Spring表达语言的作业(在Spring 3.0中引入)给我.虽然为该任务使用工厂bean可能更容易(它获取通过IOC注入的静态信息,并提供实例化其他bean的工厂方法,包括计算的动态信息).像这样
class FileNameFactoryBean
{
private Date date = new Date();
private String prefix;
private String postfix;
public OtherBean createBean()
{
String filename = prefix + date.toString() + postfix;
return new OtherBean(filename);
}
// Getters and Setters
}
Run Code Online (Sandbox Code Playgroud)
然后在你的bean配置中
<bean id="fileNameFactory" class="package.FileNameFactoryBean">
<property name="prefix" value="file_" />
<property name="postfix" value=".txt" />
</bean>
<bean id="otherBean" factory-bean="fileNameFactory" factory-method="createBean"/>
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2838 次 |
| 最近记录: |