James McMahon
James McMahon

Reputation: 49629

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.

Upvotes: 2

Views: 20358

Answers (6)

Juri
Juri

Reputation: 32900

Matt is absolutely correct. You should not need with any kind of bean-loading/instantiating code in your MVC application, otherwise you're doing something wrong. You define your beans inside the according spring XML configuration files.

<bean id="pinboardServiceTarget" class="com.lifepin.services.PinboardService">
    <property name="pinboardEntryDao" ref="pinboardEntryDAO"/>
</bean>
...
<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

public class PinboardService implements IPinboardService{
  private IPinboardEntryDAO pinboardEntryDao;

  ...

  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).

Upvotes: 4

amyzhou
amyzhou

Reputation: 11

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"/>

Upvotes: 1

suresh
suresh

Reputation: 31

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()

Upvotes: 1

Rafe
Rafe

Reputation: 3066

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.

Upvotes: 1

matt b
matt b

Reputation: 139921

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.

Upvotes: 1

D&#243;nal Boyle
D&#243;nal Boyle

Reputation: 3079

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.

Upvotes: 4

Related Questions