日期:2014-05-17  浏览次数:20756 次

sprng mvc注解 service注入的问题 求救。。。
最近在学spring mvc ,为新项目做技术储备
折腾了一个星期,总算有点眉目了,今天下午写了个例子,用了三层结构 controller service 还有DAO,但是运行后发现service没有注入,导致项目的功能运行不正常,调了半天还是没有结果。。。各位大神帮忙看看
好了,废话不多说,上代码。。。。

先是controller

package com.itsum.springmvcpt.bg.test.controller;


import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;

import javax.validation.Valid;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.validation.FieldError;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.view.RedirectView;

import com.itsum.springmvcpt.bg.test.benas.dtos.User;
import com.itsum.springmvcpt.bg.test.services.Iservices.Iuserservice;
import com.itsum.springmvcpt.bg.test.services.serviceImpl.UserService;


@Controller
public class UserController {


private Iuserservice service;


public Iuserservice getService() {
return service;
}

public void setService(Iuserservice service) {
this.service = service;
}

@RequestMapping("/welcome")
public ModelAndView welcome(){
ModelAndView mv=new ModelAndView();
System.out.println("service:  "+service);
List<User> list=service.findByLimit(10);
mv.addObject("list", list);
mv.setViewName("welcome");
return mv;
}
}



然后是service接口:

package com.itsum.springmvcpt.bg.test.services.Iservices;

import java.util.List;

import com.itsum.springmvcpt.bg.test.benas.dtos.User;

public interface Iuserservice {

public List<User> findByLimit(int limit);

}



service实现:

package com.itsum.springmvcpt.bg.test.services.serviceImpl;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.itsum.springmvcpt.bg.test.benas.dtos.User;
import com.itsum.springmvcpt.bg.test.dao.Idaos.UserDao;
import com.itsum.springmvcpt.bg.test.services.Iservices.Iuserservice;


@Service("service")
public class UserService implements Iuserservice{


private UserDao dao;

public UserDao getDao() {
return dao;
}
public void setDao(UserDao dao) {
this.dao = dao;