Solved

Spring MVC - Unable to get bean for controller

Posted on 2010-09-16
15
2,099 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
  • 7
  • 6
  • 2
15 Comments
 
LVL 20

Assisted Solution

by:ChristoferDutz
ChristoferDutz earned 300 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
 
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
Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

 
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 200 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 300 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

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

Suggested Solutions

Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
The viewer will learn how to implement Singleton Design Pattern in Java.

758 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now