spring - Use an ApplicationContextListener or @PostConstruct to -


in 1 of our beans reading file based data in memory.

would better applicationcontextlistener e.g. calling beans init() method, or adding @postconstruct init() method container automatically?

you can use:
1. @postconstruct
2. initializingbean interface
3. <bean class="your bean class" init-method="your init method"/>

attribute : init-method: name of custom initialization method invoke after setting bean properties. method must have no arguments, may throw exception. alternative implementing spring's initializingbean interface or marking method postconstruct annotation.

they alternative: if program totally annotated go annotation, if xml go xml (i don't mixing , don't need ask if implements feature annot or xml)

edit:

  • context listener: called every context refresh (usually once, @ startup)
  • initializingbean or @postconstruct: apply bean's lifecycle called every time bean created (depends scope)

in case using context listener:

  1. call init() waits until bean's init() termination, sure @ time of container lifecycle called.
  2. you lost possibility of lazy-init,
  3. but gain possibility recover error (maybe accepting work without data in memory)

with initializingbean :

  1. have care bean' scope "singleton" (else every time wire bean, init() method called),
  2. you lost possibility recovery in case of failure because bean's initialization cycle not in hands (or more difficult recover)
  3. you gain possibility lazy-init (startup time reduced)

in case, if using singleton bean without lazy-init , don't need error recovery in case of failure, there not differences @ all


Comments

Popular posts from this blog

plot - Remove Objects from Legend When You Have Also Used Fit, Matlab -

java - Why does my date parsing return a weird date? -

Need help in packaging app using TideSDK on Windows -