2

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>

Community
  • 1
  • 1
blue-sky
  • 51,962
  • 152
  • 427
  • 752

2 Answers2

0

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

Shailesh Vaishampayan
  • 1,766
  • 5
  • 24
  • 52
0

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

Konstantin Yovkov
  • 62,134
  • 8
  • 100
  • 147