解决方案 »

  1.   

    用字符串取得类,构造函数,然后创建对象:
    Class clazz = Class.forName("com.tur.command.ControllerCommand"); 
    Constructor constructor = clazz.getConstructor(/*可以有参数类型*/), 
    Object obj = constructor.newInstance();当然还有泛型的使用。public Constructor<T> getConstructor(Class<?>... parameterTypes)
                                  throws NoSuchMethodException,
                                         SecurityException
    Returns a Constructor object that reflects the specified public constructor of the class represented by this Class object. The parameterTypes parameter is an array of Class objects that identify the constructor's formal parameter types, in declared order. If this Class object represents an inner class declared in a non-static context, the formal parameter types include the explicit enclosing instance as the first parameter. public T newInstance(Object... initargs)
                  throws InstantiationException,
                         IllegalAccessException,
                         IllegalArgumentException,
                         InvocationTargetException
    Uses the constructor represented by this Constructor object to create and initialize a new instance of the constructor's declaring class, with the specified initialization parameters. Individual parameters are automatically unwrapped to match primitive formal parameters, and both primitive and reference parameters are subject to method invocation conversions as necessary. 
    If the number of formal parameters required by the underlying constructor is 0, the supplied initargs array may be of length 0 or null. If the constructor's declaring class is an inner class in a non-static context, the first argument to the constructor needs to be the enclosing instance; see section 15.9.3 of The Java™ Language Specification. If the required access and argument checks succeed and the instantiation will proceed, the constructor's declaring class is initialized if it has not already been initialized. If the constructor completes normally, returns the newly created and initialized instance.
      

  2.   

    通过反射出来的呀,想看嘛,它也不可能new呀
      

  3.   

    我还以为是Spring 帮new实例化 然后注入给对象
      

  4.   

    反射得到的,你主要搞明白spring中bean的声明周期就好了
      

  5.   

    反射 还有java的动态代理 如果你是初级使用者不需要太了解 只需要了解bean的声明和生命周期就可以了
      

  6.   


    new 只适合在源码中使用,当我们在运行的时候动态创建对象时只能是反射了。
      

  7.   

    有点看不懂bean的生命周期的解说
      

  8.   


    new 只适合在源码中使用,当我们在运行的时候动态创建对象时只能是反射了。话说:non-singleton行为的bean,Spring容器仅仅是new的替代,容器只负责创建。 
    这怎么理解