Spring newbie here.
I observed that Spring was able to instantiate a non-public class (i.e. a class with default visibility) that I had defined. Can anyone tell me how Spring achieves this? Why is this allowed ?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
OK, here’s how they do it. Take this sample class:
The above is a package-private class with a private constructor in a different package, but we’ll still instantiate it:
Code (run from a class in a different package):
Output:
Of course what Spring does is much more complex, because they also deal with Constructor Injection etc., but this is how to instantiate invisible classes (or invisible constructors).