• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Spring: Setter Injection on non-public class

 
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have a non-public bean class in the package "demo.beans" with some setter methods.


I am setting these values in the Spring configuration file. But, setter injection should be failed since my bean class is in different package and BeanFactory (or XmlBeanFactory) is in different package.

Spring Client Application:


Configuration file:


But, it is running normally. How BeanFactory was able to access my bean class ( with default access ), which is in the other package? How JVM allows this?
 
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In the config file(demo.xml), you will be writing the bean with the fully qualified class path. From there withe default access specifier bean is getting instantiated and you are able to run your spring application successfully.

Thanks
Veeresh
 
S Reddy
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Even if it knows that my bean class is there.... it shouldn't access my class because both are in different packages.

According to java rules, only public classes can be accessed from other packages. For this reason only we will specify the class which has main() method (it should be invoked by JVM) as public. Right?
 
S Reddy
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I got following answer from Java_Official Yahoo Group.

I think this is because Spring use reflection to initiate your bean and invoke its method.
So even the bean class itself is not visible for the bean factory it still can access it.

 
Tick check! Okay, I guess that was just an itch. Oh wait! Just a tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic