user3150469
user3150469

Reputation: 21

Sharing Spring beans accross multiple servlets

I am kind of new to spring, My requirement is to have users login through multiple servlets,

ex: all kinds of users goes to Dispatcher Servlet but Admin goes through a different servlets to ensure additional security, How would I share beans defined in Dispacher servlet Spring context beans to other servlets in same web application context.

also I like to have one more servlet for accesing Ajax requests,

All these servlets should share same HttpSession and beans, Will be more helpful to see a sample configuration code.

Upvotes: 2

Views: 1328

Answers (1)

Mehran Hatami
Mehran Hatami

Reputation: 12961

You have 3 ways:

1- Use ContextLoaderListener to create a parent context, your serlvets would become children of that context., and define your beans in the parent context.

Check this link

2- the other way you have is to load the other servlet's xml file manually, like this:

ApplicationContext context =
                    new ClassPathXmlApplicationContext("classpath:application-context.xml");
Person person = (Person)context.getBean("person");

3- and you can also import your servlet beans into other servlet using this:

<import resource="classpath:application-context.xml"/>

Upvotes: 3

Related Questions