关于Spring的问题

J2EE 码拜 9年前 (2015-04-08) 970次浏览 0个评论

我就想问下Spring管理的对象是不是也是new实例出来的 ???

关于Spring的问题
求回复啊                                ,
关于Spring的问题
不是,是注入的形式来实现的!
关于Spring的问题
引用 2 楼 flyingMan_ZUO 的回复:

不是,是注入的形式来实现的!

不是  我是想说他的实例是怎么来的  

关于Spring的问题
怎么说呢  ,  就是实例是怎么产生的 
关于Spring的问题
大神呢  快点出来啊  
关于Spring的问题
用字符串取得类,构造函数,然后创建对象:
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.

关于Spring的问题
5分
通过反射出来的呀,想看嘛,它也不可能new呀
关于Spring的问题
5分
是通过反射生成出来的,当要用到这个类时,就会用反射实例化一个实现类出来
关于Spring的问题
我还以为是Spring 帮new实例化 然后注入给对象
关于Spring的问题
10分
反射得到的,你主要搞明白spring中bean的声明周期就好了
关于Spring的问题
引用 10 楼 u014682894 的回复:

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

生命周期

关于Spring的问题
5分
反射 还有java的动态代理 如果你是初级使用者不需要太了解 只需要了解bean的声明和生命周期就可以了
关于Spring的问题
5分
引用 10 楼 u014682894 的回复:

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

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

关于Spring的问题
引用 10 楼 u014682894 的回复:

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

有点看不懂bean的生命周期的解说关于Spring的问题

关于Spring的问题
引用 13 楼 humanity 的回复:
Quote: 引用 10 楼 u014682894 的回复:

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

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

话说:non-singleton行为的bean,Spring容器仅仅是new的替代,容器只负责创建。 
这怎么理解

关于Spring的问题
依赖注入、IOC和面向切面编程 AOP

CodeBye 版权所有丨如未注明 , 均为原创丨本网站采用BY-NC-SA协议进行授权 , 转载请注明关于Spring的问题
喜欢 (0)
[1034331897@qq.com]
分享 (0)

文章评论已关闭!