javaspringconstructorjavabeansprivate

Java Spring bean with private constructor


Is it possible in Spring that a class for bean doesn't have a public constructor but only a private one? Will this private constructor be invoked when the bean is created?


Solution

  • Yes, Spring can invoke private constructors. If it finds a constructor with the right arguments, regardless of visibility, it will use reflection to set its constructor to be accessible.