Spring注解详解:@ComponentScan自动扫描组件使用

本文详细介绍了Spring的@ComponentScan注解的使用,包括无注解方式的component-scan配置,注解方式的@ComponentScan使用,以及@ComponentScan的扫描规则。讲解了如何通过@ComponentScan扫描特定包下的注解类,如@Controller、@Service、@Repository等,并展示了如何通过@Filter进行自定义扫描过滤规则。此外,还提到了自定义扫描策略,如ASSIGNABLE_TYPE等,帮助读者深入理解Spring组件扫描的机制。
摘要由CSDN通过智能技术生成

目录

无注解方式component-scan使用

注解方式@ComponentScan使用

@ComponentScan的扫描规则


无注解方式component-scan使用

之前,我们需要扫描工程下一些类上所标注的注解,这些常用注解有:

@Controller,@Service,@Component,@Repository

通过在Spring的配置文件中配置context:component-scan扫描对应包下扫描这些注解的方式:

<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns:p="http://www.springframework.org/schema/p"
	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-2.5.xsd">

    <!--@Controller,@Service,@Component,@Repository-->
	<context:component-scan base-package="com.jektong.spring"/>

</beans>

注解方式**@ComponentScan**使用

建三个类,依次将

@Controller,@Repository,@Service,标注这些类:

9676e1d230404d359e879afc90694c96.png

图1

现在通过使用注解**@ComponentScan的方式来扫描所在包下面的这些类:之前定义的PersonConfig修改:**

package com.jektong.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值