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:
- call init() waits until bean's init() termination, sure @ time of container lifecycle called.
- you lost possibility of lazy-init,
- but gain possibility recover error (maybe accepting work without data in memory)
with initializingbean
:
- have care bean' scope "singleton" (else every time wire bean, init() method called),
- you lost possibility recovery in case of failure because bean's initialization cycle not in hands (or more difficult recover)
- 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
Post a Comment