Announcement Announcement Module
No announcement yet.
Instantiating Spring classes directly Page Title Module
Move Remove Collapse
Conversation Detail Module
  • Filter
  • Time
  • Show
Clear All
new posts

  • Instantiating Spring classes directly

    We're trying to set up a central message formatter for our exception handling, using Spring's MessageSourceAccessor. We want to make static calls to this formatter, instead of injecting the MSA into all our domain objects (lots of code and overhead).

    Unfortunately, I'm having trouble figuring out how to get a SingletonBeanFactoryLocator set up. What I would like is to have a reference to the app context that's already set up available statically, and call the MSA as needed. Alternately, I'm thinking about just creating my own MSA in the static class (outside of Spring) and using that.

    Is there any reason I can't just create a MessageSourceAccessor on my own and use it as needed? I don't really NEED Spring to manage this bean.

  • #2
    Alternatively you could do the following:
    Leave the configuration in the app context and create an additional bean. Inject the accessor to that bean, which should provide a normal getter but stores the reference in a static field.
    That way you could use a static getter method to obtain the reference from anywhere.
    You have just to ensure that initialization happened before first access.