유형이 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;
}
}
무엇이 문제입니까? – skaffman
왜 Spring Bean Factory를 사용하여 객체를 인스턴스화하고 종속성을 주입하지 않습니까? – duffymo
@skaffman 게시물에서 암시 한 '문제'는 내가 필요로하는 것을주지 않았고, IDE는 구문 적으로 잘못되었다고 말합니다. – JD009