• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

restriction on resource injection

 
Sudarshan Sreenivasan
Ranch Hand
Posts: 188
IntelliJ IDE Java MySQL Database
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Each resource may only be injected into a single field or method


Why this restriction ?
 
Amandeep Singh
Ranch Hand
Posts: 850
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
example-



Consider this above example, if there is no restriction, which one you will prefer as the Column name for the same instance field?
 
Sudarshan Sreenivasan
Ranch Hand
Posts: 188
IntelliJ IDE Java MySQL Database
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
Thanks for you reply I kind of got the picture however I was inquiring why the restriction of injected the same resource into different fields of the same class.

I deciphered this restriction after reading the below section from Mastering Ejb 3.0.

Please correct me if i am wrong.

Each resource may only be injected into a single field or method for particular bean.If you need to use a resource in more than one spot in the bean class, you need to define that resource at the class level & then look it up explicitly by name
 
Karnati Sudhakar
Ranch Hand
Posts: 270
Eclipse IDE Ubuntu
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
sid sree wrote:
Each resource may only be injected into a single field or method


Why this restriction ?


I found something related to this on the Sun blogs and quite nicely explained there.Eighth point in this blog.

Regards
Sudhakar
 
Sudarshan Sreenivasan
Ranch Hand
Posts: 188
IntelliJ IDE Java MySQL Database
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks a lot i think the blog was superb however it still has not answered my question.

I have elaborated on it in my previous post.Will be grateful if someone could clarify it. :?:


Thanks
Sudarshan
 
Mihai Radulescu
Ranch Hand
Posts: 918
IntelliJ IDE Java Linux
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi guys,

I think that behind this affirmation is the JNDI or more precisely what happen when a resource is injected - the EJB container bounds the injected resource with a name. So if you inject the same resource two time you have the bound two times for the same resource and this can lead to confusions. Is similar with the code :


you will have two variables which are pointing to the same thing.

If you do a look up(how is suggested) then you'll use this mapping (which is doned only one) how many times you need it.


Regards,
M
 
Sudarshan Sreenivasan
Ranch Hand
Posts: 188
IntelliJ IDE Java MySQL Database
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
String Myname = "sid";
String Myname1="sid";


I think the above resembles more closely what i am trying to ask.

What if the same resource is injected into 2 different fields of the same class ??

Why is this no permitted ??
 
Mihai Radulescu
Ranch Hand
Posts: 918
IntelliJ IDE Java Linux
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi Sid,

Pretty fast answer, I don't think that this is more not "desirable". The DI mechanism is triggered two times and second time is not justified because in the bean context the resource already exist (with a certain name).
Why you think that this is not permitted ?

Regards,
M
 
Sudarshan Sreenivasan
Ranch Hand
Posts: 188
IntelliJ IDE Java MySQL Database
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I guess its not desirable but i was asking this from the exam point of view it should however be allowed i think.

However the quote above implies that it is not allowed :?:

Any thoughts on this ??
 
Mihai Radulescu
Ranch Hand
Posts: 918
IntelliJ IDE Java Linux
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi

Desirable = you can do it if you consider but this can have some latter consequences.
I'll do a test, latter, to see if this is really a bad technique or a "not allowed" stuff.

Regards,
M
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic