@PropertySourece()

my.properties

1
name=kil

UserService.java

添加@PropertySource(“my.properties”)指定配置文件 引用@Value(“${name}”)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
package cn.kil.springboot_parameter.service;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

/**
* @program: springboot_parameter
* @ClassName: UserService
* @description:
* @author: toy
* @create: 2022-07-20 19:51
*/
@Service
@PropertySource("my.properties")
public class UserService {

@Value("${name}")
private String name;

public void test(){
System.out.println(name);
}
}

SpringbootParameterApplication.java #Spring启动类

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
package cn.kil.springboot_parameter;

import cn.kil.springboot_parameter.service.UserService;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.PropertySource;

@SpringBootApplication
public class SpringbootParameterApplication {

public static void main(String[] args) {
ConfigurableApplicationContext applicationContext = SpringApplication.run(SpringbootParameterApplication.class, args);
UserService bean = applicationContext.getBean(UserService.class);
bean.test();
}
}

@ConfigurationProperties

@ConfigurationProperties 让开发者将整个配置文件,自动映射到对象中,比@Value 效率更高

需要添加@Component注册bean

属性:

  • prefix:可以配置文件属性前缀

MyProperties.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
package cn.kil.springboot_parameter.properties;

import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

/**
* @program: springboot_parameter
* @ClassName: MyProperties
* @description: 我的配置
* @author: toy
* @create: 2022-07-20 19:50
*/
@Component
@ConfigurationProperties(prefix = "kil")
public class MyProperties {

private String name;

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}
}

UserService.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
package cn.kil.springboot_parameter.service;

import cn.kil.springboot_parameter.properties.MyProperties;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
* @program: springboot_parameter
* @ClassName: UserService
* @description:
* @author: toy
* @create: 2022-07-20 19:51
*/
@Service
public class UserService {

@Autowired
private MyProperties myProperties;

public void test(){
System.out.println(myProperties.getName());
}
}

application.properties

1
kil.name=kil
  • 标准用法 配置类**@EnableConfigurationProperties(MyProperties.class)代替@Component**。方便添加条件,可设置多个配置文件

AppConfig.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
import cn.kil.springboot_parameter.properties.MyProperties;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Configuration;

/**
* @program: springboot_parameter
* @ClassName: AppConfig
* @description:
* @author: toy
* @create: 2022-07-20 23:56
*/
@Configuration
@EnableConfigurationProperties(MyProperties.class)
public class AppConfig {

}
  • @ConfigurationPropertiesScan(“cn.kil.springboot_parameter.properties”) //扫描包下的配置文件

AppConfig.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
package cn.kil.springboot_parameter;

import org.springframework.boot.context.properties.ConfigurationPropertiesScan;
import org.springframework.context.annotation.Configuration;

/**
* @program: springboot_parameter
* @ClassName: AppConfig
* @description:
* @author: toy
* @create: 2022-07-20 23:56
*/
@Configuration
@ConfigurationPropertiesScan("cn.kil.springboot_parameter.properties")
public class AppConfig {

}

文件目录信息