【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注解的类,其他都扫描。

相关文章
|
5月前
|
缓存 监控 Java
SpringBoot @Scheduled 注解详解
使用`@Scheduled`注解实现方法周期性执行,支持固定间隔、延迟或Cron表达式触发,基于Spring Task,适用于日志清理、数据同步等定时任务场景。需启用`@EnableScheduling`,注意线程阻塞与分布式重复问题,推荐结合`@Async`异步处理,提升任务调度效率。
877 128
|
4月前
|
XML Java 应用服务中间件
【SpringBoot(一)】Spring的认知、容器功能讲解与自动装配原理的入门,带你熟悉Springboot中基本的注解使用
SpringBoot专栏开篇第一章,讲述认识SpringBoot、Bean容器功能的讲解、自动装配原理的入门,还有其他常用的Springboot注解!如果想要了解SpringBoot,那么就进来看看吧!
566 2
|
5月前
|
XML Java 数据格式
常用SpringBoot注解汇总与用法说明
这些注解的使用和组合是Spring Boot快速开发和微服务实现的基础,通过它们,可以有效地指导Spring容器进行类发现、自动装配、配置、代理和管理等核心功能。开发者应当根据项目实际需求,运用这些注解来优化代码结构和服务逻辑。
425 12
|
5月前
|
传感器 Java 数据库
探索Spring Boot的@Conditional注解的上下文配置
Spring Boot 的 `@Conditional` 注解可根据不同条件动态控制 Bean 的加载,提升应用的灵活性与可配置性。本文深入解析其用法与优势,并结合实例展示如何通过自定义条件类实现环境适配的智能配置。
308 0
探索Spring Boot的@Conditional注解的上下文配置
|
XML Java 测试技术
Spring IOC—基于注解配置和管理Bean 万字详解(通俗易懂)
Spring 第三节 IOC——基于注解配置和管理Bean 万字详解!
870 26
|
XML Java 数据格式
Spring5入门到实战------7、IOC容器-Bean管理XML方式(外部属性文件)
这篇文章是Spring5框架的实战教程,主要介绍了如何在Spring的IOC容器中通过XML配置方式使用外部属性文件来管理Bean,特别是数据库连接池的配置。文章详细讲解了创建属性文件、引入属性文件到Spring配置、以及如何使用属性占位符来引用属性文件中的值。
Spring5入门到实战------7、IOC容器-Bean管理XML方式(外部属性文件)
|
XML Java 数据格式
Spring5系列学习文章分享---第一篇(概述+特点+IOC原理+IOC并操作之bean的XML管理操作)
Spring5系列学习文章分享---第一篇(概述+特点+IOC原理+IOC并操作之bean的XML管理操作)
154 1
|
XML druid Java
Spring5系列学习文章分享---第二篇(IOC的bean管理factory+Bean作用域与生命周期+自动装配+基于注解管理+外部属性管理之druid)
Spring5系列学习文章分享---第二篇(IOC的bean管理factory+Bean作用域与生命周期+自动装配+基于注解管理+外部属性管理之druid)
226 0
|
存储 Java 应用服务中间件
【Spring】IoC和DI,控制反转,Bean对象的获取方式
IoC,DI,控制反转容器,Bean的基本常识,类注解@Controller,获取Bean对象的常用三种方式
534 12
|
XML Java 数据格式
Spring IOC—基于XML配置Bean的更多内容和细节(通俗易懂)
Spring 第二节内容补充 关于Bean配置的更多内容和细节 万字详解!
777 18
Spring IOC—基于XML配置Bean的更多内容和细节(通俗易懂)