在Spring LDAP 2.x中,OdmManager工具似乎已被弃用,因为大多数类似odm的东西都可以通过ldapTemplate来完成,这是真的。但是OdmManager能够注入一个可以告诉您自定义类型转换的ConverterManager。将ldapTemplate用于类似odm(ConverterManager)操作的等效方法是什么?
如果ldapTemplate中没有等效的系统,那么应该:
使用单个字符串构造函数和String toString()类方法隐式检测自定义类,如果它们作为要映射到ldap属性的属性存在。
隐式允许使用bean编辑器,从文本映射到特定类型
明确地拥有一些类似转换器管理器的功能,您可以在其中进行配置。
作为一个例子,考虑一下简单的类(我想成为bean属性的类型,它将映射到ldap时间戳)
公共类LdapTimestamp {
static private Calendar cal = Calendar.getInstance(TimeZone.getTimeZone("Australia/Brisbane"));
static private DateFormat toStringFormat;
static {
toStringFormat = DateFormat.getDateTimeInstance(DateFormat.SHORT, DateFormat.SHORT);
toStringFormat.setCalendar(cal);
}
static private DateFormat nativeLdapFormat = new SimpleDateFormat("yyyyMMddHHmmssZ");
private Date dateTime; // GMT time
public LdapTimestamp(String ldapDateTimeString) throws ParseException {
this.dateTime = nativeLdapFormat.parse(ldapDateTimeString);
}
public LdapTimestamp() {
super();
}
public Date getDateTime() {
return dateTime;
}
public void setDateTime(Date dateTimeObject) {
this.dateTime = dateTimeObject;
}
public void setDateTime(String ldapDateTimeString) throws ParseException {
this.dateTime = nativeLdapFormat.parse(ldapDateTimeString);
}
public String toStringShort() {
return toStringFormat.format(dateTime);
}
public String toString() {
return nativeLdapFormat.format(dateTime);
}
}
目的是bean本身存储一个Date对象,可以用于日期范围比较等,同时将bean的ldap日期字符串作为toString()向内返回到bean,作为构造函数单个String参数。
这似乎是ConverterManager的建议,但这是新代码,所以如果可以避免,我不想使用已弃用的OdmManager接口。不推荐使用ConverterManager,但我无法看到将其链接到ldapTemplate以使用的明显方法。
欢迎任何想法。
答案 0 :(得分:1)
LdapTemplate
有setObjectDirectoryMapper
方法,可让您注入已配置的ObjectDirectoryMapper
(与先前版本中的OdmManager
相对应)。 DefaultObjectDirectoryMapper
可以使用ConverterManager
进行配置,因此我认为您应该全部设置。