0

I have a spring core application, where I have two context files for different channels namely mobile & web.

If I get request from mobile channel , mobile-context.xml will be loaded If I get request from web channel , web-context.xml will be loaded

Where I have ClassA, which is added in both of the context files, so its loading twice as per the Spring's context loading concept since both are different context.

Is there anyway that I can load this ClassA once irrespective of the context, or can I share the ClassA object in both the context

Hari M
  • 19
  • 1
  • 9

1 Answers1

0

Creating multiple contexts is not new. You can achieve this by creating two ApplicationContext instances.

Something like below would do.

ApplicationContext webContext= new ClassPathXmlApplicationContext("web-context.xml");
ApplicationContext mobileContext= new ClassPathXmlApplicationContext("mobile-context.xml");

Now that you have two contexts, You can use them as you like.

If you are looking to provide different business logic based on device type, I would advise you to look at Spring-Mobile project. This project adds on to existing spring projects and enables device detection and routing to different views based on detected device.

This allows you to detect device at Request Handling level and lets you handle the request differently for different devices.

This question has more details that you might want.

Raja Anbazhagan
  • 4,092
  • 1
  • 44
  • 64