普通注解使用

@ComponentScan
  includeFilters()方法指定Spring扫描的时候按照什么规则只需要包含哪些组件 
  excludeFilters()方法指定Spring扫描的时候按照什么规则排除哪些组件。两个方法的返回值都是Filter[]数组,在ComponentScan注解类的内部存在Filter注解类。 
  规则:FilterType 是枚举类, 包含了
                FilterType.ANNOTATION 注解类型。                 FilterType.ASSIGNABLE_TYPE 通过指定的类型。 
                FilterType.ASPECTJ 。
                FilterType.REGEX 正则表达式。 
                FilterType.CUSTOM 自定义筛选器(下面有实例代码,实现TypeFilter)

类上面可以书写多个@ComponentScan,因为在jdk8中,@ComponentScan里面是@ComponentScans

package com.irving.aop.config;
import com.irving.aop.entity.User;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.FilterType;
import org.springframework.stereotype.Service;
 /*excludeFilters = {
       @ComponentScan.Filter(type = FilterType.ANNOTATION,classes = {Service.class})  不包含
 }*/
@Configuration
@ComponentScan(value = "com.irving.aop",
        includeFilters={
                @ComponentScan.Filter(type = FilterType.ANNOTATION,classes = {Service.class}),
        },useDefaultFilters = false
                    )
public class AopConfig {
    @Bean
    public User getUser(){
        return new User();
    }
}
package com.irving.aop.config;
import org.springframework.core.type.AnnotationMetadata;
import org.springframework.core.type.classreading.MetadataReader;
import org.springframework.core.type.classreading.MetadataReaderFactory;
import org.springframework.core.type.filter.TypeFilter;
import org.springframework.stereotype.Component;
import java.io.IOException;
@Component
public class MyTypeFilter implements TypeFilter {
    @Override
    public boolean match(MetadataReader metadataReader, MetadataReaderFactory metadataReaderFactory) throws IOException {
        AnnotationMetadata annotationMetadata = metadataReader.getAnnotationMetadata();
        String className = annotationMetadata.getClassName();
        if(className.contains("Service")){
            return true;
        }
        return false;
    }
}

Scope

 @Conditional

import org.springframework.context.annotation.Condition;
import org.springframework.context.annotation.ConditionContext;
import org.springframework.core.env.Environment;
import org.springframework.core.type.AnnotatedTypeMetadata;

public class WindowsCondition implements Condition {
    @Override
    public boolean matches(ConditionContext context, AnnotatedTypeMetadata metadata) {
        Environment environment = context.getEnvironment();
        String property = environment.getProperty("os.name");
        if (property.contains("Windows")){
            return true;
        }
        return false;
    }
}

在这里插入图片描述

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值