2012-01-15 3 views
-2

私はFooProcess(インタフェース)[nullオブジェクト]のオブジェクト 'Foo Action'を持っています。 FooProcessのサブクラス[FooOneまたはFooTwo]のオブジェクトとして 'Foo Action'を初期化したいとします。春のフレームワークを使用してSpring Frameworkを使用してクラスパス/名前からオブジェクトを作成する方法は?

、私は今、私はサブクラスの一つとしてFooAction初期化したいと思い、ArrayListFooListFooProcessのサブクラスの名前のリスト)を作成することができています。 (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()を呼び出します。これはBeanDefinitionクラス自体であり、定義されたBeanのクラスではありません。

また、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; 
} 
関連する問題