Spring dependency injection - reflection / byte code instrumentation
When I want to use dependency injection with some non-default constr开发者_JS百科uctor, i.e. with parameters, spring must be using byte code instrumentation for that, right? Because AFAIK reflection only supports default constructor?
Reflections supports any number of arguments, say for instance I have a class TestClass which takes two arguments in one of its constructors:
public TestClass(int test1, String test) {
System.out.println(test1 + test);
}
I would invoke this constructor, through reflection, like so:
Constructor<TestClass> constructor = TestClass.class.getConstructor(Integer.class, String.class);
TestClass test = constructor.newInstance(1, "test");
Reflection.
Please check source code for the class
org.springframework.beans.factory.support.ConstructorResolver Method: protected BeanWrapper autowireConstructor(...)
invokes =>
org.springframework.beans.factory.support.SimpleInstantiationStrategy Method: public Object instantiate(...)
invokes =>
org.springframework.beans.BeanUtils Method: public static Object instantiateClass(Constructor ctor, Object[] args)
which uses Reflection to create the bean
精彩评论