SpringBoot学习2---走向自动装配

本文深入探讨Spring框架中的模式注解装配,包括@Component、@Repository等注解的使用,自定义注解的过程,以及通过@ComponentScan进行组件扫描的实践。文章通过实例展示了如何创建并装配自定义的模式注解。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

了解SpringBoot的自动装配前,还是要了解下Spring的手动装配,Spring有很多种手动装配模式,今天先介绍模式注解装配

模式注解是一种用于声明在应用中扮演“组件”角色的注解。什么意思呢?这句话有两个关键字,一个是应用,一个是组件,应用其实指的就是我们的Spring应用或者SpringBoot应用,什么是组件呢,其实用过Spring的同学知道那些在类上拥有@Component,@Service,@Repository的注解就是一种组件。

如Spring Framework 中的 @Repository 标注在任何类上 ,用于扮演仓储角色的模式注解;@Component 作为一种由 Spring 容器托管的通用模式组件,任何被@Component 标准的组件均为组件扫描的候选对象。类似地,凡是被 @Component 元标注(meta-annotated)的注解,如 @Service ,当任何组件标注它时,也被视作组件扫描的候选对象。我们可以看看@Service注解的源码:

这个注解其实包含了@Component。所以它也可以被视为组件扫描的候选对象。

差点忘记了Spring3.1中出现的新注解,@Configuration,这表示当前的类是一个配置的注解。下图是模式注解举例:

那么讲了这么多的模式注解,怎么对他们进行装配呢?装配呢一共有两种方式:

一种是<context:component-scan>方式,这种方式往往写在XML配置文件中:

<!-- 激活注解驱动特性 -->
<context:annotation-config />
<!-- 找寻被 @Component 或者其派生 Annotation 标记的类(Class),将它们注册为 Spring Bean -->
<context:component-scan base-package="com.xxx" />

还有一种是@ComponentScan

@ComponentScan(basePackages = "com.xxx")
public class SpringConfiguration {
...
}

OK,模式注解讲到这里,那么我开始自定义一个模式注解,并对它进行手动装配。

首先定义一个注解FirstLevelRepository ,一会儿这个注解会被应用到其他类上,用于被装配,这个注解很简单,就是其上面有一个@Repository,另外属性就只有一个value

@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Repository
public @interface FirstLevelRepository {

    String value() default "";

}

再自定义一个注解,这个注解上面使用了FirstLevelRespository注解:

@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@FirstLevelRepository
public @interface SecondLevelRepository {

    String value() default "";

}

注解定义完了,那么我就要开始定义一个类来使用这些注解了:

@SecondLevelRepository(value = "myFirstLevelRepository") // Bean 名称
public class MyFirstLevelRepository {
}

定义了一个什么都不做的类,这个类上面使用了SecondLevelRepository注解。

接下来我们写一个启动类来对这些模式注解进行装配:

@ComponentScan(basePackages = "com.zzm.repository")
public class RepositoryBootstrap {

    public static void main(String[] args) {
        ConfigurableApplicationContext context = new SpringApplicationBuilder(RepositoryBootstrap.class)
                .web(WebApplicationType.NONE)
                .run(args);

        // myFirstLevelRepository Bean 是否存在
        MyFirstLevelRepository myFirstLevelRepository =
                context.getBean("myFirstLevelRepository",MyFirstLevelRepository.class);

        System.out.println("myFirstLevelRepository Bean : "+myFirstLevelRepository);

        // 关闭上下文
        context.close();
    }
}

这里我们使用了SpringApplicationBuilder,这是个什么东东呢?官网说它可以替代我们在SpringBoot中经常使用的SpringApplication.run(XXX.class, args),并且能够构建Spring上下文。什么时候用SpringApplicationBuilder呢?当你不想装配太多东西,需求很简单的时候,就比如我们这个例子,只想装配我们自定义的注解部分。这里还写了一个SpringApplicationBuilder(RepositoryBootstrap.class).web(WebApplicationType.NONE),表示我们的应用是非web类型的应用,不要启动内嵌的web server~类上面的@ComponentScan(basePackages = "com.zzm.repository")表示扫描指定包路径下的组件。也就是我们的MyFirstLevelRepository。

启动应用后会发现是这样的:

表示我们的类被装载到容器中了,可以发现@Component似乎具有一种“派生性”的作用。

@Component
    @Repository
         @FirstLevelRepository
              @SecondLevelRepository

其实我们在springBoot中常用的@SpringBootApplication就是一个模式注解。

OK模式注解装配就讲到这里,下一篇文章将讲述别的装配模式

### Spring Boot 中自动装配 Spring AMQP 实现消息队列功能 在 Spring Boot 应用程序中,可以通过 `spring-boot-starter-amqp` Starter 来简化基于 AMQP 协议的消息队列集成过程。以下是实现这一功能的具体方法: #### 1. 添加依赖项 为了启用 Spring AMQP 功能,在项目的构建文件(Maven 或 Gradle)中添加以下依赖项: 对于 Maven 用户: ```xml <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-amqp</artifactId> </dependency> ``` 对于 Gradle 用户: ```gradle implementation 'org.springframework.boot:spring-boot-starter-amqp' ``` 此依赖会自动引入 RabbitMQ 客户端以及必要的 Spring AMQP 组件[^3]。 --- #### 2. 配置 RabbitMQ 连接参数 在 `application.properties` 文件中定义 RabbitMQ 的连接属性: ```properties spring.rabbitmq.host=localhost spring.rabbitmq.port=5672 spring.rabbitmq.username=guest spring.rabbitmq.password=guest ``` 如果使用的是 Docker 环境或其他主机地址,请相应调整 `host` 和其他字段的值。 --- #### 3. 创建消息生产者 编写一个简单的服务来发送消息到指定的交换机或队列: ```java import org.springframework.amqp.core.AmqpTemplate; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; @Service public class MessageProducer { @Autowired private AmqpTemplate rabbitTemplate; public void sendMessage(String exchange, String routingKey, Object message) { rabbitTemplate.convertAndSend(exchange, routingKey, message); } } ``` 上述代码利用了 `AmqpTemplate` 接口的功能,它由 Spring Boot 自动注入并初始化[^4]。 --- #### 4. 创建消费者监听器 通过声明一个带有 `@RabbitListener` 注解的方法来接收来自特定队列的消息: ```java import org.springframework.amqp.rabbit.annotation.RabbitListener; import org.springframework.stereotype.Component; @Component public class MessageConsumer { @RabbitListener(queues = "exampleQueue") public void receiveMessage(String message) { System.out.println("Received Message: " + message); } } ``` 这里指定了要监听的目标队列为 `"exampleQueue"`,任何发往该队列的消息都会触发 `receiveMessage` 方法执行。 --- #### 5. 启用自动配置 确保主类上标注有 `@SpringBootApplication` 注解,这将激活默认的自动配置逻辑,包括扫描组件、加载工厂设置等操作[^2]: ```java import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication public class Application { public static void main(String[] args) { SpringApplication.run(Application.class, args); } } ``` --- #### 总结 以上步骤展示了如何借助 Spring Boot 的自动化特性快速搭建起支持 AMQP 的消息传递框架。开发者只需关注业务层面的设计而无需过多关心底层细节处理[^1]。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值