Spring学习笔记之通过注解配置Bean(1)

1、配置形式
①、基于XML文件的方式;
②、基于注解配置Bean
2、Bean的配置方式
①通过全类名(反射)
②通过工厂方法(静态方法&实例工厂方法)
③FactoryBean

3、特定组件
@Component:基本注解,标识了一个受Spring管理的组件
@Repository:标识持久层组件
@Service:标识服务层(业务层组件)
@Controller:标识表现层组件

当在组件类上使用了特定的注解之后,还需要再Spring的配置文件中声明 context:component-scan
-base-package属性指定一个需要扫描的基类包,Spring容器将会扫描这个基类包里及其所有子包中的所有类,
-当扫描多个包时,可以使用逗号分隔
-如果仅希望扫描特定的类而非所有类,可使用resource-pattern属性过滤特定的类
context:include-filter子节点表示要包含的目标类
context:exclude-fileter子节点表示要排除在外的目标类
context:component-scan下可以拥有若干个context:include-filter和context:exclude-fileter子节点

<!-- 指定Spring IOC 容器扫描的包 -->
<!-- 可以通过resource-pattern扫描指定的资源 -->
<!--  <context:component-scan
       base-package="com.jhh.spring.beans.annotation"
       resource-pattern="repository/*.class"></context:component-scan>
 -->
<!-- 
   use-default-filters true 扫描默认的注解
 -->
 <context:component-scan base-package="com.jhh.spring.beans.annotation" use-default-filters="false">
<!-- 排除哪些指定表达式的组件 
    annotation 通过注解去指定包含哪些注解排除哪些注解
    <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Repository"/>
    assignable 通过具体的类去指定包含哪些 排除哪些
    <context:exclude-filter type="assignable" expression="com.jhh.spring.beans.annotation.repository.UserRepositoryImpl"/>
-->
<!-- 
    <context:include-filter type="annotation" expression="org.springframework.stereotype.Repository"/>
-->    
    <context:include-filter type="assignable" expression="com.jhh.spring.beans.annotation.repository.UserRepositoryImpl"/>
   </context:component-scan>
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值