How do you load a bean into Spring MVC's application context?

How do you load a bean into Spring MVC's application context?

As I understand it, Spring MVC application has two distinct contexts, the application context and the web context, which are controlled by applicationContext.xml and dispatcher-servlet.xml, respectively.

Inside my controllers, how do I go about loading a bean into either of these contexts?

Note that I am aware of Getting Spring Application Context. That would answer my question for a stand alone application. Where I would use a factory to load the application context from the xml file, but this seems like the wrong way to go about loading beans in Spring MVC.

Something like EJB wiring in Spring for non EJB's


How to get the id of a bean from inside the bean in Spring?
Matt is absolutely correct.

How should I redirect the second page in Spring framework
You should not need with any kind of bean-loading/instantiating code in your MVC application, otherwise you're doing something wrong.

jdbcTemplate hangs on long update
You define your beans inside the according spring XML configuration files..
Tracking down cause of Spring's “not eligible for auto-proxying”
<bean id="pinboardServiceTarget" class="">     <property name="pinboardEntryDao" ref="pinboardEntryDAO"/> </bean> ... 

How can I easily mock out a static method in Java (jUnit4)
<bean id="pinboardEntryDAO" class="com.lifepin.daos.PinboardEntryDAO"> <property name="sessionFactory" ref="sessionFactory"/> </bean>
Your PinboardService class (this is just from an application I wrote recently) will have a property IPinboardEntryDAO like.
How can one determine page render time in Spring (on Google App Engine/Java)?
public class PinboardService implements IPinboardService{   private IPinboardEntryDAO pinboardEntryDao;    ... 

How can I have previous value of checkbox in Spring?
public void setPinboardEntryDAO(IPinboardEntryDAO dao){ this.pinboardEntryDao = dao; } public IPinboardEntryDAO getPinboardEntryDAO(){ ...

} ...

} public class PinboardEntryDAO implements IPinboardEntryDAO{ ...

Note that inside the the PinboardService class I'm using the DAO interface, not the implementation itself, while in the configuration I'm then injecting the real implementation PinboardEntryDAO.

This is a very good practice for separating the different layers (presentation, service and data layer)..


Although a Spring MVC application has two distinct contexts, the web context has access to all the beans loaded in the application context.

The application context however cannot access beans in the web context.

This is used to enforce separation of concerns, e.g.

business rules class does not need to know about the HTTP session.

So if you have a bean you need access to from both contexts it will have to be declared within the application context..


Any dependencies that your Controller has (such as on service-layer classes, DAOs, etc) should be expressed as normal - through injection, either constructor injection or setter injection.. The context where the controller is mapped just wires it up with any dependencies it needs as normal.

The Controller code never needs to work with Spring directly to get any beans, it is wired up with them..


You should use dependency injection and your config files to load beans into your controllers, but if you do need to access the application context directly, any Controller that extends AbstractController (or any of its descendents) has access to the getApplicationContext() method..


In stand alone application we can user context.Refresh() it will reloading/re-instantiating the new requested beans the old beans will have the old instance only.. In web applications we need to overwrite the ContextLoaderListener and call the contextInitialized().


You need to import the file containing the bean definitions of the service layer(say, service-context.xml) into the new project.

It can be done as:.
<import  resource="classpath:service-context.xml"/> 

88 out of 100 based on 78 user ratings 478 reviews