감사합니다 모두 (그리고 특히 봄에 다양한 옵션을 표시하기 위해 많은 노력을 기울인 Espen).
결국 스프링 구성을 필요로하지 않는 솔루션을 직접 발견했습니다.
Stephen C의 링크를 따라 가서 해당 스레드 내에서 SimpleBeanInfo 클래스에 대한 참조를 찾았습니다. 이 클래스는 사용자가 비표준 setter/getter가있는 클래스와 동일한 패키지에 다른 클래스를 배치하여 클래스 이름에 'BeanInfo'가 추가 된 로직을 무시하고 'BeanInfo'를 구현함으로써 자신의 bean 메소드 해결 코드를 작성할 수있게합니다. '인터페이스.
그런 다음 Google에서 검색을 한 결과이 blog은 길을 가리 켰습니다. 블로그의 솔루션은 아주 기본적인 것이므로 제 목적을 위해 덧붙여 설명했습니다.이 방법
당 클래스 (와 유창함 세터)
public class MyComponentBeanInfo<T> extends SimpleBeanInfo {
private final static Class<?> _clazz = MyComponent.class;
PropertyDescriptor[] _properties = null;
public synchronized PropertyDescriptor[] getPropertyDescriptors() {
if (_properties == null) {
_properties = Helpers.getPropertyDescriptionsIncludingFluentSetters(_clazz);
}
return _properties;
}
public BeanDescriptor getBeanDescriptor() {
return new BeanDescriptor(_clazz);
}
}
PropertyDescriptor를 생성 방법
public static PropertyDescriptor[] getPropertyDescriptionsIncludingFluentSetters(Class<?> clazz) {
Map<String,Method> getterMethodMap = new HashMap<String,Method>();
Map<String,Method> setterMethodMap = new HashMap<String,Method>();
Set<String> allProperties = new HashSet<String>();
PropertyDescriptor[] properties = null;
try {
Method[] methods = clazz.getMethods();
for (Method m : methods) {
String name = m.getName();
boolean isSetter = m.getParameterTypes().length == 1 && name.length() > 3 && name.substring(0,3).equals("set") && name.charAt(3) >= 'A' && name.charAt(3) <= 'Z';
boolean isGetter = (!isSetter) && m.getParameterTypes().length == 0 && name.length() > 3 && name.substring(0,3).equals("get") && name.charAt(3) >= 'A' && name.charAt(3) <= 'Z';
if (isSetter || isGetter) {
name = name.substring(3);
name = name.length() > 1
? name.substring(0,1).toLowerCase() + name.substring(1)
: name.toLowerCase();
if (isSetter) {
setterMethodMap.put(name, m);
} else {
getterMethodMap.put(name, m);
}
allProperties.add(name);
}
}
properties = new PropertyDescriptor[allProperties.size()];
Iterator<String> iterator = allProperties.iterator();
for (int i=0; i < allProperties.size(); i++) {
String propertyName = iterator.next();
Method readMethod = getterMethodMap.get(propertyName);
Method writeMethod = setterMethodMap.get(propertyName);
properties[i] = new PropertyDescriptor(propertyName, readMethod, writeMethod);
}
} catch (IntrospectionException e) {
throw new RuntimeException(e.toString(), e);
}
return properties;
}
장점 :
- 커스텀 스프링 설정이 없다. (스프링은 비표준 setter를 인식하지 못하고 정상적인 것으로 간주한다.) Spring .jar 파일에는 의존하지 않지만 Spring에서는 액세스 할 수 있습니다.
- 그냥 작동하는 것 같습니다. 이 방법
단점 :
- I 표준이 아닌 세터 내 API 클래스 모두를위한의 BeanInfo 클래스를 만들 배치해야합니다. 다행스럽게도 약 10 개의 클래스가 있으며 메소드 분석 로직을 별도의 클래스로 옮겨서 유지할 수있는 곳이 하나뿐입니다.
제 생각에는 생각
닫기, 봄은 기본적으로, 그들은 사람을 다치게하지 않아 유창 세터를 처리해야하며, 그것은 단지 반환 값을 무시한다.
세터가 엄격하게 무효가되도록 요구함으로써 보일러 플레이트 코드를 다른 방법으로 많이 쓰게되었습니다. Bean Specification을 고맙게 생각하지만 Bean 해상도는 표준 Bean 리졸버를 사용하지 않고도 리플렉션을 사용하는 것이 쉽지 않으므로 Spring은이 상황을 처리 할 Bean 리졸버 옵션을 제공해야한다.
반드시 기본 메커니즘을 그대로두고 한 줄 구성 옵션을 제공하십시오. 이 버전이 선택적으로 완화 될 수 있기를 기대합니다.
사실 그것은 다소 흥미로운 아이디어입니다. – alternative