Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Spring MVC - Unable to get bean for controller

Posted on 2010-09-16
15
Medium Priority
?
2,349 Views
Last Modified: 2012-05-10
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
Comment
Question by:sandeep1984
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 7
  • 6
  • 2
15 Comments
 
LVL 20

Assisted Solution

by:ChristoferDutz
ChristoferDutz earned 600 total points
ID: 33692469
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
 
LVL 20

Expert Comment

by:ChristoferDutz
ID: 33692483
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
 
LVL 2

Author Comment

by:sandeep1984
ID: 33692762
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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
LVL 20

Expert Comment

by:ChristoferDutz
ID: 33693011
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
 
LVL 2

Author Comment

by:sandeep1984
ID: 33693393
I tried @Controller("myBean"), but its not working
0
 
LVL 20

Expert Comment

by:ChristoferDutz
ID: 33695797
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
 
LVL 92

Expert Comment

by:objects
ID: 33697185
Why do you need access to you controller?
0
 
LVL 2

Author Comment

by:sandeep1984
ID: 33698683
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
 
LVL 2

Author Comment

by:sandeep1984
ID: 33698685
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
 
LVL 20

Expert Comment

by:ChristoferDutz
ID: 33698907
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
 
LVL 92

Accepted Solution

by:
objects earned 400 total points
ID: 33698939
bizarre that the taglib needs access to the controller
0
 
LVL 20

Expert Comment

by:ChristoferDutz
ID: 33752602
Have you managed to solve your problem? If not, please send us the information we need to continue helping you.
0
 
LVL 2

Author Comment

by:sandeep1984
ID: 33752679
Not able to find a solution yet.
0
 
LVL 20

Assisted Solution

by:ChristoferDutz
ChristoferDutz earned 600 total points
ID: 33752866
Could you please attach your web.xml and the context files setting up the spring-context?
0
 
LVL 2

Author Closing Comment

by:sandeep1984
ID: 34068457
Not useful
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
Suggested Courses

636 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question