Solved

Spring MVC - Unable to get bean for controller

Posted on 2010-09-16
15
2,182 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
Resolve Critical IT Incidents Fast

If your data, services or processes become compromised, your organization can suffer damage in just minutes and how fast you communicate during a major IT incident is everything. Learn how to immediately identify incidents & best practices to resolve them quickly and effectively.

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

Salesforce Made Easy to Use

On-screen guidance at the moment of need enables you & your employees to focus on the core, you can now boost your adoption rates swiftly and simply with one easy tool.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Running JavaFX on JDeveloper 12C 1 78
ejb wildfly example 2 74
Java basic valueOf question 1 34
American Express @Work site and Java 4 68
Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…

726 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