以上笔记来源:
尚硅谷Spring零基础入门到进阶,一套搞定spring6全套视频教程(源码级讲解)https://www.bilibili.com/video/BV1kR4y1b7Qc
目录
19.1 @Autowired注入
①场景一:属性注入
19.1.1创建UserDao接口
19.1.2 创建UserDaoImpl实现类
19.1.3 创建UserService接口
19.1.4 创建UserServiceImpl实现类
19.1.5 创建Controller类
19.1.6 创建测试类
19.1.7 运行结果
②场景二:set注入
19.2.1 修改UserServiceImpl实现类
19.2.2 修改Controller类
③场景三:构造方法注入
19.3.1 修改UserServiceImpl实现类
19.3.2 修改Controller类
④场景四:形参上注入
19.4.1 修改UserServiceImpl类
19.4.2 修改Controller类
⑤场景五:只有一个构造函数,无注解
19.5.1 修改UserServiceImpl类
⑥ 场景六:@Autowired注解和@Qualifier注解联合
19.6.1 添加dao层实现
19.6.2 修改UserServiceImpl类
19.7 总结
19.1 @Autowired注入
package org.springframework.beans.factory.annotation;
import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;import java.lang.annotation.Target;@Target({ElementType.CONSTRUCTOR, ElementType.METHOD, ElementType.PARAMETER, ElementType.FIELD,ElementType.ANNOTATION_TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documentedpublic
@interface Autowired
{
boolean required() default true;
}
源码中有两处需要注意:
-
第一处:该注解可以标注在哪里?
-
-
构造方法上
-
方法上
-
形参上
-
属性上
-
注解上
-
第二处:该注解有一个required属性,默认值是true,表示在注入的时候要求被注入的Bean必须是存在的,如果不存在则报错。如果required属性设置为false,表示注入的Bean存在或者不存在都没关系,存在的话就注入,不存在的话,也不报错。
①场景一:属性注入
19.1.1创建UserDao接口
package com.atguigu.spring6.autowired.dao;
import com.atguigu.spring6.bean.User;
public interface UserDao {
public void UserDaoadd();
}
19.1.2 创建UserDaoImpl实现类
package com.atguigu.spring6.autowired.dao;
import com.atguigu.spring6.autowired.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.stereotype.Service;
@Repository
public class UserDaoImpl implements UserDao {
@Override
public void UserDaoadd() {
System.out.println("addDao被调用了");
}
}
19.1.3 创建UserService接口
package com.atguigu.spring6.autowired.service;
public interface UserService {
public void UserServiceadd();
}
19.1.4 创建UserServiceImpl实现类
package com.atguigu.spring6.autowired.service;
import com.atguigu.spring6.autowired.dao.UserDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@Service
public class UserServiceImpl implements UserService {
@Autowired
UserDao userDao;
@Override
public void UserServiceadd() {
System.out.println("Service方法被调用了");
userDao.UserDaoadd();
}
}
19.1.5 创建Controller类
package com.atguigu.spring6.autowired.controller;
import com.atguigu.spring6.autowired.service.UserService;
import com.atguigu.spring6.autowired.service.UserServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Controller;
@Controller
public class controller {
@Autowired
UserService userService;
public void addcontroller(){
System.out.println("controller调用了");
userService.UserServiceadd();
}
}
19.1.6 创建测试类
package com.atguigu.spring6.autowired;
import com.atguigu.spring6.autowired.controller.controller;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Test {
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("bean.xml");
controller controller = context.getBean(controller.class);
controller.addcontroller();
}
}
19.1.7 运行结果
②场景二:set注入
19.2.1 修改UserServiceImpl实现类
package com.atguigu.spring6.autowired.service;
import com.atguigu.spring6.autowired.dao.UserDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@Service
public class UserServiceImpl implements UserService {
// @Autowired
private UserDao userDao;
@Autowired
public void setUserDao(UserDao userDao) {
this.userDao = userDao;
}
@Override
public void UserServiceadd() {
System.out.println("Service方法被调用了");
userDao.UserDaoadd();
}
}
19.2.2 修改Controller类
package com.atguigu.spring6.autowired.controller;
import com.atguigu.spring6.autowired.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
@Controller
public class controller {
// @Autowired
private UserService userService;
@Autowired
public void setUserService(UserService userService) {
this.userService = userService;
}
public void addcontroller(){
System.out.println("controller调用了");
userService.UserServiceadd();
}
}
③场景三:构造方法注入
19.3.1 修改UserServiceImpl实现类
package com.atguigu.spring6.autowired.service;
import com.atguigu.spring6.autowired.dao.UserDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@Service
public class UserServiceImpl implements UserService {
private UserDao userDao;
@Autowired
public UserServiceImpl(UserDao userDao) {
this.userDao = userDao;
}
@Override
public void UserServiceadd() {
System.out.println("Service方法被调用了");
userDao.UserDaoadd();
}
}
19.3.2 修改Controller类
package com.atguigu.spring6.autowired.controller;
import com.atguigu.spring6.autowired.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
@Controller
public class controller {
private UserDao userDao;
public void addcontroller(){
System.out.println("controller调用了");
userService.UserServiceadd();
}
@Autowired
public controller(UserService userService) {
this.userService = userService;
}
}
④场景四:形参上注入
19.4.1 修改UserServiceImpl类
package com.atguigu.spring6.autowired.service;
import com.atguigu.spring6.autowired.dao.UserDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@Service
public class UserServiceImpl implements UserService {
public UserServiceImpl(@Autowired UserDao userDao) {
this.userDao = userDao;
}
@Override
public void UserServiceadd() {
System.out.println("Service方法被调用了");
userDao.UserDaoadd();
}
}
19.4.2 修改Controller类
package com.atguigu.spring6.autowired.controller;
import com.atguigu.spring6.autowired.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
/**
* @package: com.atguigu.spring6.autowired
* @className: controller
* @Description:
* @author: haozihua
* @date: 2024/10/22 11:37
*/
@Controller
public class controller {
public void addcontroller(){
System.out.println("controller调用了");
userService.UserServiceadd();
}
public controller(@Autowired UserService userService) {
this.userService = userService;
}
}
⑤场景五:只有一个构造函数,无注解
19.5.1 修改UserServiceImpl类
package com.atguigu.spring6.autowired.service;
import com.atguigu.spring6.autowired.dao.UserDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@Service
public class UserServiceImpl implements UserService {
private UserDao userDao;
public UserServiceImpl(@Autowired UserDao userDao) {
this.userDao = userDao;
}
@Override
public void UserServiceadd() {
System.out.println("Service方法被调用了");
userDao.UserDaoadd();
}
}
注意:
当有参数的构造方法只有一个时,@Autowired注解可以省略。
说明:有多个构造方法时呢?大家可以测试(再添加一个无参构造函数),测试报错
⑥ 场景六:@Autowired注解和@Qualifier注解联合
19.6.1 添加dao层实现
package com.atguigu.spring6.autowired.service;s
import com.atguigu.spring6.autowired.dao.UserDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;
@Service
public class UserServiceImpl implements UserService {
@Autowired
@Qualifier("userDaoImpl")
private UserDao userDao;
@Override
public void UserServiceadd() {
System.out.println("Service方法被调用了");
userDao.UserDaoadd();
}
}
测试:测试异常
错误信息中说:不能装配,UserDao这个Bean的数量等于2
怎么解决这个问题呢?当然要byName,根据名称进行装配了。
19.6.2 修改UserServiceImpl类
package com.atguigu.spring6.service.impl;
import com.atguigu.spring6.dao.UserDao;
import com.atguigu.spring6.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@Service
public class UserServiceImpl implements UserService {
@Autowired
@Qualifier("userDaoImpl") // 指定bean的名字
private UserDao userDao;
@Override
public void out() {
userDao.print();
System.out.println("Service层执行结束");
}
}
19.7 总结
-
@Autowired注解可以出现在:属性上、构造方法上、构造方法的参数上、setter方法上。
-
当带参数的构造方法只有一个,@Autowired注解可以省略。()
-
@Autowired注解默认根据类型注入。如果要根据名称注入的话,需要配合@Qualifier注解一起使用。