File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Other Application Frameworks and the fly likes Instantiated bean with private Constructor Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Frameworks » Other Application Frameworks
Bookmark "Instantiated bean with private Constructor" Watch "Instantiated bean with private Constructor" New topic

Instantiated bean with private Constructor

Gaurav Ram
Ranch Hand

Joined: Feb 23, 2008
Posts: 32

I have created one bean with Default Private Constructor like below:

and i make its entry in applicationContext.xml, now when i run my server it run successfully and also instantiated my Test bean but i think it should give error as my constructor is Private.

Can you please tell me why it allows this scenerio?

Stevi Deter
Ranch Hand

Joined: Mar 22, 2008
Posts: 265


This is explained in the Spring forums in this thread.

The short answer is that Spring is using reflection to set the constructor's accessibility to true for the purpose of instantiating it. The thread also explains why and how to use a security manager to override that behavior if you need to.

There will always be people who are ahead of the curve, and people who are behind the curve. But knowledge moves the curve. --Bill James
Gaurav Ram
Ranch Hand

Joined: Feb 23, 2008
Posts: 32
Again thank you very much sir for explaining me so well and giving me such a good link to study, that really helpful and open many windows of my mind and also removed many hidden queries.

Thanks again Sir,
I agree. Here's the link:
subject: Instantiated bean with private Constructor
It's not a secret anymore!