spring之基于注解管理Bean

本文涉及的产品
服务治理 MSE Sentinel/OpenSergo,Agent数量 不受限
可观测可视化 Grafana 版,10个用户账号 1个月
简介: 【1月更文挑战第14天】从 Java 5 开始,Java 增加了对注解(Annotation)的支持,它是代码中的一种特殊标记,可以在编译、类加载和运行时被读取,执行相应的处理。开发人员可以通过注解在不改变原有代码和逻辑的情况下,在源代码中嵌入补充信息。Spring 从 2.5 版本开始提供了对注解技术的全面支持,我们可以使用注解来实现自动装配,简化 Spring 的 XML 配置。Spring 通过注解实现自动装配的步骤如下:1. 引入依赖2. 开启组件扫描3. 使用注解定义 Bean4. 依赖注入

文章目录

前言

一、搭建子模块spring6-ioc-annotation

二、开启组件扫描

三、使用注解定义 Bean

四、实验一:@Autowired注入

①场景一:属性注入

②场景二:set注入

③场景三:构造方法注入

④场景四:形参上注入

⑤场景五:只有一个构造函数,无注解

⑥场景六:@Autowired注解和@Qualifier注解联合

五、实验二:@Resource注入

①场景一:根据name注入

②场景二:name未知注入

③场景三 其他情况

六、Spring全注解开发

总结


前言

从 Java 5 开始,Java 增加了对注解(Annotation)的支持,它是代码中的一种特殊标记,可以在编译、类加载和运行时被读取,执行相应的处理。开发人员可以通过注解在不改变原有代码和逻辑的情况下,在源代码中嵌入补充信息。

Spring 从 2.5 版本开始提供了对注解技术的全面支持,我们可以使用注解来实现自动装配,简化 Spring 的 XML 配置。

Spring 通过注解实现自动装配的步骤如下:

  1. 引入依赖
  2. 开启组件扫描
  3. 使用注解定义 Bean
  4. 依赖注入

一、搭建子模块spring6-ioc-annotation

①搭建模块

搭建方式如:spring6-ioc-xml

②引入配置文件

引入spring-ioc-xml模块日志log4j2.xml

③添加依赖

<dependencies><!--spring context依赖--><!--当你引入Spring Context依赖之后,表示将Spring的基础依赖引入了--><dependency><groupId>org.springframework</groupId><artifactId>spring-context</artifactId><version>6.0.3</version></dependency><!--junit5测试--><dependency><groupId>org.junit.jupiter</groupId><artifactId>junit-jupiter-api</artifactId><version>5.3.1</version></dependency><!--log4j2的依赖--><dependency><groupId>org.apache.logging.log4j</groupId><artifactId>log4j-core</artifactId><version>2.19.0</version></dependency><dependency><groupId>org.apache.logging.log4j</groupId><artifactId>log4j-slf4j2-impl</artifactId><version>2.19.0</version></dependency></dependencies>

二、开启组件扫描

Spring 默认不使用注解装配 Bean,因此我们需要在 Spring 的 XML 配置中,通过 context:component-scan 元素开启 Spring Beans的自动扫描功能。开启此功能后,Spring 会自动从扫描指定的包(base-package 属性设置)及其子包下的所有类,如果类上使用了 @Component 注解,就将该类装配到容器中。

<?xmlversion="1.0" encoding="UTF-8"?><beansxmlns="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/beanshttp://www.springframework.org/schema/beans/spring-beans-3.0.xsdhttp://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context.xsd"><!--开启组件扫描功能--><context:component-scanbase-package="com.gedeshidai.spring6"></context:component-scan></beans>

注意:在使用 context:component-scan 元素开启自动扫描功能前,首先需要在 XML 配置的一级标签 中添加 context 相关的约束。

情况一:最基本的扫描方式

<context:component-scanbase-package="com.gedeshidai.spring6"></context:component-scan>

情况二:指定要排除的组件

<context:component-scanbase-package="com.gedeshidai.spring6"><!-- context:exclude-filter标签:指定排除规则 --><!-- type:设置排除或包含的依据type="annotation",根据注解排除,expression中设置要排除的注解的全类名type="assignable",根据类型排除,expression中设置要排除的类型的全类名--><context:exclude-filtertype="annotation"expression="org.springframework.stereotype.Controller"/><!--<context:exclude-filter type="assignable" expression="com.gedeshidai.spring6.controller.UserController"/>--></context:component-scan>

情况三:仅扫描指定组件

