• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Injection of Simple Environment Entries Using Annotations

 
Padma Priya
Ranch Hand
Posts: 125
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all,

I have a small doubt regarding "Injection of Simple Environment Entries Using Annotations".

If i have the code as shown below

@Stateless public class EmployeeServiceBean
implements EmployeeService
{
...
// The maximum number of tax exemptions, configured by Deployer
@Resource int maxExemptions;

// The minimum number of tax exemptions, configured by Deployer
@Resource int minExemptions;

public void setTaxInfo(int numberOfExemptions,...) throws InvalidNumberOfExemptionsException {
...
// Use the environment entries to customize business logic.
if (numberOfExemptions > maxExemptions ||numberOfExemptions < minExemptions)
throw new InvalidNumberOfExemptionsException();
}
}


How can i specify the values of the simple Environment entries maxExemptions and minExemptions?

Can anybody show me the dd snippet of it. Also can't we specify the value directly in the bean code as follows

@Resource int maxExemptions = 4;

The spec says

To support this case, the container must only inject a value for the environment entry if the application
assembler or deployer has specified a value to override the default value.


Can anybody explain the above statement. Does this mean that we should always specify <env-entry-value> in the dd even after we have given a default value in @Resource int maxExemptions = 4;. But that doesn't make sense.


Thank you all in advance.

With Regards
Deepthi

 
Prasad Kumbhare
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here is one sample DD entry of env vairable.


Hope this helps.
About your second question I am really not sure. So I will try this and let you know.

Thanks,
Prasad
 
Alexey Saenko
Ranch Hand
Posts: 30
Chrome Eclipse IDE Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Deepthi Tirunahari wrote:The spec says
To support this case, the container must only inject a value for the environment entry if the application
assembler or deployer has specified a value to override the default value.

Can anybody explain the above statement. Does this mean that we should always specify <env-entry-value> in the dd even after we have given a default value in @Resource int maxExemptions = 4;. But that doesn't make sense.


I thinks this means the following: if a value for the env-entry has been specified, container must and will inject it. If there is no value in env-entries, container must not and will not inject this value and default value will be used. Also, env-entries always overrides default values (and those which were set in code, like int x = 4), because firstly jvm must create an object (or primitive) and then read the value from DD.

So, we can write down something like this in the bean code:
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic