关于SpringIOC容器组件一些扫描模式的笔记

本人的一些SpringIOC容器学习笔记

spring能够从classpath下自动扫描,侦测和实例化具有特定注解的组件。
可以将带有注解的类,注入到IOC容器当中,这些类受IOC容器管理

<?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">

<!--    自动扫描指定包以及子包下所有添加了注解的类,然后加入到IOC容器当中-->
    <context:component-scan base-package="spring"></context:component-scan>
</beans>

可以设置只扫描特定的类:

<!--    设置只扫描时,需更改扫描规则use-default-filters="false"-->
    <context:component-scan base-package="spring" use-default-filters="false">
<!--        只扫描特定注解的类-->
        <context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
<!--        只扫描自己写的某个类-->
        <context:include-filter type="assignable" expression="spring.controller.UserController"/>
    </context:component-scan>

可以设置扫描时排除的类

    <context:component-scan base-package="spring">
        <!--        只排除特定注解的类-->
        <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
        <!--        只排除自己写的某个类-->
        <context:exclude-filter type="assignable" expression="spring.controller.UserController"/>
    </context:component-scan>

特定组件包括:

  • @Component:基本注解,标识了一个受spring管理的组件
  • @Repository:标识持久层组件
  • @Service:标识服务层(业务层)组件
  • @Controller:标识表现层组件
  • @Autowired:自动注入,会将IOC容器中的对象自动注入给我们要使用的对象当中。注意,使用这个注解的前提是,要被注入的那个对象所在的类要受IOC容器管理。

使用getBean获取类对象的方法是让类名第一个字母小写,等价于< bean>中的id属性
在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值