Announcement Announcement Module
Collapse
No announcement yet.
Controller can't find service Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Controller can't find service

    I am new to Spring and I can't find out why my controller raises a null pointer exception.

    My controller's code

    Code:
    @Controller
    @RequestMapping("/home")
    public class HomeController {
    
    	@RequestMapping(method = RequestMethod.GET)
    	protected ModelAndView handleRequestInterval(
    			HttpServletRequest request,
    			HttpServletResponse response) throws Exception{
    		List<String> recentRants = rantService.getRecentRants(); //this fails
    		return new ModelAndView("home","rants",recentRants);
    		
    	}
    
    	private RantService rantService;
    	public void setRantService(RantService rantService) {
    		this.rantService = rantService;
    	}
    	
    }
    My service
    Code:
    @Service
    public class RantService {
    	@Autowired
    	public List<String> getRecentRants(){
    		List<String> l = new ArrayList<String>();
    		l.add("one");
    		l.add("two");
    		return l;
    	}
    }
    My service-conf.xml
    <context:component-scan base-package="net.nacho.services" />

    that points to the package where my Service is placed


    I was looking for similar problems-solutions but no luck. Anyone could give me a clue?

  • #2
    You have to Autowire rentService, otherwise Spring doesn't know you want to "dependency inject" the service there, so your controller looks like:

    Code:
    @Controller
    @RequestMapping("/home")
    public class HomeController {
    
           .......
    
           @Autowired
    	private RantService rantService;
    	public void setRantService(RantService rantService) {
    		this.rantService = rantService;
    	}
    	
    }
    Or you could put the annotation in the setter to, or even just don't write any setter, but that's a matter of taste.

    It seems to me you still need some reading to understand the basics of th IOC You are you annotating the getRecentRants method with @Autowired?

    Comment


    • #3
      Make sure also that you put

      <context:annotation-config/>

      in your application context XML file (service-conf.xml)

      Comment


      • #4
        Originally posted by hernani View Post
        You have to Autowire rentService, otherwise Spring doesn't know you want to "dependency inject" the service there, so your controller looks like:

        Code:
        @Controller
        @RequestMapping("/home")
        public class HomeController {
        
               .......
        
               @Autowired
        	private RantService rantService;
        	public void setRantService(RantService rantService) {
        		this.rantService = rantService;
        	}
        	
        }
        Or you could put the annotation in the setter to, or even just don't write any setter, but that's a matter of taste.

        It seems to me you still need some reading to understand the basics of th IOC You are you annotating the getRecentRants method with @Autowired?
        Thanks, I only needed to add the @Autowired annotation to my service.

        Comment

        Working...
        X