Spring基于注解配置的容器

本文涉及的产品
容器镜像服务 ACR,镜像仓库100个 不限时长
容器服务 Serverless 版 ACK Serverless,952元额度 多规格
容器服务 Serverless 版 ACK Serverless,317元额度 多规格
简介: Spring 容器的元数据可以基于注解配置,它比 XML 配置更简洁,而且提供了更多的上下文配置。两种配置方式各有优缺点,XML 配置不会侵入源代码,配置修改后不需要重新编译源文件。你可以在项目中任意选择哪种配置方式,或者两者混合使用。


一、Bean 管理



Spring 通过扫描指定包路径下所有的类(包括子包下的类),来寻找哪些类是要容器管理的。

默认情况下,根据类是否存在 @Component 注解(或其组合注解)来判断是否由容器管理。


1. 扫描类路径配置


基于 XML 配置

在 XML 配置文件中使用 context:component-scan 来配置扫描的包路径。

<beans>
    <context:component-scan base-package="cn.codeartist.spring.bean.annotation"/>
</beans>

使用 ClassPathXmlApplicationContext 来实例化容器。

public static void main(String[] args) {
    ApplicationContext applicationContext =
        new ClassPathXmlApplicationContext("bean-annotation.xml");
    BeanExample beanExample = (BeanExample) applicationContext.getBean("beanExample");
}


基于注解配置

定义类并使用 @Configuration@ComponentScan 来配置扫描的包路径。

@Configuration
@ComponentScan(basePackages = "cn.codeartist.spring.bean.annotation")
public class AppConfig {
}

@ComponentScan 可以不指定 basePackages,默认扫描类(AppConfig)所在的包路径。

使用 AnnotationConfigApplicationContext 来实例化容器。

public static void main(String[] args) {
    ApplicationContext applicationContext =
        new AnnotationConfigApplicationContext(AppConfig.class);
    BeanExample beanExample = (BeanExample) applicationContext.getBean("beanExample");
}


2. 使用注解管理 Bean


在扫描的包下面,在类上面使用 @Component 注解来注册 Bean。

@Component
public class BeanExample {
    private String name;
    private Integer year;
    // Getter and setter
}

为了更明确 Bean 的业务用途,体现项目的分层结构,Spring 还提供了 @Service@Repository@Controller 等注解。

这些注解默认使用类名小驼峰格式作为 Bean 名称,也可以通过注解的 value 属性来指定。

BeanExample -> beanExample

Bean 作用域

使用 @Scope 注解来指定 Bean 的作用域。

@Component
@Scope("singleton")
public class BeanExample {
    private String name;
    private Integer year;
}

在基于 XML 配置容器中,使用 context:annotation-config 来配置注解注入。

<beans>
    <context:annotation-config/>
</beans>


如果 XML 配置文件中存在 context:component-scan,则不需要配置 context:annotation-config

Spring 通常使用 @Autowired@Resource 注解注入 Bean 依赖,@Autowired 注解默认通过类型注入。

@Resource 注解默认通过名称注入,只有匹配不到对应名称的 Bean,才会按类型注入。


1. 依赖注入


1.1 字段注入


直接在字段上面使用注解注入依赖。

@Component
public class BeanExample {
    private String name;
    private Integer year;
    @Autowired
    private BeanProvider beanProvider;
}


1.2 构造器注入


在构造器方法上使用注解注入依赖。

@Component
public class BeanExample {
    private String name;
    private Integer year;
    private BeanProvider beanProvider;
    @Autowired
    public BeanExample(BeanProvider beanProvider) {
        this.beanProvider = beanProvider;
    }
}


1.3 Setter 方法注入


在 Setter 方法上使用注解注入依赖。

@Component
public class BeanExample {
    private String name;
    private Integer year;
    private BeanProvider beanProvider;
    @Autowired
    public void setBeanProvider(BeanProvider beanProvider) {
        this.beanProvider = beanProvider;
    }
}


2. 依赖关系


使用 @DependsOn 注解指定依赖关系。

@Component
@DependsOn("beanProvider")
public class BeanExample {
    private String name;
    private Integer year;
    private BeanProvider beanProvider;
    // Getter and setter
}


3. 懒加载


使用 @Lazy 注解配置懒加载。

@Lazy
@Component
public class BeanProvider {
    // Fields
    // Getter and setter
}

懒加载 Bean 在注入的地方也要加上 @Lazy 注解,或者使用 ApplicationContext.getBean() 方法获取 Bean,才能使懒加载生效。

@Component
public class BeanExample {
    private String name;
    private Integer year;
    @Lazy
    @Autowired
    private BeanProvider beanProvider;
}


