0

I have a utility class which I want to initialize when the application starts in Spring MVC. So I am implementing InitializingBean. Now I have to create an object for the same and save it in Application scope so that I can access the same instance everywhere. But I am not able to get hold of this.

Here is my try:

public class DashboardInitializer implements InitializingBean, ApplicationContextAware {

    private ApplicationContext mApplication;

    @Override
    public void afterPropertiesSet() throws Exception {

        initializeConfigurationUtil();

        ConfigurationUtil util = ConfigurationUtil.getInstance();

        /* Save the util to application scope */

    }

    @Override
    public void setApplicationContext(ApplicationContext pApplication) throws BeansException {
        this.mApplication = pApplication;
    }
}

Is this approach correct or there is a better way to do that?

divinedragon
  • 5,105
  • 13
  • 50
  • 97

2 Answers2

3

I think you need to simplify this a little bit.

You want the utility class to be initialized after your application context is loaded, but you also want your util class to be in the application context?

Seems the util class has some dependency objects configured in the application context, and the util class is in turn a dependency of some classes in the application context.

If you can express these dependencies in the form of beans (util is a bean, which has its dependency beans injected into it, and beans that need util have util injected into them), Spring will ensure that all dependencies of util are initialized first, then util is initialized and then it is injected into classes that need util.

You should not try to add something to an initialized context.. Its not possible.

If you cannot express util and its dependencies as beans, you can also take this approach: 1. Configure util as a bean in the application context, add a default constructor that does nothing. So this object would be created, but not initialized when the spring context is loaded.

  1. In the ApplicationContextAware implementation you have, modify the setApplicationContext method. Get the util bean you configured earlier from the context.

  2. You can now initialize (execute some code that you want to execute) the util instance, just make sure you do not try to reassign the bean to some other instance of util.

Hope this helps.

Akshay
  • 3,158
  • 24
  • 28
1

You can use @postconstruct annotation on methods to perform business logic immediately after the application has been initilized. And properties can simply be injected using placeholder in config and @Value annotation on java fields.

NimChimpsky
  • 46,453
  • 60
  • 198
  • 311
  • I checked for ````@PostConstruct````. This is called for each controller after the controller object is created. However, my logic is not specific to any controller, but application specific. My ConfigurationUtil is used across controllers, so I have to initialize it just after application is configured. So ````afterPropertiesSet()```` seems to be ideal choice. But I am not able to get how to store its instance in application context so that I use the same instance everywhere. I was thinking of using ````ServletContextAware````. Is it the right choice for my purpose? – divinedragon Apr 25 '13 at 12:41
  • postconstuct is not specifc to a controller – NimChimpsky Apr 25 '13 at 14:17