• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 2417
  • Last Modified:

Spring MVC - Unable to get bean for controller

Hi,

I am using Spring MVC 3.0. I have created an annotation based controller and it is working fine.
I have a requirement to get the controller bean from the application context.

I have tried with ApplicationContextAware and I am able to get the bean, but If I try with following code, its failing. The requirement is that, I need to do this way.
Need help on the same.
WebApplicationContext appContext = WebApplicationContextUtils.getRequiredWebApplicationContext(getServletContext());
System.out.println("RegistrationComponentController : " + ctx.containsBean("beanName"));

Open in new window

0
sandeep1984
Asked:
sandeep1984
  • 7
  • 6
  • 2
3 Solutions
 
ChristoferDutzCommented:
I guess you meant "appContext.containsBean("beanName");"?

If you are I am using excatly that approach in a HttpSessionListener (outside the Spring context) and it's working like a charm.
0
 
ChristoferDutzCommented:
Perhaps you should explain in a little more detail what you are trying to do. Perhaps then we will be able to help you. As I mentioned, your code should work, as long as your spring context is a WebApplicationContext and is configured in the same webapp.
0
 
sandeep1984Author Commented:
Yes, I meant appContext.containsBean("beanName")
I am trying to get the bean for a controller (Its an annotation based controller).

@Controller
public class MyController
{
  .....
}

I did not create bean definition, since its taken care by the controller annotation.
I am trying to get the bean using WebApplicationContext which is not able to find the bean defined for controller. It works if I explicitly define bean in web spring xml file.
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
ChristoferDutzCommented:
As far as I know you have to provide an Id somewhere if you want to access it by id (You are trying to access it by its id in "containsBean"). The way I understood the Controllers, you generally create an anonymous instance that is identified by a strategy depending on the dispatcher you are using (I usually use the name: name="/myHandler.html". You can pass a name to the controller to make it non-anonymous:


@Controller("myCoolBean")

By this you should be able to look it up by id.

If this also doesn't work. I'd suggest setting a breakpoint in line 2 and having a look at what's actually in the context. This might take some time but it will definitely tell you what's going wrong.
0
 
sandeep1984Author Commented:
I tried @Controller("myBean"), but its not working
0
 
ChristoferDutzCommented:
Well then set a breakpoint and have a look at what you can find in the context. Are you sure the Controller bean is instantiated? It could also be possible that Spring never finds and instantiates the bean. You have to have the following definition in your context:


0
 
objectsCommented:
Why do you need access to you controller?
0
 
sandeep1984Author Commented:
Yes, I do have the above statement (<context:component-scan base-package="... package that should be scanned ..."/>)
Also controller bean is getting created and is accessible if I use ApplicationContextAware.
But its not available in WebApplicationContext. I tried debugging, none of the controller instances (beans) are available here.
:(
0
 
sandeep1984Author Commented:
Objects : I am using a taglib which takes controller bean as input and it tries to get the bean using WebApplicationContext. I do not want to change the taglib.
0
 
ChristoferDutzCommented:
Well in this case I would simply assume that the application contexts are different. It is sometimes to have a Global Spring Context and several sub-contexts for each MVC Dispatcher Servlet. If your Controller is created in one of these sub-contexts, I think you will not find it in the global one (The one you are accessing). Do you have such a setup? If yes, it would be good to describe your setup in a little more detail.
0
 
objectsCommented:
bizarre that the taglib needs access to the controller
0
 
ChristoferDutzCommented:
Have you managed to solve your problem? If not, please send us the information we need to continue helping you.
0
 
sandeep1984Author Commented:
Not able to find a solution yet.
0
 
ChristoferDutzCommented:
Could you please attach your web.xml and the context files setting up the spring-context?
0
 
sandeep1984Author Commented:
Not useful
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

  • 7
  • 6
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now