2012-01-15 3 views
-2

유형이 FooProcess (인터페이스) [null 객체] 인 'Foo Action'객체가 있습니다. [Foo Action]을 서브 클래스 [FooOne 또는 FooTwo]의 객체로 FooProcess으로 초기화하고 싶습니다. Spring 프레임 워크를 사용 Spring Framework를 사용하여 클래스 경로/이름에서 객체를 만드는 방법은 무엇입니까?

, 내가 ArrayList FooList ( FooProcess의 서브 클래스의 이름 목록)를 만들 수 있어요, 지금은 서브 클래스 중 하나 FooAction를 초기화하고 싶습니다. ( selected으로 초기화 할 클래스를 정의하십시오)

FooProcess의 모든 서브 클래스에는 String을 허용하는 생성자가 있습니다. 그것은 내가 볼 같은

import java.awt.Rectangle; 
import java.lang.reflect.Constructor; 
import java.lang.reflect.InvocationTargetException; 

public class SampleInstance { 

    public static void main(String[] args) { 

     Rectangle rectangle; 
     Class rectangleDefinition; 
     Class[] intArgsClass = new Class[] { int.class, int.class }; 
     Integer height = new Integer(12); 
     Integer width = new Integer(34); 
     Object[] intArgs = new Object[] { height, width }; 
     Constructor intArgsConstructor; 

     try { 
      rectangleDefinition = Class.forName("java.awt.Rectangle"); 
      intArgsConstructor = rectangleDefinition 
        .getConstructor(intArgsClass); 
      rectangle = (Rectangle) createObject(intArgsConstructor, intArgs); 
     } catch (ClassNotFoundException e) { 
      System.out.println(e); 
     } catch (NoSuchMethodException e) { 
      System.out.println(e); 
     } 
    } 

    public static Object createObject(Constructor constructor, 
      Object[] arguments) { 

     System.out.println("Constructor: " + constructor.toString()); 
     Object object = null; 

     try { 
      object = constructor.newInstance(arguments); 
      System.out.println("Object: " + object.toString()); 
      return object; 
     } catch (InstantiationException e) { 
      System.out.println(e); 
     } catch (IllegalAccessException e) { 
      System.out.println(e); 
     } catch (IllegalArgumentException e) { 
      System.out.println(e); 
     } catch (InvocationTargetException e) { 
      System.out.println(e); 
     } 
     return object; 
    } 
} 
+0

무엇이 문제입니까? – skaffman

+0

왜 Spring Bean Factory를 사용하여 객체를 인스턴스화하고 종속성을 주입하지 않습니까? – duffymo

+0

@skaffman 게시물에서 암시 한 '문제'는 내가 필요로하는 것을주지 않았고, IDE는 구문 적으로 잘못되었다고 말합니다. – JD009

답변

0

봐 :

내 문제는이 라인에 특별히

FooAction = component.getClass().getConstructor(f); 

전체 방법 당신 ' BeanDefinition 클래스에 getClass()를 호출하면 정의 된 Bean 클래스가 아닌 BeanDefinition 클래스 자체가됩니다.

또한 Java에서 문자열을 ==와 비교하면 안됩니다. .equals()를 대신 사용하십시오.

0

한 문제를 작동 이것

public FooProcess Load(String selected, String f) throws ClassCastException, InstantiationException, IllegalAccessException, ClassNotFoundException{ 
    ArrayList<String> FooList = new ArrayList<String>(); 
    final ClassPathScanningCandidateComponentProvider provider = new ClassPathScanningCandidateComponentProvider(true); 
    provider.addIncludeFilter(new AssignableTypeFilter(FooProcess.class)); 
    for (BeanDefinition component : provider.findCandidateComponents("org.project.foomodule.systems")) { 
     if(selected == component.getBeanClassName()){ 
     FooAction = component.getClass().getConstructor(f); 
    } } 
    return FooAction; 
} 
관련 문제