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

LVL 2
sandeep1984Asked:
Who is Participating?
 
objectsConnect With a Mentor Commented:
bizarre that the taglib needs access to the controller
0
 
ChristoferDutzConnect With a Mentor Commented:
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
Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
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
 
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
 
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
 
ChristoferDutzConnect With a Mentor Commented:
Could you please attach your web.xml and the context files setting up the spring-context?
0
 
sandeep1984Author Commented:
Not useful
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.