Spring IOC基于XML和注解管理Bean(二)(下):https://developer.aliyun.com/article/1416392
3.4、实验一:@Autowired注入
单独使用@Autowired
注解,默认根据类型装配。【默认是byType】
查看源码:
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) @Documented public @interface Autowired { boolean required() default true; }
源码中有两处需要注意:
- 第一处:该注解可以标注在哪里?
- 构造方法上
- 方法上
- 形参上
- 属性上
- 注解上
- 第二处:该注解有一个
required
属性,默认值是true,表示在注入的时候要求被注入的Bean必须是存在的,如果不存在则报错。如果required属性设置为false,表示注入的Bean存在或者不存在都没关系,存在的话就注入,不存在的话,也不报错。
①场景一:属性注入
创建UserDao接口
package org.example.dao; public interface UserDao { public void print(); }
创建UserDaoImpl实现
package org.example.dao.impl; import org.example.dao.UserDao; import org.springframework.stereotype.Repository; @Repository public class UserDaoImpl implements UserDao { @Override public void print() { System.out.println("Dao层执行结束"); } }
创建UserService接口
package org.example.spring6.service; public interface UserService { public void out(); }
创建UserServiceImpl实现类
package org.example.spring6.service.impl; import org.example.spring6.dao.UserDao; import org.example.spring6.service.UserService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; @Service public class UserServiceImpl implements UserService { @Autowired private UserDao userDao; @Override public void out() { userDao.print(); System.out.println("Service层执行结束"); } }
创建UserController类
package org.exampleu.spring6.controller; import org.example.spring6.service.UserService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; @Controller public class UserController { @Autowired private UserService userService; public void out() { userService.out(); System.out.println("Controller层执行结束。"); } }
测试一
package org.example.spring6.bean; import org.example.spring6.controller.UserController; import org.junit.jupiter.api.Test; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class UserTest { private Logger logger = LoggerFactory.getLogger(UserTest.class); @Test public void testAnnotation(){ ApplicationContext context = new ClassPathXmlApplicationContext("Beans.xml"); UserController userController = context.getBean("userController", UserController.class); userController.out(); logger.info("执行成功"); } }
以上构造方法和setter方法都没有提供,经过测试,仍然可以注入成功。
②场景二:set注入
修改UserServiceImpl类
package org.example.service.impl; import org.example.dao.UserDao; import org.example.service.UserService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; @Service public class UserServiceImpl implements UserService { private UserDao userDao; @Autowired public void setUserDao(UserDao userDao) { this.userDao = userDao; } @Override public void out() { userDao.print(); System.out.println("Service层执行结束"); } }
修改UserController类
package org.example.controller; import org.example.service.UserService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; @Controller public class UserController { private UserService userService; @Autowired public void setUserService(UserService userService) { this.userService = userService; } public void out() { userService.out(); System.out.println("Controller层执行结束。"); } }
测试:成功调用
③场景三:构造方法注入
修改UserServiceImpl类
package org.example.service.impl; import org.example.dao.UserDao; import org.example.service.UserService; 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 out() { userDao.print(); System.out.println("Service层执行结束"); } }
修改UserController类
package org.example.controller; import org.example.service.UserService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; @Controller public class UserController { private UserService userService; @Autowired public UserController(UserService userService) { this.userService = userService; } public void out() { userService.out(); System.out.println("Controller层执行结束。"); } }
测试:成功调用
注:在 Spring4.x 中增加了新的特性:如果类只提供了一个带参数的构造方法,则不需要对对其内部的属性写 @Autowired 注解,Spring 会自动为你注入属性。(请看场景五)
④场景四:形参上注入
修改UserServiceImpl类
package org.example.service.impl; import org.example.spring6.dao.UserDao; import org.example.service.UserService; 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 out() { userDao.print(); System.out.println("Service层执行结束"); } }
修改UserController类
package org.example.controller; import org.example.service.UserService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; @Controller public class UserController { private UserService userService; public UserController(@Autowired UserService userService) { this.userService = userService; } public void out() { userService.out(); System.out.println("Controller层执行结束。"); } }
测试:成功调用
⑤场景五:只有一个构造函数,无注解
修改UserServiceImpl类
package org.example.service.impl; import org.example.dao.UserDao; import org.example.service.UserService; 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 private UserDao userDao; public UserServiceImpl(UserDao userDao) { this.userDao = userDao; } @Override public void out() { userDao.print(); System.out.println("Service层执行结束"); } }
测试通过
当有参数的构造方法只有一个时,@Autowired注解可以省略。
说明:有多个构造方法时呢?大家可以测试(再添加一个无参构造函数),测试报错
⑥场景六:@Autowired注解和@Qualifier注解联合
添加dao层实现
package org.example.dao.impl; import org.example.dao.UserDao; import org.springframework.stereotype.Repository; @Repository public class UserDaoRedisImpl implements UserDao { @Override public void print() { System.out.println("Redis Dao层执行结束"); } }
测试:测试异常
错误信息中说:不能装配,UserDao这个Bean的数量等于2
怎么解决这个问题呢?当然要byName,根据名称进行装配了。
修改UserServiceImpl类
package org.example.service.impl; import org.example.dao.UserDao; import org.example.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层执行结束"); } }
总结
@Autowired
注解可以出现在:属性上、构造方法上、构造方法的参数上、setter方法上。- 当带参数的构造方法只有一个,@Autowired注解可以省略。()
- @Autowired注解默认根据类型注入。如果要根据名称注入的话,需要配合
@Qualifier
注解一起使用。
3.5、实验二:@Resource注入
@Resource
注解也可以完成属性注入。那它和@Autowired注解有什么区别?
@Resource
注解是JDK扩展包中的,也就是说属于JDK的一部分。所以该注解是标准注解,更加具有通用性。(JSR-250标准中制定的注解类型。JSR是Java规范提案。)- @Autowired注解是Spring框架自己的。
- @Resource注解默认根据名称装配byName,未指定name时,使用属性名作为name。通过name找不到的话会自动启动通过类型byType装配。
- @Autowired注解默认根据类型装配byType,如果想根据名称装配,需要配合@Qualifier注解一起用。
- @Resource注解用在属性上、setter方法上。
- @Autowired注解用在属性上、setter方法上、构造方法上、构造方法参数上。
@Resource注解属于JDK扩展包,所以不在JDK当中,需要额外引入以下依赖:【如果是JDK8的话不需要额外引入依赖。高于JDK11或低于JDK8需要引入以下依赖。】
<dependency> <groupId>jakarta.annotation</groupId> <artifactId>jakarta.annotation-api</artifactId> <version>2.1.1</version> </dependency>
源码:
package jakarta.annotation; import java.lang.annotation.ElementType; import java.lang.annotation.Repeatable; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; import java.lang.annotation.Target; @Target({ElementType.TYPE, ElementType.FIELD, ElementType.METHOD}) @Retention(RetentionPolicy.RUNTIME) @Repeatable(Resources.class) public @interface Resource { String name() default ""; String lookup() default ""; Class<?> type() default Object.class; Resource.AuthenticationType authenticationType() default Resource.AuthenticationType.CONTAINER; boolean shareable() default true; String mappedName() default ""; String description() default ""; public static enum AuthenticationType { CONTAINER, APPLICATION; private AuthenticationType() { } } }
①场景一:根据name注入
修改UserDaoImpl类
package org.example.dao.impl; import org.example.dao.UserDao; import org.springframework.stereotype.Repository; @Repository("myUserDao") public class UserDaoImpl implements UserDao { @Override public void print() { System.out.println("Dao层执行结束"); } }
修改UserServiceImpl类
package org.example.service.impl; import org.example.dao.UserDao; import org.example.service.UserService; import jakarta.annotation.Resource; 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 { @Resource(name = "myUserDao") private UserDao myUserDao; @Override public void out() { myUserDao.print(); System.out.println("Service层执行结束"); } }
测试通过
②场景二:name未知注入
修改UserDaoImpl类
package org.example.dao.impl; import org.example.dao.UserDao; import org.springframework.stereotype.Repository; @Repository("myUserDao") public class UserDaoImpl implements UserDao { @Override public void print() { System.out.println("Dao层执行结束"); } }
修改UserServiceImpl类
package org.example.service.impl; import org.example.dao.UserDao; import org.example.service.UserService; import jakarta.annotation.Resource; 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 { @Resource private UserDao myUserDao; @Override public void out() { myUserDao.print(); System.out.println("Service层执行结束"); } }
测试通过
当@Resource
注解使用时没有指定name的时候,还是根据name进行查找,这个name是 属性名 。
③场景三 其他情况
修改UserServiceImpl类,userDao1属性名不存在
package org.example.service.impl; import org.example.dao.UserDao; import org.example.service.UserService; import jakarta.annotation.Resource; 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 { @Resource private UserDao userDao1; @Override public void out() { userDao1.print(); System.out.println("Service层执行结束"); } }
测试异常
根据异常信息得知:显然当通过name找不到的时候,自然会启动byType进行注入,以上的错误是因为UserDao接口下有两个实现类导致的。所以根据类型注入就会报错。
@Resource的set注入可以自行测试
总结:
@Resource注解:默认byName注入,没有指定name时把属性名当做name,根据name找不到时,才会byType注入。byType注入时,某种类型的Bean只能有一个
3.6、Spring全注解开发
全注解开发就是不再使用spring配置文件了,写一个配置类来代替配置文件。
package org.example.config; import org.springframework.context.annotation.ComponentScan; import org.springframework.context.annotation.Configuration; @Configuration //@ComponentScan({"org.example.controller", "org.example.service","org.example.dao"}) @ComponentScan("org.example") public class Spring6Config { }
测试类
@Test public void testAllAnnotation(){ ApplicationContext context = new AnnotationConfigApplicationContext(Spring6Config.class); UserController userController = context.getBean("userController", UserController.class); userController.out(); logger.info("执行成功"); }