0

When I want to use dependency injection with some non-default constructor, i.e. with parameters, spring must be using byte code instrumentation for that, right? Because AFAIK reflection only supports default constructor?

Sumedh
  • 335
  • 3
  • 13

2 Answers2

3

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");
alexwen
  • 1,128
  • 7
  • 16
0

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

Ramesh
  • 3,841
  • 5
  • 22
  • 28