当前位置:网站首页>2021-06-18 SSM项目中自动装配错误
2021-06-18 SSM项目中自动装配错误
2022-07-27 14:55:00 【虎刀蛇邪】
问题
最近在整合Spring mybatis 和 SpringMVC过程中出现了错误
在项目Controller层调用Dao层的过程中
import com.mybatis.pojo.MyUser;
import org.apache.ibatis.annotations.Mapper;
import org.springframework.stereotype.Repository;
import java.util.List;
/** * /* * * 使用Spring自动扫描MyBatis的接口并装配 (Spring将指定包中所有被@Mapper注解标注的接口自动装配为MyBatis的映射接口 * */
@Repository("userDao")
@Mapper
/* * 使用Spring自动扫描MyBatis的接口并装配 (Spring将指定包中所有被@Mapper注解标注的接口自动装配为MyBatis的映射接口 */
public interface UserDao {
public MyUser selectUserById(Integer uid);
public List<MyUser> selectAllUser();
public int addUser(MyUser user);
public int updateUser(MyUser user);
public int deleteUser(Integer uid);
}
Controller层
mport com.mybatis.pojo.MyUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import javax.annotation.Resource;
import java.util.List;
@Controller("userController")
public class UserController {
@Autowired
private UserDao userDao;
//自动注入一个实例对象
public void selectById(){
MyUser user= userDao.selectUserById(1);
System.out.println(user);
System.out.println("============================");
}
}
在test中加载SpringApplicationContext并且运行报错
@Test
public void test01(){
ApplicationContext applicationContext=new ClassPathXmlApplicationContext("ApplicationContext.xml");
System.out.println(applicationContext);
}
此时报错
无法自动装配。存在多个 'UserDao' 类型的 Bean。Beans:userDao (UserDao.java)userDao (UserDao.java)
改正
将controller代码改成
@Resource(name ="userDao")
private UserDao userDao;
加载成功
org.springframework.context.support.ClassPathXmlApplicationContext@64c64813: startup date [Fri Jun 18 15:40:21 CST 2021]; root of context hierarchy
反思
在首次整合SSM框架时
对Spring注解自动装配原理不熟悉
| 注解名称 | 描述 | |
|---|---|---|
| 1 | @Component | 可以使用此注解描述 Spring 中的 Bean,但它是一个泛化的概念,仅仅表示一个组件(Bean),并且可以作用在任何层次。使用时只需将该注解标注在相应类上即可。 |
| 2 | @Repository | 用于将数据访问层(DAO层)的类标识为 Spring 中的 Bean,其功能与 @Component 相同。 |
| 3 | @Service | 通常作用在业务层(Service 层),用于将业务层的类标识为 Spring 中的 Bean,其功能与 @Component 相同。 |
| 4 | @Controller | 通常作用在控制层(如 Struts2 的 Action),用于将控制层的类标识为 Spring 中的 Bean,其功能与 @Component 相同。 |
| 5 | @Autowired | 用于对 Bean 的属性变量、属性的 Set 方法及构造函数进行标注,配合对应的注解处理器完成 Bean 的自动配置工作。默认按照 Bean 的类型进行装配。 |
| 6 | @Resource | 其作用与 Autowired 一样。其区别在于 @Autowired 默认按照 Bean 类型装配,而 @Resource 默认按照 Bean 实例名称进行装配。 |
| 7 | @Qualifier | 与 @Autowired 注解配合使用,会将默认的按 Bean 类型装配修改为按 Bean 的实例名称装配,Bean 的实例名称由 @Qualifier 注解的参数指定。 |
以及对于Spring IOC容器和Spring工作原理不够熟悉
边栏推荐
- Array analog linked list
- 分享一个网上搜不到的「Redis」实现「聊天回合制」的方案
- Ten thousand words analysis ribbon core components and operation principle
- 牛客题目——最小的K个数
- Codeforces Round #100 E. New Year Garland & 2021 CCPC Subpermutation
- The class declared by the scala object keyword directly calls methods and associated objects
- Log4j.jar and slf4-log4 download link
- Build a cloud native file system for media assets
- JDBC连接数据库
- mysql视图及存储过程
猜你喜欢

Layoff quarrel, musk: I'm too hard; Mercilessly open source a public opinion acquisition project; Feature engineering is as simple as parameter adjustment?! Nerf boss shouted that he couldn't move; Cu

Flex弹性盒布局2

Advanced pointer of C language

JDBC连接数据库
mvc和mvp和mvvm的区别

CDQ divide and conquer and whole dichotomy learning notes

什么是jsp?

OpenCV(三)——图像分割

了解Bom与DOM的基本属性

mysql视图及存储过程
随机推荐
数据采集之:巧用布隆过滤器提取数据摘要
Log4j.jar and slf4-log4 download link
OpenCV(三)——图像分割
Mpc5744p clock module
Data collection: skillfully using Bloom filter to extract data summary
UML图介绍
Segment tree beats~
2021 national vocational college skills competition (secondary vocational group) network security competition questions (9) ideas
Start from scratch blazor server (1) -- project construction
Rotate the whole model in ADAMS
mysql视图及存储过程
File class byte input and output stream
获取当前时间的前N天和前后天的数组列表循环遍历每一天
2021-05-30
数据库基础
Stylelint check error: unexpected missing generic font family font family no missing generic family keyword
js中的函数
Automatic classification of e-commerce UGC pictures using Baidu PaddlePaddle easydl
Duplicate names in molecular class methods
Natural sorting: comparable interface, customized sorting: the difference between comparator interface