application.yml
mysql:
driver: com.mysql.jdbc.Driver
username: 4444
password: 3333
url: jdbc:mysql://localhost:3306/test
server:
port: 8989
//属性类
@Data
@AllArgsConstructor
@NoArgsConstructor
@ConfigurationProperties(prefix = "mysql") //读取application.yml的属性,并且为当前属性类添加前缀
public class MySqlProperties {
//属性
private String driver;
private String url;
private String username;
private String password;
}
//配置类
@Configuration
@EnableConfigurationProperties(MySqlProperties.class)//获取配置文件类
public class MySqlConfigration {
//依赖注入
@Autowired
private MySqlProperties mySqlProperties;
@Bean
//把DruidSource交给工厂管理
public DruidDataSource druidDataSource(){
DruidDataSource dataSource = new DruidDataSource();
dataSource.setUsername(mySqlProperties.getUsername());
dataSource.setPassword(mySqlProperties.getPassword());
dataSource.setDriverClassName(mySqlProperties.getDriver());
dataSource.setUrl(mySqlProperties.getUrl());
return dataSource;
}
org.springframework.boot
spring-boot-configuration-processor
true
@Import 也可以将组件交由工厂管理 注意:使用@Import注解交由工厂管理的对象名称为全限定名.
第一种使用方式 @Import(value = {Black.class,Pink.class})
第二种使用方式 实现ImportSelector接口和其中方法
springboot用到了第二种方式
实现ImportSelector接口和其中方法
@Override
public String[] selectImports(AnnotationMetadata importingClassMetadata) {
// 逻辑代码
String[] strs={"com.baizhi.conf.Blue","com.baizhi.conf.Red"};
return strs;
}
@EnableAutoConfiguration=@AutoConfigurationPackage+
@Import(EnableAutoConfigurationImportSelector.class) 注解 @AutoConfigurationPackage
//作用就是帮你找到入口类
@Import(EnableAutoConfigurationImportSelector.class)
public @interface EnableAutoConfiguration {
}
@Import(EnableAutoConfigurationImportSelector.class)
//作用就是往容器种添加组件,添加一个全限定名数组.
//EnableAutoConfigurationImportSelector的父类中
@Override
public String[] selectImports(AnnotationMetadata annotationMetadata) {
if (!isEnabled(annotationMetadata)) {
return NO_IMPORTS;
}
try {
AutoConfigurationMetadata autoConfigurationMetadata = AutoConfigurationMetadataLoader
.loadMetadata(this.beanClassLoader);
AnnotationAttributes attributes = getAttributes(annotationMetadata);
List configurations = getCandidateConfigurations(annotationMetadata,
attributes);//获取那些组建的全限类名,进行创建
configurations = removeDuplicates(configurations);
configurations = sort(configurations, autoConfigurationMetadata);
Set exclusions = getExclusions(annotationMetadata, attributes);
checkExcludedClasses(configurations, exclusions);
configurations.removeAll(exclusions);
configurations = filter(configurations, autoConfigurationMetadata);
fireAutoConfigurationImportEvents(configurations, exclusions);
return configurations.toArray(new String[configurations.size()]);
}
catch (IOException ex) {
throw new IllegalStateException(ex);
}
}
public static List loadFactoryNames(Class> factoryClass, ClassLoader classLoader) {
String factoryClassName = factoryClass.getName();
try {
Enumeration urls = (classLoader != null ? classLoader.getResources(FACTORIES_RESOURCE_LOCATION) :
//从类路径下获取所有的类对象"META-INF/spring.factories"
ClassLoader.getSystemResources(FACTORIES_RESOURCE_LOCATION));
List result = new ArrayList();
while (urls.hasMoreElements()) {
URL url = urls.nextElement();
Properties properties = PropertiesLoaderUtils.loadProperties(new UrlResource(url));
String factoryClassNames = properties.getProperty(factoryClassName);
result.addAll(Arrays.asList(StringUtils.commaDelimitedListToStringArray(factoryClassNames)));
}
return result;
}
protected Class> getSpringFactoriesLoaderFactoryClass() {
return EnableAutoConfiguration.class;
}
每个xxxConfiguration自动配置类在该类中已经预先配置了相关的一些组件,比如视图解析器,但是默认的视图解析器 没有属性值,如果想设置属性值则需通过配置文件进行属性注入方可完成自动配置,这就回到了springboot的属性注入。
而属性配置类一般一properties结尾,每一个自动配置类都会有一个对应的properties。