SpringBoot configuration file: SpringBoot configuration file: SpringBoot configuration file

Use the Value(${}) annotation in the Bean annotation

@Data
@Component
public class ApplicationProperty {
    @Value("${application.name}")
	private String name;
}
Copy the code

This mode automatically reads the configuration values in the current configuration file appliation.yml or application.properties

The difference is that when reading YML files, Chinese encoding is supported, while peoperties need transcoding

Use the @configurationProperties (prefix = “developer”) annotation

@Data
@ConfigurationProperties(prefix = "developer")
@Component
public class DeveloperProperty {
	private String name;
	private String website;
	private String qq;
	private String phoneNumber;
}
Copy the code

This method directly prefixes the property of the currently loaded YML configuration file with Developer

Read the developer. The name…

Importing dependencies into POM files

<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-configuration-processor</artifactId>
			<optional>true</optional>
</dependency>
Copy the code

Use of the first two types of read configuration

// How to use it
private final ApplicationProperty applicationProperty;
private final DeveloperProperty developerProperty;

@Autowired
	public PropertyController(ApplicationProperty applicationProperty, DeveloperProperty developerProperty) {
		this.applicationProperty = applicationProperty;
		this.developerProperty = developerProperty;
	}

@GetMapping("/property")
	public Dict index(a) {
        System.out.println("name:"+applicationProperty.getName());
        System.out.println("version:"+applicationProperty.getVersion());
        System.out.println("DevName:"+applicationProperty.getDeveloperName());
	}
Copy the code

4. Use Hutool to read configuration files (yML format is not supported)

1. Read the Props file

static Props props1 = new Props("application.properties",CharsetUtil.CHARSET_UTF_8);
Copy the code

2. Read by Setting

static Setting setting = new Setting("application-dev.yml", CharsetUtil.CHARSET_UTF_8,true);
Copy the code

3. Read the configuration file

public class Constant {
    
    static Props props1 = new Props("application.properties",CharsetUtil.CHARSET_UTF_8);

    static Setting setting = new Setting("application-dev.properties", CharsetUtil.CHARSET_UTF_8,true);

    public static final String Name ;
    public static final String SettingName ;

    static {
        Name = props.getStr("application.name");
        SettingName = setting.getByGroup("name"."application"); }}Copy the code

4. Usage

System.out.println(Constant.DevName+"-- -- -- -- -- -"+Constant.DevWebsite);
Copy the code

It can be used by directly calling the class property with the constant class

5. Read configuration files with @propertysource annotations (all format configuration files are supported)

@Configuration
@Component
@PropertySource(value = {"application.yml"})
@Data
public class ApplicationProperty_PropertySource {
	@Value("${server.port}")
	private String port;

	@Value("${spring.profiles.active}")
	private String active;

}
Copy the code

Use the @propertysource annotation directly to read any configuration file from the classPath or from a non-CLASspath path. The usage is consistent with the above method. Use the built-in dependencies in the Spring framework.