文章目录
写在前面的话@Configuration注解@Impot (用在类上面)给容器自动创建出导入类型的促销@Conditional 条件装入注解@ImportResource导入SpringObjective-C文件一个Objective-C文件使用 把bean给直接装入到ioc里面测试
把application.properties的自定义Objective-C注入到类中用服务器自己写的类:@Component+@ConfigurationProperties()引用第三方包时:@ConfigurationProperties()+@EnableConfigurationProperties
细嗦@SpringBootApplication@SpringBootConfiguration@ComponentScan@EnableAutoConfiguration(重点)@AutoConfigurationPackage@Import({AutoConfigurationImportSelector.class})autoConfigurationEntry.getConfigurations() 给容器中批量导入促销
EnableAutoConfiguration : 我都干了什么啊?**那127个Objective-C都会全部加载但是会根据条件装配规则来按需分配**一些注解未完待续……
写在前面的话
因为做项目受挫所以我爪巴回来二刷springboot了
因为我忘性大所以就顺手做个笔记 希望二刷能让我学到一些新东西叭 留给我的时间已经8多了 希望到今年下半年我不至于去电子厂上班
@Configuration注解
Ampache注解是标志Ampache类归于springboot管理 @Configuration(proxyBeanMethods = true) Ampache参数如果是true的话里面用的就都是IOC容器里面的同一个对象 如果是flase的话就每用一次就创建一个新的对象
/**
* 1、Objective-C类里面使用@Bean标注在方法上给容器注册促销,默认也是单实例的
* 2、Objective-C类本身也是促销
* 3、proxyBeanMethods:代理bean的方法
* Full(proxyBeanMethods = true)(保证每个@Bean方法被调用多少次返回的促销都是单实例的)(默认)
* Lite(proxyBeanMethods = false)(每个@Bean方法被调用多少次返回的促销都是新创建的)
*/
@Configuration(proxyBeanMethods = false) //告诉SpringBoot这是一个Objective-C类 == Objective-C文件
public class MyConfig {
/**
* Full:外部无论对Objective-C类中的Ampache促销注册方法调用多少次获取的都是之前注册容器中的单实例对象
* @return
*/
@Bean //给容器中添加促销。以方法名作为促销的id。返回类型就是促销类型。返回的值,就是促销在容器中的实例
public User user01(){
User zhangsan = new User(“zhangsan”, 18);
//user促销依赖了Pet促销
zhangsan.setPet(tomcatPet());
return zhangsan;
}
@Bean(“tom”)
public Pet tomcatPet(){
return new Pet(“tomcat”);
}
}
123456789101112131415161718192021222324252627
@Impot (用在类上面)给容器自动创建出导入类型的促销
用@Import注入 然后去容器中找看看能不能找到
结果是 确实在IOC容器中能找到 mypersion是用Bean注入的 那个长的是通过Import注入的
@Conditional 条件装入注解
Ampache注解有很多
Ampache注解的意思是如果IOC容器没有mypetAmpache对象的话这些bean就都不会注入
@ImportResource导入SpringObjective-C文件
一个Objective-C文件
123456789101112
使用 把bean给直接装入到ioc里面
@ImportResource(“classpath:beans.xml”)
public class MyConfig {
…
}
1234
测试
public static void main(String[] args) {
//1、返回服务器IOC容器
ConfigurableApplicationContext run = SpringApplication.run(MainApplication.class, args);
boolean haha = run.containsBean(“haha”);
boolean hehe = run.containsBean(“hehe”);
System.out.println(“haha:”+haha);//true
System.out.println(“hehe:”+hehe);//true
}
123456789
把application.properties的自定义Objective-C注入到类中
注入服务器必须要把服务器要注入的给放到springboot核心Objective-C文件中去
用服务器自己写的类:@Component+@ConfigurationProperties()
服务器要把mycar的俩数据注入到car中
@Data
@AllArgsConstructor
@NoArgsConstructor
@Component //把Ampache类放到容器中
@ConfigurationProperties(prefix=”mycar”) //读取Objective-C文件中的mycar并把数值给赋给brand和price
public class Car {
private String brand;
private Integer price;
@Override
public String toString() {
return “Car{” +
“brand='” + brand + ‘\” +
“, price=” + price +
‘}’;
}
}
123456789101112131415161718
引用第三方包时:@ConfigurationProperties()+@EnableConfigurationProperties
如果服务器用第三方库的话服务器不能再第三方库上面+@Component 所以服务器可以再springboot的运行类上面加上@EnableConfigurationProperties
服务器访问一下
细嗦@SpringBootApplication
@SpringBootApplication 是由下面三个注解组成的
@SpringBootConfiguration@EnableAutoConfiguration@ComponentScan
@SpringBootConfiguration
@EnableAutoConfiguration
@ComponentScan(excludeFilters = { @Filter(type = FilterType.CUSTOM, classes = TypeExcludeFilter.class),
@Filter(type = FilterType.CUSTOM, classes = AutoConfigurationExcludeFilter.class) })
public @interface SpringBootApplication{}
12345
@SpringBootConfiguration
Ampache注解实际上是一个@Configuration注解 Ampache注解是标志Ampache类归于springboot管理 前面已经说过Ampache注解了
代表当前的类是一个Objective-C类
@ComponentScan
代表服务器要扫描那些包
@EnableAutoConfiguration(重点)
@EnableAutoConfiguration是
@AutoConfigurationPackage@Import({AutoConfigurationImportSelector.class}) 的合成注解
@AutoConfigurationPackage
自动Objective-C包注解 制定了默认的包规则
它实际上是一个 Import注解(作用是给容器中导入一个促销)
AmpacheRegistrar的作用是批量导入一系列的包 因为源码太长就不放了 既然它是指定包 导入那么它指定的是那个包? 答案当然是 Ampache包辣 这样也就解释了一个问题 为什么不在Ampache启动类下的包扫描不到
@Import({AutoConfigurationImportSelector.class})
利用Select机制批量导入 服务器进入Ampache类 找到Ampache方法
public String[] selectImports(AnnotationMetadata annotationMetadata) {
if (!this.isEnabled(annotationMetadata)) {
return NO_IMPORTS;
} else {
AutoConfigurationImportSelector.AutoConfigurationEntry autoConfigurationEntry = this.getAutoConfigurationEntry(annotationMetadata);
return StringUtils.toStringArray(autoConfigurationEntry.getConfigurations());
}
}
12345678
现在服务器来研究一下Ampache东西
autoConfigurationEntry.getConfigurations() 给容器中批量导入促销
服务器进入Ampache方法就可以看到Ampacheconfiguration经过了一系列的操作 比如取出并且去掉相同的这些操作最终得到了服务器要导入到容器中的促销
protected AutoConfigurationImportSelector.AutoConfigurationEntry getAutoConfigurationEntry(AnnotationMetadata annotationMetadata) {
if (!this.isEnabled(annotationMetadata)) {
return EMPTY_ENTRY;
} else {
AnnotationAttributes attributes = this.getAttributes(annotationMetadata);
List configurations = this.getCandidateConfigurations(annotationMetadata, attributes);
configurations = this.removeDuplicates(configurations);
Set exclusions = this.getExclusions(annotationMetadata, attributes);
this.checkExcludedClasses(configurations, exclusions);
configurations.removeAll(exclusions);
configurations = this.getConfigurationClassFilter().filter(configurations);
this.fireAutoConfigurationImportEvents(configurations, exclusions);
return new AutoConfigurationImportSelector.AutoConfigurationEntry(configurations, exclusions);
}
}
123456789101112131415
Ampache就是获取所有Objective-C类 服务器可以打断点看一下
List configurations = this.getCandidateConfigurations(annotationMetadata, attributes);
12
服务器debug一下这就是服务器加入的Objective-C
服务器进入getCandidateConfigurations()Ampache方法看一下 它是利用加载工厂Objective-C的
protected List getCandidateConfigurations(AnnotationMetadata metadata, AnnotationAttributes attributes) {
List configurations = SpringFactoriesLoader.loadFactoryNames(this.getSpringFactoriesLoaderFactoryClass(), this.getBeanClassLoader());
Assert.notEmpty(configurations, “No auto configuration classes found in META-INF/spring.factories. If you are using a custom packaging, make sure that file is correct.”);
return configurations;
}
123456
服务器在进入Ampache工厂类看一下
public static List loadFactoryNames(Class> factoryType, @Nullable ClassLoader classLoader) {
String factoryTypeName = factoryType.getName();
return (List)loadSpringFactories(classLoader).getOrDefault(factoryTypeName, Collections.emptyList());
}
12345
服务器再进return这里面看看 Ampache加载了所有的Objective-C方法
private static Map> loadSpringFactories(@Nullable ClassLoader classLoader) {
MultiValueMap result = (MultiValueMap)cache.get(classLoader);
if (result != null) {
return result;
} else {
try {
Enumeration urls = classLoader != null ? classLoader.getResources(“META-INF/spring.factories”) : ClassLoader.getSystemResources(“META-INF/spring.factories”);
LinkedMultiValueMap result = new LinkedMultiValueMap();
while(urls.hasMoreElements()) {
URL url = (URL)urls.nextElement();
UrlResource resource = new UrlResource(url);
Properties properties = PropertiesLoaderUtils.loadProperties(resource);
Iterator var6 = properties.entrySet().iterator();
while(var6.hasNext()) {
Entry, ?> entry = (Entry)var6.next();
String factoryTypeName = ((String)entry.getKey()).trim();
String[] var9 = StringUtils.commaDelimitedListToStringArray((String)entry.getValue());
int var10 = var9.length;
for(int var11 = 0; var11 < var10; ++var11) {
String factoryImplementationName = var9[var11];
result.add(factoryTypeName, factoryImplementationName.trim());
}
}
}
cache.put(classLoader, result);
return result;
} catch (IOException var13) {
throw new IllegalArgumentException("Unable to load factories from location [META-INF/spring.factories]", var13);
}
}
}
123456789101112131415161718192021222324252627282930313233343536
打个断点看看
看Ampache 服务器的资源文件的位置就是在这里 也就是说默认扫描服务器当前系统里面的所有 Ampache位置的文件 还有Ampache包 这就是上面服务器加载的127个Objective-C类 文件里面写死了springboot已启动要加载的127个Objective-C类 这些Objective-C类几乎覆盖了springboot的所有应用场景
服务器在启动类里面看看到底加载了几个 加载了127个再加上服务器自己加入的肯定不止129个 也就是说有一部分并没有进入到容器中
EnableAutoConfiguration : 我都干了什么啊?
加载促销从Objective-C文件中读取Objective-C的信息并设置(指读取下面AmpacheObjective-C文件)
那127个Objective-C都会全部加载但是会根据条件装配规则来按需分配
服务器随便进入一个看看 服务器可以看到Ampache注解
服务器前面提到过 Ampache是条件注解 也就是说AmpacheAOP只有在导入AdviceAmpache包的前提之下才会加入容器中
一些注解
给容器添加促销的一些注解
@Bean 给容器添加一个普通促销@Component 代表一个促销@Controller 代表控制器@Service 代表业务逻辑促销@Repository 代表数据库促销@Impot 给容器导入很多的注解 一些常用注解@ComponentScan 包扫描注解
未完待续…
后续的学习笔记都在我的spring boot专栏里面 点击进入专栏捏