【Spring 从0开始】IOC容器的Bean管理 - 基于注解 - 创建对象&组件扫描

简介: 【Spring 从0开始】IOC容器的Bean管理 - 基于注解 - 创建对象&组件扫描

什么是注解?


注解是代码里的特殊标记,格式:@注解名称(属性名称=属性值, 属性名称2=属性值...)


可以作用在:类、方法、属性上面。


使用注解的目的:简化 xml 配置,让使用配置更简洁优雅。


一、spring 针对 bean 管理中创建对象提供注解


  • @Component
  • @Service
  • @Controller
  • @Repository


这 4 个注解功能是一样的,都可以用来创建 bean 实例。


但是通常实际应用中,为了让开发人员更加清晰当前组件所扮演的角色,一般会让它们各自应用在不同的层。比如 @Service 用在逻辑层、@Service 用在web层等。


示例


1. 引入依赖


引入 AOP 依赖,可以在这里搜索下载需要的 jar 包。


1268169-20210731225534902-2003343397.png


2. 开启组件扫描


其实就是告诉 spring 你要在什么地方使用注解。通过在 xml 里配置,spring就会到对应位置扫描下面的类:


<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="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/beans http://www.springframework.org/schema/beans/spring-beans.xsd
                           http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
    <!--开启组件扫描-->
    <context:component-scan base-package="com.pingguo.spring5.dao"></context:component-scan>
</beans>


现在,我这里有多个包:


1268169-20210731230647652-1349569472.png


  • 如果要扫描多个包,可以用逗号,隔开:


<context:component-scan base-package="com.pingguo.spring5.dao, com.pingguo.spring5.service"></context:component-scan>


  • 如果所有下层的包都要扫描,那也可以之间写上层的目录:


<context:component-scan base-package="com.pingguo.spring5"></context:component-scan>


3. 创建类,并添加注解来创建对象


package com.pingguo.spring5.service;
import org.springframework.stereotype.Component;
@Component(value = "userService")
public class UserService {
    public void add() {
        System.out.println("service add() ... ...");
    }
}


现在终于不用去 xml 写 bean 标签了。


  • @Component(value = "userService"),这里 value 的值,等同于 <bean id="userService" ...里的 id 。
  • @Component(value = "userService"),这里括号里的 value 可以不写,默认就是类名称的首字母小写。比如 类 UserService 就是 userService 。


4. 测试一下


package com.pingguo.spring5.testdemo;
import com.pingguo.spring5.service.UserService;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class TestService {
    @Test
    public void testService() {
        ApplicationContext context
                = new ClassPathXmlApplicationContext("bean1.xml");
        UserService userService = context.getBean("userService", UserService.class);
        System.out.println(userService);
        userService.add();
    }
}


运行一下:


com.pingguo.spring5.service.UserService@60611244
service add() ... ...
Process finished with exit code 0


成功。


如果把注解换成其他几个,重新运行测试方法,结果也是一样的。


二、组件扫描的其他过滤条件


在上述的开启扫描配置:


<!--开启组件扫描-->
<context:component-scan base-package="com.pingguo.spring5"></context:component-scan>


意思就是说扫描包路径com.pingguo.spring5下的所有类。


其实这里有个属性 use-default-filters,默认情况下就是等于true,也就是使用默认过滤规则,会去扫描路径下的所有。


那如果use-default-filters="false",就是不使用默认过滤条件,我们可以自己配置过滤。


1. include-filter


在指定的包路径下,只扫描包含了某种注解的类。比如:


<context:component-scan base-package="com.pingguo.spring5" use-default-filters="false">
    <context:include-filter type="annotation" expression="org.springframework.stereotype.Service"/>
</context:component-scan>


这就是说,在路径com.pingguo.spring5下,只扫描Service注解的类。


2. exclude-filter


与上面相反,这里是除了xx之外,都去扫描。


<context:component-scan base-package="com.pingguo.spring5" use-default-filters="false">
    <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Service"/>
</context:component-scan>


做了改动之后,意思也变了。现在是说在路径com.pingguo.spring5下,除了Service注解的类,其他都扫描。

相关文章
|
6月前
|
缓存 监控 Java
SpringBoot @Scheduled 注解详解
使用`@Scheduled`注解实现方法周期性执行,支持固定间隔、延迟或Cron表达式触发,基于Spring Task,适用于日志清理、数据同步等定时任务场景。需启用`@EnableScheduling`,注意线程阻塞与分布式重复问题,推荐结合`@Async`异步处理,提升任务调度效率。
964 128
|
5月前
|
XML Java 测试技术
《深入理解Spring》:IoC容器核心原理与实战
Spring IoC通过控制反转与依赖注入实现对象间的解耦,由容器统一管理Bean的生命周期与依赖关系。支持XML、注解和Java配置三种方式,结合作用域、条件化配置与循环依赖处理等机制,提升应用的可维护性与可测试性,是现代Java开发的核心基石。
|
5月前
|
XML Java 应用服务中间件
【SpringBoot(一)】Spring的认知、容器功能讲解与自动装配原理的入门,带你熟悉Springboot中基本的注解使用
SpringBoot专栏开篇第一章,讲述认识SpringBoot、Bean容器功能的讲解、自动装配原理的入门,还有其他常用的Springboot注解!如果想要了解SpringBoot,那么就进来看看吧!
619 2
|
6月前
|
XML Java 数据格式
常用SpringBoot注解汇总与用法说明
这些注解的使用和组合是Spring Boot快速开发和微服务实现的基础,通过它们,可以有效地指导Spring容器进行类发现、自动装配、配置、代理和管理等核心功能。开发者应当根据项目实际需求,运用这些注解来优化代码结构和服务逻辑。
448 12
|
7月前
|
Kubernetes Docker Python
Docker 与 Kubernetes 容器化部署核心技术及企业级应用实践全方案解析
本文详解Docker与Kubernetes容器化技术,涵盖概念原理、环境搭建、镜像构建、应用部署及监控扩展,助你掌握企业级容器化方案,提升应用开发与运维效率。
1070 108
|
8月前
|
存储 监控 测试技术
如何将现有的应用程序迁移到Docker容器中?
如何将现有的应用程序迁移到Docker容器中?
627 57
|
5月前
|
监控 Kubernetes 安全
还没搞懂Docker? Docker容器技术实战指南 ! 从入门到企业级应用 !
蒋星熠Jaxonic,技术探索者,以代码为笔,在二进制星河中书写极客诗篇。专注Docker与容器化实践,分享从入门到企业级应用的深度经验,助力开发者乘风破浪,驶向云原生新世界。
还没搞懂Docker? Docker容器技术实战指南 ! 从入门到企业级应用 !