Spring常用注解

1.@Configuration注解

该类等价 与XML中配置beans,相当于Ioc容器,它的某个方法头上如果注册了@Bean,就会作为这个Spring容器中的Bean,与xml中配置的bean意思一样。

2.@Bean注解

向容器中注入组件

@Configuration
public class MainConfig {
    @Bean("darkred")
    //默认为方法名
    public Red red(){
        return new Red();
    }
}
    @Test
    public void contextLoads() {
        AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(MainConfig.class);
        String[] beanDefinitionNames = context.getBeanDefinitionNames();
        for (String name:beanDefinitionNames) {
            System.out.println(name);
        }
    }

在这里插入图片描述
3.@ComponentScan注解

@Configuration
//value 指定要扫描的路径 (会默认扫描该路径下的@Controller, @Service, @Repository,@Component)
//excludeFilters = Filter{}  按什么规则排除
/*includeFilters = Filter{}  自定义规则筛选  useDefaultFilters要设置为false (默认为true)
  type = FilterType.ANNOTATION :按注解筛选
  type = FilterType.ASSIGNABLE_TYPE :按给定类型筛选
  type = FilterType.CUSTOM :按自定义筛选
  [自定义过滤参考](https://blog.youkuaiyun.com/qq_39548700/article/details/105996227)
*/
//@Import({Red.class,Green.class})
@ComponentScan(value = "me.zhengjie.modules.ioc",includeFilters = {
        @ComponentScan.Filter(type = FilterType.ANNOTATION,classes = Controller.class),
        @ComponentScan.Filter(type = FilterType.ASSIGNABLE_TYPE,classes = IocService.class),
        @ComponentScan.Filter(type = FilterType.CUSTOM,classes = IocTypeFilter.class)
},useDefaultFilters = false)
public class MainConfig {
    @Bean("darkred")
    public Red red(){
        System.out.println("注入容器。。。。");
        return new Red();
    }
}

4.@Scope注解 @Lazy注解

@Configuration
@ComponentScan(value = "me.zhengjie.modules.ioc")
public class MainConfig {

    //singleton:单实例(默认),ioc容器启用时会调用方法创建对象注入到容器中,以后每次调用,都是从容器中直接获取(Map.get())
    //prototype:多实例  ioc容器启用时不对调用方法创建对象注入,每次调用时创建对象
    //@Lazy :懒加载,ioc容器启动的时候不创建对象,第一次使用时创建,并初始化
    @Scope("prototype")
    @Bean("darkred")
    @Lazy
    public Red red(){
        return new Red();
    }
}

5.@Conditional注解
条件注入组件

    @Bean
    @Conditional(value = redCondition.class)
    public Red blue(){
        return new Red();
    }
public class redCondition implements Condition {
    @Override
    public boolean matches(ConditionContext conditionContext, AnnotatedTypeMetadata annotatedTypeMetadata) {
        //获取ioc使用的beanFactory
        ConfigurableListableBeanFactory beanFactory = conditionContext.getBeanFactory();
        //获取类加载器
        ClassLoader classLoader = conditionContext.getClassLoader();
        //获取当前环境信息
        Environment environment = conditionContext.getEnvironment();
        //获取bean定义的注册类
        BeanDefinitionRegistry registry = conditionContext.getRegistry();
        
        //将返回值为true的组件注入到容器中
        return true;
    }
}

6.@Import注解

@Configuration
@Import({Red.class,Green.class})
public class MainConfig {
}

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值