Results 1 to 4 of 4

Thread: Controller can't find service

  1. #1
    Join Date
    Aug 2010
    Posts
    2

    Default 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. #2
    Join Date
    Jul 2010
    Posts
    10

    Default

    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?

  3. #3
    Join Date
    Oct 2009
    Location
    Sofia, Bulgaria
    Posts
    20

    Default

    Make sure also that you put

    <context:annotation-config/>

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

  4. #4
    Join Date
    Aug 2010
    Posts
    2

    Default

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

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •