2
votes

For a bean :

@Autowired
private MyBean myBean;

Is this bean autowired before :

@RequestMapping("VIEW")
public String showView { 
  //myBean is null here
}

Reason I'm asking is that myBean is set to null. However for other beans which have

@RenderMapping("NORMAL")
public String renderNormal{ 
  //myBean is not null here
}

myBean seems to be autowired correctly.

Is @RequestMapping("VIEW") causing myBean to not be wired or is being invoked before Spring autowires myBean ? If so how I can I configure Spring to wire myBean before entering method showView ?

Update : I was missing <context:annotation-config /> in Spring context file, this question provides some detail on its use : Difference between <context:annotation-config> vs <context:component-scan>

2
is your showView method in a class with annotation @Controller?Shailesh Vaishampayan
You need to show more Spring related code so we can figure out what's going ongeoand
You seem to be mixing concepts of Spring MVC and Spring beans.Droidekas

2 Answers

0
votes

The alone @RequestMapping annotation doesn't make your class a Spring bean.

Usually, the @RequestMapping goes hand-by-hand with the @Controller annotation (which is extends the @Component annotation) so that it's considered as a Spring bean and (according to the javadoc) to

represent a component that receives HttpServletRequest and HttpServletResponse instances

0
votes

Assuming your showView Method is in a class annotated with annotation @Controller, you should have

  <context:component-scan base-package="your.package.path" />

where your.package.path is path in which you have your controller classes.

Also please see follwoing link for example:Auto component scan example