转自:https://www.jianshu.com/p/35685394c1fc
1 关于@EnableAutoConfiguration注解
我们使用springBoot创建一个项目时,我们会发现我们的项目启动类有@SpringBootApplication
如下:
@EnableTransactionManagement
@ServletComponentScan
@MapperScan({"com.txdata.*.*.dao","com.txdata.*.dao"})
@SpringBootApplication
@EnableCaching
public class TxdataApplication {
public static void main(String[] args) {
SpringApplication.run(TxdataApplication.class, args);
System.out.println("ヾ(◍°∇°◍)ノ゙ 项目启动成功 ");
}
}
点进这个注解进去看实现,如下:
//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//
package org.springframework.boot.autoconfigure;
import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Inherited;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import org.springframework.boot.SpringBootConfiguration;
import org.springframework.boot.context.TypeExcludeFilter;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.FilterType;
import org.springframework.context.annotation.ComponentScan.Filter;
import org.springframework.core.annotation.AliasFor;
@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}
)}
)
public @interface SpringBootApplication {
@AliasFor(
annotation = EnableAutoConfiguration.class
)
Class<?>[] exclude() default {};
@AliasFor(
annotation = EnableAutoConfiguration.class
)
String[] excludeName() default {};
@AliasFor(
annotation = ComponentScan.class,
attribute = "basePackages"
)
String[] scanBasePackages() default {};
@AliasFor(
annotation = ComponentScan.class,
attribute = "basePackageClasses"
)
Class<?>[] scanBasePackageClasses() default {};
}
我们可以发现该注解上面已包含@EnableAutoConfiguration。
@EnableAutoConfiguration:启用 SpringBoot 的自动配置机制
@ComponentScan: 扫描被@Component (@Service,@Controller)注解的 bean,注解默认会扫描该类所在的包下所有的类。
@SpringBootConfiguration:允许在 Spring 上下文中注册额外的 bean 或导入其他配置类
@EnableAutoConfiguration作用:
帮助SpringBoot应用将所有符合条件的@Configuration配置都加载到当前SpringBoot,并创建对应配置类的Bean,并把该Bean实体交给IoC容器进行管理。
@EnableAutoConfiguration的实现代码如下:
//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//
package org.springframework.boot.autoconfigure;
import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Inherited;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import org.springframework.context.annotation.Import;
@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Inherited
@AutoConfigurationPackage
@Import({AutoConfigurationImportSelector.class})
public @interface EnableAutoConfiguration {
String ENABLED_OVERRIDE_PROPERTY = "spring.boot.enableautoconfiguration";
Class<?>[] exclude() default {};
String[] excludeName() default {};
}
关于这个注解的实现原理自行百度,我现今一个入门小白也只能理解到这儿~(百度一下网上一大堆的 = - =!)
2 关于@EnableAutoConfiguration注解使用问题
比如说:我们的项目启动类上面已经讲到包含了该注解了,默认会扫描与项目启动类同级或者更低级目录下的所有使用@Configuration注解的类,如果我们想要避开某些配置类的扫描(包括避开一些第三方jar包下面的配置),
我们可以进行如下操作:
package com.txdata;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.servlet.ServletComponentScan;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.transaction.annotation.EnableTransactionManagement;
@EnableAutoConfiguration(exclude = {
org.springframework.boot.autoconfigure.security.servlet.SecurityAutoConfiguration.class
})
@EnableTransactionManagement
@ServletComponentScan
@MapperScan({"com.txdata.*.*.dao","com.txdata.*.dao"})
@SpringBootApplication
@EnableCaching
public class TxdataApplication {
public static void main(String[] args) {
SpringApplication.run(TxdataApplication.class, args);
System.out.println("ヾ(◍°∇°◍)ノ゙ txdata启动成功 ");
}
// @Override
// protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) {
// return builder.sources(TxdataApplication.class);
// }
}
避开配置扫描关键:
@EnableAutoConfiguration(exclude = {
org.springframework.boot.autoconfigure.security.servlet.SecurityAutoConfiguration.class
})
这样,在项目启动的时候就不会去扫描exclude后面的配置类。
反思:
如果在项目启动类不进行上面的"避开"操作,而在项目的某一个具体的配置类进行上述"避开"操作,效果会一样吗?
代码如下:
启动类(不进行"避开"操作)
package com.txdata;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.servlet.ServletComponentScan;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.transaction.annotation.EnableTransactionManagement;
@EnableTransactionManagement
@ServletComponentScan
@MapperScan({"com.txdata.*.*.dao","com.txdata.*.dao"})
@SpringBootApplication
@EnableCaching
public class TxdataApplication {
public static void main(String[] args) {
SpringApplication.run(TxdataApplication.class, args);
System.out.println("ヾ(◍°∇°◍)ノ゙ txdata启动成功 ");
}
// @Override
// protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) {
// return builder.sources(TxdataApplication.class);
// }
}
某个配置类(进行"避开"操作)
package com.txdata.common.config;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.context.annotation.Configuration;
@EnableAutoConfiguration(exclude = {
org.springframework.boot.autoconfigure.security.servlet.SecurityAutoConfiguration.class })
@Configuration
public class SecuityConfig {
}
效果会一样吗?(等过段时间理解了原理再回答这个问题~)
:"避开"操作放在不同地方会有不同效果,放在启动类的话,是针对整个项目的所有文件;放在具体某个配置类的话,是针对该配置类文件(作用范围不同)。
作者:Isak_e7c4
链接:https://www.jianshu.com/p/35685394c1fc
来源:简书
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。