Wednesday, December 12, 2012

Java Spring bean with private constructor - Stack Overflow

Java Spring bean with private constructor - Stack Overflow:


Is possible in Spring that class for bean doesn't have public constructor but only private ? Will this private constructor invoked when bean is created? Thanks.
share|improve this question

77% accept rate
3 
How about giving it a try and then posting the results? – dm3 Aug 31 '11 at 8:38
It is difficult - I don't have all source. – user710818 Aug 31 '11 at 8:40
feedback
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.
share|improve this answer
Could you please give me the reference to docs? Constructor has parameters, and in xml file to this arguments assigned values. – user710818 Aug 31 '11 at 8:52
you mean if a bean has specifically something like private bean() { ... } spring can invoke that? How is that possible, that defies the whole purpose of "private". – Ashkan Aryan Aug 31 '11 at 8:56
Sorry, I'm probably wrong about it only being no-arg constructors. I'm just going by what I've noticed on my own projects. I can't say I've ever seen it in the Spring documentation anywhere. But this is the javadoc for the class that is responsible for doing the instantiation. static.springsource.org/spring/docs/3.0.x/javadoc-api/org/…...) – Kevin Stembridge Aug 31 '11 at 9:01
4 
@Ashkan It is possible using reflection. In particular, the Spring BeanUtils class uses a class called ReflectionUtils to make the constructor accessible. See javadocs for Constructor.setAccessible(). – Kevin Stembridge Aug 31 '11 at 9:03
@Kevin Stembridge you are correct, it is possible using Reflection. – Ashkan Aryan Aug 31 '11 at 9:07
show 2 more comments
feedback
You can always use a factory method to create beans rather than relying on a default constructor, fromThe IoC container: Instantiation using an instance factory method:
<!-- the factory bean, which contains a method called createInstance() -->
<bean id="serviceLocator" class="com.foo.DefaultServiceLocator">
  <!-- inject any dependencies required by this locator bean -->
</bean>

<!-- the bean to be created via the factory bean -->
<bean id="exampleBean"
      factory-bean="serviceLocator"
      factory-method="createInstance"/>
This has the advantage that you can use non-default constructors for your bean, and the dependencies for the factory method bean can be injected as well.
share|improve this answer
interesting, but I cannot find factory. May be exists another way? – user710818 Aug 31 '11 at 9:08
1 
You can't find factory? I don't understand what you are saying. – Matthew Farwell Aug 31 '11 at 9:09
@Matthew: He appears to be retrofitting Spring around some fugly third-party code. That's just bound to be messy. – Donal Fellows Aug 31 '11 at 9:29
1 
@Donal, no I didn't understand what he said in the comment. – Matthew Farwell Aug 31 '11 at 9:52


'via Blog this'

No comments:

Post a Comment