Is it possible to have a constant valued through a Spring Service? -




we have web service 1 of parameters called origin , origin validated against code in database.

for each 1 of our services have validate code. code not change want keep in constant, still have validate prevent clients sending wrong code.

basically want this:

@service public class service {      @autowired     private logbs logbs;      // know cannot used in static context.     public static final long code = this.logbs.retrievelogwebservicecode("webservicename");      public void validateorigincode(final long origin) {         if (!origin.equals(code)) {             throw new serviceexception("wrong origin code!");        }    } } 

i know similar can done spring caching, possible constant?

i rather go this:

@service public class codevalidatorservice {      private logbs logbs;      private long code;      @autowired     public codevalidatorservice(logbs logbs){         this.logbs = logbs;         code = this.logbs.retrievelogwebservicecode("webservicename");         if (code == null){             throw new serviceexception("code cannot read db!");         }     }      public void validateorigincode(final long origin) {         if (!origin.equals(code)) {             throw new serviceexception("wrong origin code!");        }    } } 

just code review, prefer injecting dependencies in constructor rather using @autowired in field directly, makes service testable. try read code in @postconstruct method, think it's better in constructor have service in ready-to-go state.

for using in rest of services, inject codevalidatorservice instance on them:

@service public class otherservice {      private codevalidatorservice codevalidatorservice;      @autowired     public otherservice(codevalidatorservice codevalidatorservice){         this.codevalidatorservice = codevalidatorservice;     }      public void performaction(final long origin) {         codevalidatorservice.validateorigincode(origin);         //do rest of logic here    } } 

see also:





wiki

Comments

Popular posts from this blog

Asterisk AGI Python Script to Dialplan does not work -

python - Read npy file directly from S3 StreamingBody -

kotlin - Out-projected type in generic interface prohibits the use of metod with generic parameter -