目录:
1、
2、
3、demo
项目结构
pom.xml
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.1.2.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> <groupId>com.oy</groupId> <artifactId>SpringBoot10-autoconfig</artifactId> <version>0.0.1-SNAPSHOT</version> <name>SpringBoot10-autoconfig</name> <description>Demo project for Spring Boot</description> <properties> <java.version>1.8</java.version> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-configuration-processor</artifactId> <optional>true</optional> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>
启动类 Application
package com.oy; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication public class Application { public static void main(String[] args) { SpringApplication.run(Application.class, args); } }
A 类
package com.oy.config; /** * @author oy * @version 1.0 * @date 2019年1月19日 * @time 上午7:33:32 */ public class A { }
AProperties
package com.oy.config; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.boot.context.properties.ConfigurationProperties; /** * @author oy * @version 1.0 * @date 2019年1月19日 * @time 上午7:34:35 */ @ConfigurationProperties(prefix = "a") public class AProperties { private final Logger logger = LoggerFactory.getLogger(getClass()); private String username; private String password; public AProperties() { logger.info("实例化AProperties============="); } public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } }
MyAutoConfiguration
package com.oy.config; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.boot.autoconfigure.condition.ConditionalOnClass; import org.springframework.boot.context.properties.EnableConfigurationProperties; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.PropertySource; import com.oy.entity.User; /** * @author oy * @version 1.0 * @date 2019年1月19日 * @time 上午7:32:44 */ @PropertySource({"classpath:a.properties"}) @Configuration //在IoC容器中添加一个AProperties类型的Bean,该Bean绑定了属性文件中的属性 @EnableConfigurationProperties({AProperties.class}) // 当类路径下有A这个类,本自动配置类才会生效 @ConditionalOnClass(A.class) public class MyAutoConfiguration { private final Logger logger = LoggerFactory.getLogger(getClass()); private AProperties properties; // 如果本自动配置类生效,将自动调用该有参构造进行实例化,AProperties由容器注入 public MyAutoConfiguration(AProperties properties) { logger.info("配置类MyAutoConfiguration实例化========start========="); logger.info("properties:" + properties.getUsername() + "---" + properties.getPassword()); logger.info("配置类MyAutoConfiguration实例化======== end ========"); this.properties = properties; } @Bean User user() { logger.info("Bean user实例化=================="); User user = new User(); user.setUsername(properties.getUsername()); user.setPassword(properties.getPassword());
logger.info(user.toString()); return user; } }
a.properties
a.username=zhangsan
a.password=abc
application.properties
a.username=zhangsan111
a.password=abc111
a.properties中的配置为默认配置,application.properties为个性化配置。个性化配置会覆盖默认配置。
启动打印日志:
---
原文:https://www.cnblogs.com/xy-ouyang/p/14013530.html