<context:component-scanbase-package="com.gedeshidai"use-default-filters="false"><!-- context:include-filter标签:指定在原有扫描规则的基础上追加的规则 --><!-- use-default-filters属性:取值false表示关闭默认扫描规则 --><!-- 此时必须设置use-default-filters="false",因为默认规则即扫描指定包下所有类 --><!-- type:设置排除或包含的依据type="annotation",根据注解排除,expression中设置要排除的注解的全类名type="assignable",根据类型排除,expression中设置要排除的类型的全类名--><context:include-filtertype="annotation"expression="org.springframework.stereotype.Controller"/><!--<context:include-filter type="assignable" expression="com.gedeshidai.spring6.controller.UserController"/>--></context:component-scan>

三、使用注解定义 Bean

Spring 提供了以下多个注解,这些注解可以直接标注在 Java 类上,将它们定义成 Spring Bean。

注解 说明
@Component 该注解用于描述 Spring 中的 Bean,它是一个泛化的概念,仅仅表示容器中的一个组件(Bean),并且可以作用在应用的任何层次,例如 Service 层、Dao 层等。 使用时只需将该注解标注在相应类上即可。
@Repository 该注解用于将数据访问层(Dao 层)的类标识为 Spring 中的 Bean,其功能与 @Component 相同。
@Service 该注解通常作用在业务层(Service 层),用于将业务层的类标识为 Spring 中的 Bean,其功能与 @Component 相同。
@Controller 该注解通常作用在控制层(如SpringMVC 的 Controller),用于将控制层的类标识为 Spring 中的 Bean,其功能与 @Component 相同。

四、实验一:@Autowired注入

单独使用@Autowired注解,默认根据类型装配。【默认是byType】

查看源码:

