以上笔记来源:
尚硅谷Spring零基础入门到进阶,一套搞定spring6全套视频教程(源码级讲解)https://www.bilibili.com/video/BV1kR4y1b7Qc
目录
①引入
②场景模拟
2.1 创建UserController类文件
2.2 创建UserService接口文件
2.3 创建UserServiceImpl实现类文件
2.4 创建UserDao接口文件
2.5 创建UserDaoImpl实现类文件
2.6 创建XML配置文件
2.6.1 根据类型进行自动装配
2.6.2 根据名称进行自动装配
2.6 创建测试类文件
2.7 运行截图
③总结
①引入
自动装配:根据指定的策略,在IOC容器中匹配某一个bean,自动为指定的bean中所依赖的类类型或接口类型属性赋值
②场景模拟
2.1 创建UserController类文件
package com.atguigu.spring6.iocxml.auto.controller;import com.atguigu.spring6.iocxml.auto.dao.UserDao;
import com.atguigu.spring6.iocxml.auto.dao.UserDaoImpl;
import com.atguigu.spring6.iocxml.auto.service.UserService;
import com.atguigu.spring6.iocxml.auto.service.UserServiceImpl;/*** @package: com.atguigu.spring6.iocxml.auto.controller* @className: UserController* @Description:* @author: haozihua* @date: 2024/10/19 16:34*/
public class UserController {private UserService userService;public void setUserService(UserService userService) {this.userService = userService;}public void addUser(){System.out.println("Controller方法执行了...");//调用Service的方法userService.addUserService();
// UserService userService = new UserServiceImpl();
// userService.addUserService();}
}
2.2 创建UserService接口文件
package com.atguigu.spring6.iocxml.auto.service;public interface UserService {public void addUserService();
}
2.3 创建UserServiceImpl实现类文件
package com.atguigu.spring6.iocxml.auto.service;import com.atguigu.spring6.iocxml.auto.dao.UserDao;
import com.atguigu.spring6.iocxml.auto.dao.UserDaoImpl;
import com.atguigu.spring6.iocxml.factorybean.User;/*** @package: com.atguigu.spring6.iocxml.auto.service* @className: UserServiceImpl* @Description:* @author: haozihua* @date: 2024/10/19 16:35*/
public class UserServiceImpl implements UserService{private UserDao userDao;public void setUserDao(UserDao userDao) {this.userDao = userDao;}@Overridepublic void addUserService() {System.out.println("userService方法执行了...");//调用dao方法userDao.addUserDao();
// UserDao userDao = new UserDaoImpl();
// userDao.addUserDao();}
}
2.4 创建UserDao接口文件
package com.atguigu.spring6.iocxml.auto.dao;public interface UserDao {public void addUserDao();
}
2.5 创建UserDaoImpl实现类文件
package com.atguigu.spring6.iocxml.auto.dao;/*** @package: com.atguigu.spring6.iocxml.auto.dao* @className: UserDaoImpl* @Description:* @author: haozihua* @date: 2024/10/19 16:38*/
public class UserDaoImpl implements UserDao {@Overridepublic void addUserDao() {System.out.println("addUserDao方法执行了...");}
}
2.6 创建XML配置文件
2.6.1 根据类型进行自动装配
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"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.xsd"><!--根据类型自动装配--><bean id="userController" class="com.atguigu.spring6.iocxml.auto.controller.UserController"autowire="byType"></bean><bean id="userService" class="com.atguigu.spring6.iocxml.auto.service.UserServiceImpl"autowire="byType"></bean><bean id="userDao" class="com.atguigu.spring6.iocxml.auto.dao.UserDaoImpl"></bean></beans>
2.6.2 根据名称进行自动装配
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"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.xsd"><!--根据名称自动装配--><bean id="userController" class="com.atguigu.spring6.iocxml.auto.controller.UserController"autowire="byName"></bean><bean id="userService" class="com.atguigu.spring6.iocxml.auto.service.UserServiceImpl"autowire="byName"></bean><bean id="userDao" class="com.atguigu.spring6.iocxml.auto.dao.UserDaoImpl"></bean></beans>
2.6 创建测试类文件
package com.atguigu.spring6.iocxml.auto;import com.atguigu.spring6.iocxml.auto.controller.UserController;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;/*** @package: com.atguigu.spring6.iocxml.auto* @className: Test* @Description:* @author: haozihua* @date: 2024/10/19 17:01*/
public class TestUser {public static void main(String[] args) {ApplicationContext context = new ClassPathXmlApplicationContext("bean-auto.xml");UserController userController = context.getBean("userController", UserController.class);userController.addUser();}
}
2.7 运行截图
③总结
实现XML的自动装配一共有两种方式可以实现,通过类型和名称均可以进行装配,需要注意的是,当按照类型进行自动装配时,需要注意接口的实现类是否有多个,当接口的实现类有多个时,就会产生如下异常,需要注意。
org.springframework.beans.factory.NoUniqueBeanDefinitionException: No qualifying bean of type 'com.atguigu.spring6.bean.HelloWorld' available: expected single matching bean but found 2: helloworldOne,helloworldTwo