8、Spring之基于注解的自动装配
8.1、场景模拟
8.1.1、UserDao接口及实现类
package org.rain.spring.dao;
/**
* @author liaojy
* @date 2023/8/5 - 18:32
*/
public interface UserDao {
void saveUser();
}
package org.rain.spring.dao.impl;
import org.rain.spring.dao.UserDao;
import org.springframework.stereotype.Repository;
/**
* @author liaojy
* @date 2023/8/5 - 18:33
*/
@Repository
public class UserDaoImpl implements UserDao {
public void saveUser() {
System.out.println("保存成功");
}
}
8.1.2、UserService接口及实现类
package org.rain.spring.service;
/**
* @author liaojy
* @date 2023/8/5 - 18:35
*/
public interface UserService {
void saveUser();
}
package org.rain.spring.service.impl;
import org.rain.spring.dao.UserDao;
import org.rain.spring.service.UserService;
import org.springframework.stereotype.Service;
/**
* @author liaojy
* @date 2023/8/5 - 18:36
*/
@Service
public class UserServiceImpl implements UserService {
private UserDao userDao;
public void saveUser() {
userDao.saveUser();
}
}
8.1.3、UserController
package org.rain.spring.controller;
import org.rain.spring.service.UserService;
import org.springframework.stereotype.Controller;
/**
* @author liaojy
* @date 2023/8/5 - 18:38
*/
@Controller("myUserController")
public class UserController {
private UserService userService;
public void saveUser(){
userService.saveUser();
}
}
8.1.4、bean配置
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd">
<!--
对指定的package进行扫描,将使用组件注解的类的对象,交给spring的ioc容器来管理
-->
<context:component-scan base-package="org.rain.spring"></context:component-scan>
</beans>
8.1.5、测试
由控制台日志可知,组件注解+组件扫描,存在尚未实现依赖注入的问题
@Test
public void testAutowireByAnnotation(){
ApplicationContext ioc = new ClassPathXmlApplicationContext("spring-ioc-annotation.xml");
UserController userController = ioc.getBean(UserController.class);
userController.saveUser();
}
8.2、@Autowired注解
@Autowired:实现自动装配功能的注解,其能够标识的位置有三处
8.2.1、方式一:标识在成员变量上(常用)
@Autowired
private UserDao userDao;
@Autowired
private UserService userService;
8.2.2、方式二:标识在setter方法上
private UserDao userDao;
@Autowired
public void setUserDao(UserDao userDao) {
this.userDao = userDao;
}
private UserService userService;
@Autowired
public void setUserService(UserService userService) {
this.userService = userService;
}
8.2.3、方式三:标识在有参构造方法上
private UserDao userDao;
@Autowired
public UserServiceImpl(UserDao userDao) {
this.userDao = userDao;
}
private UserService userService;
@Autowired
public UserController(UserService userService) {
this.userService = userService;
}
8.3、@Autowired的工作流程
-
默认通过byType的方式,在IOC容器中匹配类型相同的bean,为属性赋值
-
如果有多个类型匹配的bean,则自动转换为通过byName的方式,在IOC容器中匹配id和属性名相同的bean,为属性赋值
-
如果有多个类型匹配的bean,且在IOC容器中没有找到id和属性名相同的bean;
则可以使用@Qualifier注解指定某个bean的id,将这个bean赋值给属性
如果有多个类型匹配的bean,且在IOC容器中没有找到id和属性名相同的bean,也没有使用@Qualifier注解指定将某个bean赋值给属性;则会抛出异常NoUniqueBeanDefinitionException
@Autowired
@Qualifier("myUserService")
private UserService userService;
8.4、@Autowired的注意事项
在@Autowired注解中有一个属性required,默认值为true,意思是必要完成自动装配
如果在IOC容器中没有匹配的bean,就会抛出异常NoSuchBeanDefinitionException
为此可以将required属性的值设置为false,这样能装配就装配,不能装配就使用默认值
一般可以通过排查组件注解、组件扫描和@Qualifier注解,定位出为什么在IOC容器中没有匹配的bean
@Autowired(required = false)
private UserService userService;
本文来自博客园,作者:Javaer1995,转载请注明原文链接:https://www.cnblogs.com/Javaer1995/p/17609457.html