二、附录



1. 配置属性


属性 描述
context:component-scan 在基于 XML 配置容器中,指定扫描包路径
context:annotation-config 在基于 XML 配置容器中,启用注解注入依赖


2. 常用注解


注解 描述
@Configuration 指定 Bean 的配置类
@ComponentScan (默认为类所在的包)指定包路径,该包下的类由容器管理
@Component 指定该类由 Spring 容器管理
@Service @Component 一致,通常在业务层使用
@Repository @Component 一致,通常在数据层使用
@Controller @Component 一致,通常在控制层使用
@Autowired 配置自动注入,优先通过类型注入
@Resource 配置自动注入,优先通过名称注入
@Scope 指定 Bean 的作用域
@DependsOn 指定 Bean 的依赖关系
@Lazy 配置懒加载


3. 示例代码


Gitee 仓库:

https://gitee.com/code_artist/spring

项目模块:

spring-ioc

示例路径:

cn.codeartist.spring.bean.annotation


目录
相关文章
|
14天前
|
XML Java 数据格式
Spring IOC—基于XML配置Bean的更多内容和细节(通俗易懂)
Spring 第二节内容补充 关于Bean配置的更多内容和细节 万字详解!
88 18
Spring IOC—基于XML配置Bean的更多内容和细节(通俗易懂)
|
2天前
|
XML Java 开发者
经典面试---spring IOC容器的核心实现原理
作为一名拥有十年研发经验的工程师,对Spring框架尤其是其IOC(Inversion of Control,控制反转)容器的核心实现原理有着深入的理解。
12 3
|
2天前
|
Java Spring 容器
Spring使用异步注解@Async正确姿势
Spring使用异步注解@Async正确姿势,异步任务,spring boot
|
1天前
|
XML Java 数据格式
spring复习03,注解配置管理bean
Spring框架中使用注解配置管理bean的方法,包括常用注解的标识组件、扫描组件、基于注解的自动装配以及使用注解后的注意事项,并提供了一个基于注解自动装配的完整示例。
spring复习03,注解配置管理bean
|
2天前
|
XML 前端开发 Java
控制spring框架注解介绍
控制spring框架注解介绍
|
2天前
|
前端开发 Java Spring
关于spring mvc 的 addPathPatterns 拦截配置常见问题
关于spring mvc 的 addPathPatterns 拦截配置常见问题
|
15天前
|
Java 数据库连接 数据格式
【Java笔记+踩坑】Spring基础2——IOC,DI注解开发、整合Mybatis,Junit
IOC/DI配置管理DruidDataSource和properties、核心容器的创建、获取bean的方式、spring注解开发、注解开发管理第三方bean、Spring整合Mybatis和Junit
【Java笔记+踩坑】Spring基础2——IOC,DI注解开发、整合Mybatis,Junit
|
15天前
|
Java 数据库连接 Maven
Spring基础1——Spring(配置开发版),IOC和DI
spring介绍、入门案例、控制反转IOC、IOC容器、Bean、依赖注入DI
Spring基础1——Spring(配置开发版),IOC和DI
|
26天前
|
IDE Java 开发工具
还在为繁琐的配置头疼吗?一文教你如何用 Spring Boot 快速启动,让开发效率飙升,从此告别加班——打造你的首个轻量级应用!
【9月更文挑战第2天】Spring Boot 是一款基于 Spring 框架的简化开发工具包,采用“约定优于配置”的原则,帮助开发者快速创建独立的生产级应用程序。本文将指导您完成首个 Spring Boot 项目的搭建过程,包括环境配置、项目初始化、添加依赖、编写控制器及运行应用。首先需确保 JDK 版本不低于 8,并安装支持 Spring Boot 的现代 IDE,如 IntelliJ IDEA 或 Eclipse。
81 5
|
28天前
|
Java Spring 开发者
解锁 Spring Boot 自动化配置的黑科技:带你走进一键配置的高效开发新时代,再也不怕繁琐设置!
【8月更文挑战第31天】Spring Boot 的自动化配置机制极大简化了开发流程,使开发者能专注业务逻辑。通过 `@SpringBootApplication` 注解组合,特别是 `@EnableAutoConfiguration`,Spring Boot 可自动激活所需配置。例如,添加 JPA 依赖后,只需在 `application.properties` 配置数据库信息,即可自动完成 JPA 和数据源设置。这一机制基于多种条件注解(如 `@ConditionalOnClass`)实现智能配置。深入理解该机制有助于提升开发效率并更好地解决问题。
45 0