packageorg.springframework.beans.factory.annotation;
importjava.lang.annotation.Documented;
importjava.lang.annotation.ElementType;
importjava.lang.annotation.Retention;
importjava.lang.annotation.RetentionPolicy;
importjava.lang.annotation.Target;
@Target({ElementType.CONSTRUCTOR, ElementType.METHOD, ElementType.PARAMETER, ElementType.FIELD, ElementType.ANNOTATION_TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documentedpublic@interfaceAutowired {
booleanrequired() defaulttrue;
}

源码中有两处需要注意:

  • 第一处:该注解可以标注在哪里?
  • 构造方法上
  • 方法上
  • 形参上
  • 属性上
  • 注解上
  • 第二处:该注解有一个required属性,默认值是true,表示在注入的时候要求被注入的Bean必须是存在的,如果不存在则报错。如果required属性设置为false,表示注入的Bean存在或者不存在都没关系,存在的话就注入,不存在的话,也不报错。

①场景一:属性注入

创建UserDao接口

packagecom.gedeshidai.spring6.dao;
publicinterfaceUserDao {
publicvoidprint();
}

创建UserDaoImpl实现

packagecom.gedeshidai.spring6.dao.impl;
importcom.gedeshidai.spring6.dao.UserDao;
importorg.springframework.stereotype.Repository;
@RepositorypublicclassUserDaoImplimplementsUserDao {
@Overridepublicvoidprint() {
System.out.println("Dao层执行结束");
    }
}

创建UserService接口

packagecom.gedeshidai.spring6.service;
publicinterfaceUserService {
publicvoidout();
}

创建UserServiceImpl实现类

packagecom.gedeshidai.spring6.service.impl;
importcom.gedeshidai.spring6.dao.UserDao;
importcom.gedeshidai.spring6.service.UserService;
importorg.springframework.beans.factory.annotation.Autowired;
importorg.springframework.stereotype.Service;
@ServicepublicclassUserServiceImplimplementsUserService {
@AutowiredprivateUserDaouserDao;
@Overridepublicvoidout() {
userDao.print();
System.out.println("Service层执行结束");
    }
}

创建UserController类

packagecom.gedeshidai.spring6.controller;
importcom.gedeshidai.spring6.service.UserService;
importorg.springframework.beans.factory.annotation.Autowired;
importorg.springframework.stereotype.Controller;
@ControllerpublicclassUserController {
@AutowiredprivateUserServiceuserService;
publicvoidout() {
userService.out();
System.out.println("Controller层执行结束。");
    }
}

测试一

packagecom.gedeshidai.spring6.bean;
importcom.gedeshidai.spring6.controller.UserController;
importorg.junit.jupiter.api.Test;
importorg.slf4j.Logger;
importorg.slf4j.LoggerFactory;
importorg.springframework.context.ApplicationContext;
importorg.springframework.context.support.ClassPathXmlApplicationContext;
publicclassUserTest {
privateLoggerlogger=LoggerFactory.getLogger(UserTest.class);
@TestpublicvoidtestAnnotation(){
ApplicationContextcontext=newClassPathXmlApplicationContext("Beans.xml");
UserControlleruserController=context.getBean("userController", UserController.class);
userController.out();
logger.info("执行成功");
    }
}

②场景二:set注入

修改UserServiceImpl类

packagecom.gedeshidai.spring6.service.impl;
importcom.gedeshidai.spring6.dao.UserDao;
importcom.gedeshidai.spring6.service.UserService;
importorg.springframework.beans.factory.annotation.Autowired;
importorg.springframework.stereotype.Service;
@ServicepublicclassUserServiceImplimplementsUserService {
privateUserDaouserDao;
@AutowiredpublicvoidsetUserDao(UserDaouserDao) {
this.userDao=userDao;
    }
@Overridepublicvoidout() {
userDao.print();
System.out.println("Service层执行结束");
    }
}

修改UserController类

packagecom.gedeshidai.spring6.controller;
importcom.gedeshidai.spring6.service.UserService;
importorg.springframework.beans.factory.annotation.Autowired;
importorg.springframework.stereotype.Controller;
@ControllerpublicclassUserController {
privateUserServiceuserService;
@AutowiredpublicvoidsetUserService(UserServiceuserService) {
this.userService=userService;
    }
publicvoidout() {
userService.out();
System.out.println("Controller层执行结束。");
    }
}

测试:成功调用

③场景三:构造方法注入

修改UserServiceImpl类

packagecom.gedeshidai.spring6.service.impl;
importcom.gedeshidai.spring6.dao.UserDao;
importcom.gedeshidai.spring6.service.UserService;
importorg.springframework.beans.factory.annotation.Autowired;
importorg.springframework.stereotype.Service;
@ServicepublicclassUserServiceImplimplementsUserService {
privateUserDaouserDao;
@AutowiredpublicUserServiceImpl(UserDaouserDao) {
this.userDao=userDao;
    }
@Overridepublicvoidout() {
userDao.print();
System.out.println("Service层执行结束");
    }
}

修改UserController类

packagecom.gedeshidai.spring6.controller;
importcom.gedeshidai.spring6.service.UserService;
importorg.springframework.beans.factory.annotation.Autowired;
importorg.springframework.stereotype.Controller;
@ControllerpublicclassUserController {
privateUserServiceuserService;
@AutowiredpublicUserController(UserServiceuserService) {
this.userService=userService;
    }
publicvoidout() {
userService.out();
System.out.println("Controller层执行结束。");
    }
}

测试:成功调用

④场景四:形参上注入

修改UserServiceImpl类

packagecom.gedeshidai.spring6.service.impl;
importcom.gedeshidai.spring6.dao.UserDao;
importcom.gedeshidai.spring6.service.UserService;
importorg.springframework.beans.factory.annotation.Autowired;
importorg.springframework.stereotype.Service;
@ServicepublicclassUserServiceImplimplementsUserService {
privateUserDaouserDao;
publicUserServiceImpl(@AutowiredUserDaouserDao) {
this.userDao=userDao;
    }
@Overridepublicvoidout() {
userDao.print();
System.out.println("Service层执行结束");
    }
}

修改UserController类

packagecom.gedeshidai.spring6.controller;
importcom.gedeshidai.spring6.service.UserService;
importorg.springframework.beans.factory.annotation.Autowired;
importorg.springframework.stereotype.Controller;
@ControllerpublicclassUserController {
privateUserServiceuserService;
publicUserController(@AutowiredUserServiceuserService) {
this.userService=userService;
    }
publicvoidout() {
userService.out();
System.out.println("Controller层执行结束。");
    }
}

测试:成功调用

⑤场景五:只有一个构造函数,无注解

修改UserServiceImpl类

packagecom.gedeshidai.spring6.service.impl;
importcom.gedeshidai.spring6.dao.UserDao;
importcom.gedeshidai.spring6.service.UserService;
importorg.springframework.beans.factory.annotation.Autowired;
importorg.springframework.beans.factory.annotation.Qualifier;
importorg.springframework.stereotype.Service;
@ServicepublicclassUserServiceImplimplementsUserService {
@AutowiredprivateUserDaouserDao;
publicUserServiceImpl(UserDaouserDao) {
this.userDao=userDao;
    }
@Overridepublicvoidout() {
userDao.print();
System.out.println("Service层执行结束");
    }
}

测试通过

当有参数的构造方法只有一个时,@Autowired注解可以省略。

说明:有多个构造方法时呢?大家可以测试(再添加一个无参构造函数),测试报错

⑥场景六:@Autowired注解和@Qualifier注解联合

添加dao层实现

packagecom.gedeshidai.spring6.dao.impl;
importcom.gedeshidai.spring6.dao.UserDao;
importorg.springframework.stereotype.Repository;
@RepositorypublicclassUserDaoRedisImplimplementsUserDao {
@Overridepublicvoidprint() {
System.out.println("Redis Dao层执行结束");
    }
}

测试:测试异常

错误信息中说:不能装配,UserDao这个Bean的数量等于2

怎么解决这个问题呢?当然要byName,根据名称进行装配了。

修改UserServiceImpl类

packagecom.gedeshidai.spring6.service.impl;
importcom.gedeshidai.spring6.dao.UserDao;
importcom.gedeshidai.spring6.service.UserService;
importorg.springframework.beans.factory.annotation.Autowired;
importorg.springframework.stereotype.Service;
@ServicepublicclassUserServiceImplimplementsUserService {
@Autowired@Qualifier("userDaoImpl") // 指定bean的名字privateUserDaouserDao;
@Overridepublicvoidout() {
userDao.print();
System.out.println("Service层执行结束");
    }
}

总结

  • @Autowired注解可以出现在:属性上、构造方法上、构造方法的参数上、setter方法上。
  • 当带参数的构造方法只有一个,@Autowired注解可以省略。()
  • @Autowired注解默认根据类型注入。如果要根据名称注入的话,需要配合@Qualifier注解一起使用。

五、实验二:@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>

源码:

packagejakarta.annotation;
importjava.lang.annotation.ElementType;
importjava.lang.annotation.Repeatable;
importjava.lang.annotation.Retention;
importjava.lang.annotation.RetentionPolicy;
importjava.lang.annotation.Target;
@Target({ElementType.TYPE, ElementType.FIELD, ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
@Repeatable(Resources.class)
public@interfaceResource {
Stringname() default"";
Stringlookup() default"";
Class<?>type() defaultObject.class;
Resource.AuthenticationTypeauthenticationType() defaultResource.AuthenticationType.CONTAINER;
booleanshareable() defaulttrue;
StringmappedName() default"";
Stringdescription() default"";
publicstaticenumAuthenticationType {
CONTAINER,
APPLICATION;
privateAuthenticationType() {
        }
    }
}

①场景一:根据name注入

修改UserDaoImpl类

packagecom.gedeshidai.spring6.dao.impl;
importcom.gedeshidai.spring6.dao.UserDao;
importorg.springframework.stereotype.Repository;
@Repository("myUserDao")
publicclassUserDaoImplimplementsUserDao {
@Overridepublicvoidprint() {
System.out.println("Dao层执行结束");
    }
}

修改UserServiceImpl类

packagecom.gedeshidai.spring6.service.impl;
importcom.gedeshidai.spring6.dao.UserDao;
importcom.gedeshidai.spring6.service.UserService;
importjakarta.annotation.Resource;
importorg.springframework.beans.factory.annotation.Autowired;
importorg.springframework.beans.factory.annotation.Qualifier;
importorg.springframework.stereotype.Service;
@ServicepublicclassUserServiceImplimplementsUserService {
@Resource(name="myUserDao")
privateUserDaomyUserDao;
@Overridepublicvoidout() {
myUserDao.print();
System.out.println("Service层执行结束");
    }
}

测试通过

②场景二:name未知注入

修改UserDaoImpl类

packagecom.gedeshidai.spring6.dao.impl;
importcom.gedeshidai.spring6.dao.UserDao;
importorg.springframework.stereotype.Repository;
@Repository("myUserDao")
publicclassUserDaoImplimplementsUserDao {
@Overridepublicvoidprint() {
System.out.println("Dao层执行结束");
    }
}

修改UserServiceImpl类

packagecom.gedeshidai.spring6.service.impl;
importcom.gedeshidai.spring6.dao.UserDao;
importcom.gedeshidai.spring6.service.UserService;
importjakarta.annotation.Resource;
importorg.springframework.beans.factory.annotation.Autowired;
importorg.springframework.beans.factory.annotation.Qualifier;
importorg.springframework.stereotype.Service;
@ServicepublicclassUserServiceImplimplementsUserService {
@ResourceprivateUserDaomyUserDao;
@Overridepublicvoidout() {
myUserDao.print();
System.out.println("Service层执行结束");
    }
}

测试通过

当@Resource注解使用时没有指定name的时候,还是根据name进行查找,这个name是属性名。

③场景三 其他情况

修改UserServiceImpl类,userDao1属性名不存在

packagecom.gedeshidai.spring6.service.impl;
importcom.gedeshidai.spring6.dao.UserDao;
importcom.gedeshidai.spring6.service.UserService;
importjakarta.annotation.Resource;
importorg.springframework.beans.factory.annotation.Autowired;
importorg.springframework.beans.factory.annotation.Qualifier;
importorg.springframework.stereotype.Service;
@ServicepublicclassUserServiceImplimplementsUserService {
@ResourceprivateUserDaouserDao1;
@Overridepublicvoidout() {
userDao1.print();
System.out.println("Service层执行结束");
    }
}

测试异常

根据异常信息得知:显然当通过name找不到的时候,自然会启动byType进行注入,以上的错误是因为UserDao接口下有两个实现类导致的。所以根据类型注入就会报错。

@Resource的set注入可以自行测试

总结:

@Resource注解:默认byName注入,没有指定name时把属性名当做name,根据name找不到时,才会byType注入。byType注入时,某种类型的Bean只能有一个

六、Spring全注解开发

全注解开发就是不再使用spring配置文件了,写一个配置类来代替配置文件。

packagecom.gedeshidai.spring6.config;
importorg.springframework.context.annotation.ComponentScan;
importorg.springframework.context.annotation.Configuration;
@Configuration//@ComponentScan({"com.gedeshidai.spring6.controller", "com.gedeshidai.spring6.service","com.gedeshidai.spring6.dao"})@ComponentScan("com.gedeshidai.spring6")
publicclassSpring6Config {
}

测试类

@TestpublicvoidtestAllAnnotation(){
ApplicationContextcontext=newAnnotationConfigApplicationContext(Spring6Config.class);
UserControlleruserController=context.getBean("userController", UserController.class);
userController.out();
logger.info("执行成功");
}

总结

以上就是spring之基于注解管理Bean的相关知识点,希望对你有所帮助。

积跬步以至千里,积怠惰以至深渊。时代在这跟着你一起努力哦!

相关文章
|
16天前
|
缓存 Java Sentinel
Springboot 中使用 Redisson+AOP+自定义注解 实现访问限流与黑名单拦截
Springboot 中使用 Redisson+AOP+自定义注解 实现访问限流与黑名单拦截
|
5天前
|
运维 Java 程序员
Spring5深入浅出篇:基于注解实现的AOP
# Spring5 AOP 深入理解:注解实现 本文介绍了基于注解的AOP编程步骤,包括原始对象、额外功能、切点和组装切面。步骤1-3旨在构建切面,与传统AOP相似。示例代码展示了如何使用`@Around`定义切面和执行逻辑。配置中,通过`@Aspect`和`@Around`注解定义切点,并在Spring配置中启用AOP自动代理。 进一步讨论了切点复用,避免重复代码以提高代码维护性。通过`@Pointcut`定义通用切点表达式,然后在多个通知中引用。此外,解释了AOP底层实现的两种动态代理方式:JDK动态代理和Cglib字节码增强,默认使用JDK,可通过配置切换到Cglib
|
1天前
|
JSON 前端开发 Java
【JAVA进阶篇教学】第七篇:Spring中常用注解
【JAVA进阶篇教学】第七篇:Spring中常用注解
|
4天前
|
JavaScript Java 开发者
Spring Boot中的@Lazy注解:概念及实战应用
【4月更文挑战第7天】在Spring Framework中,@Lazy注解是一个非常有用的特性,它允许开发者控制Spring容器的bean初始化时机。本文将详细介绍@Lazy注解的概念,并通过一个实际的例子展示如何在Spring Boot应用中使用它。
17 2
|
5天前
|
前端开发 Java
SpringBoot之自定义注解参数校验
SpringBoot之自定义注解参数校验
15 2
|
11天前
|
消息中间件 安全 Java
在Spring Bean中,如何通过Java配置类定义Bean?
【4月更文挑战第30天】在Spring Bean中,如何通过Java配置类定义Bean?
20 1
|
11天前
|
Java Spring
springboot自带的@Scheduled注解开启定时任务
springboot自带的@Scheduled注解开启定时任务
|
14天前
|
前端开发 Java 数据格式
【Spring系列笔记】定义Bean的方式
在Spring Boot应用程序中,定义Bean是非常常见的操作,它是构建应用程序的基础。Spring Boot提供了多种方式来定义Bean,每种方式都有其适用的场景和优势。
31 2
|
14天前
|
XML JSON Java
【SpringBoot】springboot常用注解
【SpringBoot】springboot常用注解
|
15天前
|
Java 数据库 开发者
了解Spring Boot:重要注解详解
了解Spring Boot:重要注解详解