一、在classpath中扫描组件

1.组件扫描:Spring能够从classpath下自动扫描,侦测和实例化具有特定注解的组件。
2.特定组件包括:
@Component:基本注解,标识了一个受Spring管理的组件
@Respository:标识持久层组件
@Service:标识服务层(业务层)组件
@Controller:标识表现层组件
3.对于扫描到的组件,Spring有默认的命名策略;使用非限定类名,第一个字母小写。也可以在注解中通过value属性值标识组件的名称。
4.使用注解后,还需要在Spring的配置文件中声明<context:component-scan>:
有以下属性: base-package 指定需要扫描的基类包。扫描多个包可用逗号分隔
Spring配置文件
这里需要导入context命名空间,会扫描annotation下的文件, 在pattern指定下,只会扫描repository子包下的 文件
<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"xmlns:p="http://www.springframework.org/schema/p"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd"><!-- 指定Spring IOC容器扫描的包 --><context:component-scan base-package="com.hcx.annotation"resource-pattern="repository/*.class"></context:component-scan></beans>


添加注解后的一个类:
使用repository注解,并重新设定id值。
@Repository("userRespository")
public class UserRepositoryImpl implements UserRepository{@Overridepublic void save() {System.out.println("UserRepositoryImpl  Save");}}

main测试方法:

在这里获取UserRepositoryImpl实例的时候,就使用了改变后的id名称“userRespository” 
public class Main {public static void main(String[] args) {ApplicationContext ctx=new ClassPathXmlApplicationContext("beans-annotation.xml");TestObject to=(TestObject) ctx.getBean("testObject");System.out.println(to);UserController uc=(UserController) ctx.getBean("userController");//id使用类名首字母小写uc.execute();UserService us=(UserService) ctx.getBean("userService");us.add();//@Repository("userRespository")实现类里可以重新定义id名称UserRepositoryImpl urimpl=(UserRepositoryImpl) ctx.getBean("userRespository");urimpl.save();}
}

二、筛选扫描内容

context:exclude-filter和context:include-filter可以指定包含或者不包含哪些包下内容。其中expression属性值可以是 注解路径或者是类。对应type是annotation/assignable
这个例子中,排除了annotation包下使用@Repository注解的类,注意:expression内容是注解的路径

 <context:component-scan base-package="com.hcx.annotation"><context:exclude-filter type="annotation" expression="org.springframework.stereotype.Repository"/></context:component-scan>
该例使用只包含某个子包,需要在scan节点将默认filter过滤器关掉,然后再设定
<context:component-scan base-package="com.hcx.annotation" use-default-filters="false"><context:include-filter type="annotation" expression="org.springframework.stereotype.Repository"/></context:component-scan>

使用类名方式,type=“assignable”

<context:component-scan base-package="com.hcx.annotation" ><context:exclude-filter type="assignable" expression="com.hcx.annotation.repository.UserRepositoryImpl"/>
</context:component-scan>


三、利用注解建立bean之间的关系

1.使用@Autowired注解
自动装配具有兼容类型的单个Bean属性。构造器、普通字段、有参数的方法都是可以使用
默认情况下使用该注解的属性都需要在IOC容器中,不然会报错。使用required=false属性可以选择不加载。
@Controller
public class UserController {@Autowired(required=false)private TestObject test;@Autowiredprivate UserService userService;public void execute(){System.out.println("UserController execute");userService.add();}}

20.Spring 通过注解配置Bean相关推荐

  1. Spring(二)--FactoryBean、bean的后置处理器、数据库连接池、引用外部文件、使用注解配置bean等...

