问题描述
我搭建一个Spring MVC 环境。controller中加上@Controller注解@Controllerpublic class Login { private IUserService userService; public IUserService getUserService() { return userService; } public void setUserService(IUserService userService) { this.userService = userService; } @RequestMapping("login.html") public String login(HttpServletRequest request, HttpServletResponse response, ModelAndView view){ System.out.println("handle the login.html"); System.out.println(userService.getUserByCustId("000001031234").getUserMail()); return "index"; }}在spring的配置文件中配置注解扫描等。同时配置 <bean id="loginController" class="com.sdemo.controller.Login" > <property name="userService" ref="userService" /> </bean>注入userService。问题来了:如果不加这个注入项目可以正常运行,如果加了这个注入。提示异常org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping': Initialization of bean failed; nested exception is java.lang.IllegalStateException: Cannot map handler 'loginController' to URL path [/login.html]: There is already handler of type [class com.sdemo.controller.Login] mapped.请问该怎么办呢?问题出在哪里?注解和注入不能并存么?
解决方案
userService有没配置或注解啊
解决方案二:
@Serviceprivate IUserService userService; 去掉配置 <bean id="loginController" class="com.sdemo.controller.Login" > <property name="userService" ref="userService" /> </bean>
解决方案三:
@Autowiredprivate IUserService userService;
解决方案四:
sky_sz 正解。我前端时间做过。
解决方案五:
这句日志你没有看懂吗?Cannot map handler 'loginController' to URL path [/login.html]: There is already handler of type [class com.sdemo.controller.Login] mapped. 答案就在这里。
解决方案六:
将注解进行到底!