Sping注解开发

  基本注解

  @Configuration

  作用: 标记在类上表示是一个配置类(相当于一个配置类)

  @Bean

  作用: 在容器中放一个bean相当于xml文件里的bean标签

  @Configuration

  public class DemoConfig {

  @Bean

  public Person person(){

  Person person = new Person();

  person.setPerson("china");

  return person;

  }

  }

  @scope

  作用: 指定作用域

  

scope

  分为单例,多例等

  @ComponentScan()

  作用: 包扫描

  @Configuration

  @ComponentScan("com.zyh.pratice")

  public class DemoConfig {

  @Bean

  public Person person(){

  Person person = new Person();

  person.setPerson("china");

  return person;

  }

  }

  指定报下的注解都可以被扫描到

  过滤

  excludeFilters:不包含

  用法

  @Configuration

  @ComponentScan(value = "com.zyh.pratice", excludeFilters ={

  @ComponentScan.Filter(type = FilterType.ASSIGNABLE_TYPE,value = IncludeBean.class),

  })

  public class DemoConfig {

  includeFilters:包含

  用法

  @Configuration

  @ComponentScan(value = "com.zyh.pratice", includeFilters ={

  @ComponentScan.Filter(type = FilterType.ASSIGNABLE_TYPE,value = IncludeBean.class),

  },useDefaultFilters = false)

  注意 用includeFilters 时要将useDefaultFilters 设为false

  FilterType

  有这几种类型可选

  

在这里插入图片描述

  @Conditional  无锡妇科医院 http://www.bhnnk120.com/

  作用:条件注解,传入一个Condition的实现类,条件满足时才去加载bean

  @Conditional(ConditionDemo.class)

  public Bill bill(){

  return new Bill();

  }

  Condition

  作用位置

  被bean注释的方法上:满足条件时注册该bean

  类上:满足条件时注册该类

  import

  作用:导入指定的类

  @Configuration

  @ComponentScan

  @Import(ImportDemo.class)

  public class DemoConfig2 {

  }

  这样 ImportDemo就会被注册到容器中

  ImportSelector

  实现ImportSelector接口后,可以返回多个类,注册到容器中

  

ImportSelector

  ImportBeanDefinitionRegistrar

  public class BeanDefinitionDemo implements ImportBeanDefinitionRegistrar {

  public void registerBeanDefinitions(AnnotationMetadata annotationMetadata, BeanDefinitionRegistry beanDefinitionRegistry) {

  BeanDefinition beanDefinition = new RootBeanDefinition(BeanDemo.class);

  beanDefinitionRegistry.registerBeanDefinition("beanDemo",beanDefinition);


来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/69945560/viewspace-2655526/,如需转载,请注明出处,否则将追究法律责任。

转载于:http://blog.itpub.net/69945560/viewspace-2655526/

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值