    实验1:配置通过静态工厂方法创建的bean  [通过静态方法提供实例对象,工厂类本身不需要实例化!] 1.创建静态工厂类 public class StaticFactory {private sta ...

  2. Spring中注解注入bean和配置文件注入bean

    注解的方式确实比手动写xml文件注入要方便快捷很多,省去了很多不必要的时间去写xml文件 按以往要注入bean的时候,需要去配置一个xml,当然也可以直接扫描包体,用xml注入bean有以下方法: 1 ...

  3. Spring使用注解配置依赖注入

    大部分情况下,使用Spring配置依赖注入时,都是使用注解来进行配置,因为注解比xml要方便和简单.不过类似于数据源对象这种配置信息容易变更的对象除外,这种对象使用xml文件来进行配置会更适合,方便于 ...

  4. Spring通过注解装配Bean

    通过注解实现ServiceImpl业务 一.使用@Component装配Bean 1. 定义类:User 在类上面加@Component注解,在属性上面加@Value值 package com.wbg ...

  5. 带你简化理解Spring 基于注解配置的原理

    1 需求说明 自己写一个简单的 Spring 容器, 通过读取类的注解 (@Component @Controller @Service @Reponsitory),将对象注入到 IOC 容器 bea ...

  6. Spring~用注解读取bean(属性注入、构造方法注入、set注入)

    文章目录 注解@Autowired. 属性注入 Setter注入 构造方法注入 三种注入的优缺点 注解@Resource. 1.属性注入 2.构造方法注入 @Resource与@Autowired区别 ...

  7. Spring入门第一课:Spring基础与配置Bean

    1.入门 Spring是简化java开发的一个框架,其中IoC和AOP是Spring的两个重要核心.由于Spring是非侵入性的,通过Ioc容器来管理bean的生命周期,还整合了许多其他的优秀框架,所 ...

  8. Spring事务注解配置

    一,spring基于注解式的事务配置方法:@Transactional 1.xml配置 <?xml version="1.0" encoding="UTF-8&qu ...

  9. Spring框架学习笔记03:初探Spring——利用注解配置类取代Spring配置文件

    文章目录 一.课程引入 二.利用注解配置类取代Spring配置文件 (一)打开项目[SpringDemo2021] (二)创建net.hw.spring.lesson03包 (三)移植上一讲的接口和类 ...

最新文章

  1. 2108889队2021年数学建模美赛C题花絮视频!
  2. ubuntu14.04下通过.frm, .MYD,.MYI文件恢复建立mysql数据库
  3. VTK修炼之道41:频域处理_低通滤波(理想+巴特沃兹)
  4. DevExpress GridControl使用方法总结
  5. animiz动画制作软件_AN动画制作软件
  6. linux 父子进程 资源_linux 父子进程 资源_实验4 Linux父子进程同步
  7. (130)System Verilog忽略仓数与违反仓数用法
  8. android studio查看jar包源码,Android Studio查看源代码报错
  9. 数据密集型应用系统设计--数据存储与检索
  10. DirectShow程序运行过程简析
  11. 千万千万不要运行的Linux命令
  12. 【arp】关于arp和arping命令的使用
  13. yml格式(list)
  14. 浏览器控件打开PDF文件时文件内容不显示的解决方法
  15. python播放音频文件——playsound
  16. Java 错别字检查接口 API
  17. 滴滴裁员2000,赔偿方案已出!程序员:我很开心!
  18. 十年风雨,一个普通程序员的成长之路(五) 成长:得到与教训
  19. 将Python代码制作成exe程序
  20. Qt 如何将QPushButton弹起

热门文章

  1. XML介绍之XML的语法与元素
  2. 登陆启明星辰天清web网关
  3. IOS解压缩和压缩文件
  4. 3dmax:3dmax三维VR渲染设置之高级灯光渲染(经典案例—利用目标灯光制作光域网筒灯效果)图文教程
  5. 200年前,数据可视化如何拯救成千上万人?
  6. 1月8日,30秒知全网,精选7个热点
  7. 整数规划、混合整数规划基础知识
  8. 孩子过敏源检测,如何避免被坑?
  9. MPB:韩东飞、郝光飞等细菌转录组分析样品制备方法
  10. 我的作品之《Unity虚拟现实开发实战》,翻译自《Unity Virtual Reality Projects》