SpringMVC 注解问题,调了一下午也不知道是什么原因

J2EE 码拜 9年前 (2016-03-21) 1126次浏览
SEVERE: Context initialization failed
org.springframework.beans.factory.BeanCreationException: Error creating bean with name “userController”: Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private com.toyor.truck.service.UserService com.toyor.truck.controller.UserController.userService; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [com.toyor.truck.service.UserService] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:334)
=================================
controller
package com.toyor.truck.controller;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import com.toyor.truck.model.User;
import com.toyor.truck.service.UserService;
@Controller
public class UserController {
@Autowired
private UserService userService;
@Autowired
private User user;
@RequestMapping(“/login.do”)
public String login(String username, String password, HttpServletRequest request) {
if (username==null) return “redirect:login.jsp”;
user = userService.login(username, password);
if(user != null){
request.getSession().setAttribute(“userInfo”, user);
return “main/index”;
}else {
return “redirect:login.jsp”;
}
}
@RequestMapping(“/getMenuBuf.do”)
public String getMenuBuf(HttpServletResponse response){
return userService.getMenuBuf(user, response);
}
/* public User getUser() {
return user;
}*/
/* public UserService getUserService() {
return userService;
}*/
/* public void setUser(User user) {
this.user = user;
}*/
/* public void setUserService(UserService userService) {
this.userService = userService;
} */
}
====================================
Service
package com.toyor.truck.service;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.toyor.truck.dao.UserDao;
import com.toyor.truck.model.User;
@Service(“userService”)
public class UserService {
@Autowired
private UserDao userDao;
public User login(String username, String password) {
return userDao.login(username, password);
}
public String getMenuBuf(User user, HttpServletResponse response) {
return userDao.getMenuBuf(user, response);
}
/* public void setUserDao(IUserDao userDao) {
this.userDao = userDao;
}
public IUserDao getUserDao() {
return userDao;
} */
}
===========================================
UserDao
package com.toyor.truck.dao;
import javax.servlet.http.HttpServletResponse;
import org.springframework.stereotype.Repository;
import com.toyor.truck.model.User;
@Repository(“userDao”)
public interface UserDao {
public User login(String username, String password);
public String getMenuBuf(User user, HttpServletResponse response);
}
===========================
UserDaoImpl
package com.toyor.truck.dao.impl;
import java.io.PrintWriter;
import java.util.List;
import javax.servlet.http.HttpServletResponse;
import net.sf.json.JSONArray;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import com.toyor.truck.dao.UserDao;
import com.toyor.truck.model.Permission;
import com.toyor.truck.model.User;
import com.toyor.truck.model.UserMenu;
import com.toyor.utils.pubutil.MyBatisUtils;
public class UserDaoImpl implements UserDao {
public User login(String username, String password) {
SqlSessionFactory factory = MyBatisUtils.getFactory();
SqlSession session = factory.openSession(true);
String statement = “com.toyor.truck.model.userMapper.getUserByName”;
User ruser = session.selectOne(statement, username);
statement = “com.toyor.truck.model.userMapper.getPermission”;
List<Permission> list = session.selectList(statement);
JSONArray jsArray = JSONArray.fromObject(new UserMenu().getTree(0, list));
ruser.setUsermenu(jsArray.toString());
session.close();
return ruser;
}
public String getMenuBuf(User user, HttpServletResponse response) {
// TODO Auto-generated method stub
try {
if (user != null){
response.setContentType(“text/html;charset=UTF-8”);
PrintWriter out = response.getWriter();
out.print(user.getUsermenu());
}
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
return null;
}
}
==================================
User
package com.toyor.truck.model;
import javax.persistence.Entity;
@Entity
public class User {
private String username;
private String password;
private String rolename;
private String usermenu;
public User() {
super();
// TODO Auto-generated constructor stub
}
public String getPassword() {
return password;
}
public String getRolename() {
return rolename;
}
public String getUsermenu() {
return usermenu;
}
public String getUsername() {
return username;
}
public void setPassword(String password) {
this.password = password;
}
public void setRolename(String rolename) {
this.rolename = rolename;
}
public void setUsermenu(String usermenu) {
this.usermenu = usermenu;
}
public void setUsername(String username) {
this.username = username;
}
}
=========================
springmvc-servlet
<?xml version=”1.0″ encoding=”UTF-8″?>
<beans xmlns=”http://www.springframework.org/schema/beans”
xmlns:context=”http://www.springframework.org/schema/context”
xmlns:p=”http://www.springframework.org/schema/p”
xmlns:mvc=”http://www.springframework.org/schema/mvc”
xmlns:xsi=”http://www.w3.org/2001/XMLSchema-instance”
xsi:schemaLocation=”http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd”>

<!– 对web包中的全部类进行扫描,以完成Bean创建和自动依赖注入的功能 –>
<context:component-scan base-package=”com.toyor.truck.controller” />
<!– don”t handle the static resource –>
<mvc:default-servlet-handler />
<mvc:annotation-driven />
<!–对模型视图名称的解析,即在模型视图名称添加前后缀 –>
<mvc:interceptors>
<bean class=”com.toyor.interceptor.MyInterceptor”></bean> <!– 拦截全部springmvc的url! –>
<mvc:interceptor>
<mvc:mapping path=”/user.do” />
<bean class=”com.toyor.interceptor.UserInterceptor”></bean>
</mvc:interceptor>
</mvc:interceptors>
<bean class=”org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter” />
<bean class=”org.springframework.web.servlet.view.InternalResourceViewResolver”
p:prefix=”/jsp/” p:suffix=”.jsp”/>
</beans>

解决方案

15

<!– 对web包中的全部类进行扫描,以完成Bean创建和自动依赖注入的功能 –>
<context:component-scan base-package=”com.toyor.truck.controller” />
改成
<context:component-scan base-package=”com.toyor.truck.*” />

3

同上+1  扫描包的路径不对~

2

同上+1  扫描包的路径不对~

CodeBye 版权所有丨如未注明 , 均为原创丨本网站采用BY-NC-SA协议进行授权 , 转载请注明SpringMVC 注解问题,调了一下午也不知道是什么原因
喜欢 (0)
[1034331897@qq.com]
分享 (0)