栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

SpringBoot 中@SpringBootApplication注解

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

SpringBoot 中@SpringBootApplication注解

@SpringBootApplication 可分为三个比较重要的注解

分别是:

@SpringBootConfiguration
@EnableAutoConfiguration
@ComponentScan

@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@documented
@Inherited
@SpringBootConfiguration
@EnableAutoConfiguration
@ComponentScan(
    excludeFilters = {@Filter(
    type = FilterType.CUSTOM,
    classes = {TypeExcludeFilter.class}
), @Filter(
    type = FilterType.CUSTOM,
    classes = {AutoConfigurationExcludeFilter.class}
)}
)

前四个为Java常用的元注解,这里不做过多解释

下面依次分析后三个注解的作用

@SpringBootConfiguration

@SpringBootConfiguration注解可以理解为springboot配置类,我们可以简单的点进去看一看里面的内容,第一次点击得到这四个注解,

@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@documented
@Configuration

前三个为元注解,第四个@Configuration 表明这是一个配置类,点进去有一个@Component注解又可以知道他的本质就是一个组件,其实就是为了不用像spring那样写繁琐的XML文件

@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@documented
@Component

说简单点,加了这个注解,相当于写了一个配置文件,你可以在里面配置各种Bean 你甚至可以像这样在XXXAplication中注册一个Bean 只是没有人这么做

@SpringBootApplication
@MapperScan("com.xxxx.server.mapper")
@EnableScheduling
public class XXXApplication {

    public static void main(String[] args) {
        SpringApplication.run(XXXApplication.class);
    }
    
    @Bean
    public Admin adminBean(){
        return new Admin();
    }
    
}

@ComponentScan

源码中是这样的:

@ComponentScan(
    excludeFilters = {@Filter(
    type = FilterType.CUSTOM,
    classes = {TypeExcludeFilter.class}
), @Filter(
    type = FilterType.CUSTOM,
    classes = {AutoConfigurationExcludeFilter.class}
)}
)

与ComponentScan注解相对应的XML配置就是, 根据指定的配置自动扫描package,将符合条件的组件加入到IOC容器中;

@ComponentScan有如下属性:

value:指定要扫描的package;
includeFilters=Filter[]:指定只包含的组件
excludeFilters=Filter[]:指定需要排除的组件;
useDefaultFilters=true/false:指定是否需要使用Spring默认的扫描规则:被@Component, @Repository, @Service, @Controller或者已经声明过@Component自定义注解标记的组件;

在过滤时要加上includeFilters或者excludeFilters属性 并且用大括号,因为可以有多个过滤器

过滤器之前还要加上@Filter注解

其中在过滤规范中还有几个过滤方式(FilterType),这是一个枚举类,分别是 按照注解类,按照给定的类型,按照ASPECTJ表达式,按照正则和自定义过滤规范,这里源码用的就是自定义过滤规范排除一部分类

public enum FilterType {
    ANNOTATION,
    ASSIGNABLE_TYPE,
    ASPECTJ,
    REGEX,
    CUSTOM;

    private FilterType() {
    }
}

可以试着点进一个自定义过滤规范 TypeExcludeFilter

public class TypeExcludeFilter implements TypeFilter, BeanFactoryAware {

	private BeanFactory beanFactory;

	private Collection delegates;

	@Override
	public void setBeanFactory(BeanFactory beanFactory) throws BeansException {
		this.beanFactory = beanFactory;
	}

	@Override
	public boolean match(metadataReader metadataReader, metadataReaderFactory metadataReaderFactory)
			throws IOException {
		if (this.beanFactory instanceof ListableBeanFactory && getClass() == TypeExcludeFilter.class) {
			for (TypeExcludeFilter delegate : getDelegates()) {
				if (delegate.match(metadataReader, metadataReaderFactory)) {
					return true;
				}
			}
		}
		return false;
	}

	private Collection getDelegates() {
		Collection delegates = this.delegates;
		if (delegates == null) {
			delegates = ((ListableBeanFactory) this.beanFactory).getBeansOfType(TypeExcludeFilter.class).values();
			this.delegates = delegates;
		}
		return delegates;
	}

	@Override
	public boolean equals(Object obj) {
		throw new IllegalStateException("TypeExcludeFilter " + getClass() + " has not implemented equals");
	}

	@Override
	public int hashCode() {
		throw new IllegalStateException("TypeExcludeFilter " + getClass() + " has not implemented hashCode");
	}

}

继承自TypeFilter接口 实现了接口里的唯一的match()方法,是否过滤就取决于这个方法返回的true还是false

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/777304.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号