2012-12-07 4 views
0

아파치 개찰구-질문을 IConverter 설정 : 나는 ConverterLocator 클래스에 IConverter를 등록하기 위해 노력하고있어개찰구 : ConverterLocato - 인터페이스

. 웹 응용 수준에서 : - 방법은 값을하는 MyInterface를 구현하는 클래스가 "트리거되지"않습니다

protected IConverterLocator newConverterLocator() { 
    ConverterLocator locator = (ConverterLocator) super.newConverterLocator(); 
    locator.set(MyInterface.class, new MyConverter());  
} 

내 문제는 MyConverter.convertTo의 *은()이다. MyInterface 구현을 ConverterLocator에 등록 할 때만 작동합니다.

MyConverter를 모두 수동으로 등록하지 않고도 MyInterface의 모든 구현을 변환 할 수있는 방법에 대한 제안 사항은 무엇입니까?

이것은 내가 유일한 해결책은 IConverterLocator 자신을 구현하고 WebApplication.newConverterLocator() 그것을 등록 오버라이드 (override)이라고 생각

답변

0

해결책을 찾을 수

이것은 현재의 기본 구현을 변경해야 할 코드의 조각이다. 코드는 시작 중에 만 실행되므로 성능이 문제가되지 않아야합니다. ClassPathScanningCandidateComponentProvider를 사용하여 classpath 스캐닝을 통해 인터페이스의 모든 구현을 찾는 것이 트릭입니다.

하지 매우 우아한 해결책은 :-)

ClassPathScanningCandidateComponentProvider provider = new ClassPathScanningCandidateComponentProvider(true); 
provider.addIncludeFilter(new AssignableTypeFilter(MyInterface.class)); 


Set<BeanDefinition> components = provider.findCandidateComponents("org/example/package"); 
for (BeanDefinition component : components) 
{ 
    Class cls = Class.forName(component.getBeanClassName()); 
    locator.set(cls, new MyGenericConverter<MyInterface>()); 
} 

하지만 그런 기본 ConverterLocator를 오버라이드하고, 생성자에 위의 코드를 추가.