• 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Bear Bibeault
  • Junilu Lacar
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • salvin francis
  • Frits Walraven
Bartenders:
  • Scott Selikoff
  • Piet Souris
  • Carey Brown

Does annotation autowiring violates Java’s encapsulation?

 
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All,

I am new to Spring. Please clarify this doubt. In Spring, using annotation autowiring, private variables can be accessed (i.e. while creating a bean, annotation autowiring is used to set value to an attribute of an object, even if it is declared as private). Does it not violates Java's encapsulation (i.e Information hiding)?

Another scenario:

Here, I have declared the constructor as private because i dont want anybody creating object for this class from outside. But through annotation autowiring Spring can create an object which breaks by intention. Please clarify this,.. I may be wrong in understanding... Does it mean through annotation we can break Java's encapsulation?

Regards,
G. Kamal
 
Ranch Hand
Posts: 180
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Not really the concept of encapsulation is more towards the clients that will use your code to do some business logic. these clients cannot access your private members so your encapsulation is preserved.
technical frameworks like spring hibernate will access your private member for technical initialisation only, they are not client of your business api.
I hope that makes sense to you
 
kamalakannan kamal
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ya, it makes sense. Thanks for the clarification.
    Bookmark Topic Watch Topic
  • New Topic