Four kinds of notes
Look at profiles application.properties
user.username=CCCCXXX
user.password=123123123
user.age=18
user.salary=2000.00
1. @Value read
@Service
public class UserServiceImpl{
@Value("${user.username}")
private String username;
@Value("${user.password}")
private String password;
@Value("${user.age}")
private int age;
@Value("${user.saary}")
private double salary;
// 省略...
}
2. @PropertySource read
@Service
@PropertySource(value="application.properties")
public class UserServiceImpl{
@Value("${user.username}")
private String username;
@Value("${user.password}")
private String password;
@Value("${user.age}")
private int age;
@Value("${user.saary}")
private double salary;
// 省略...
}
Used in conjunction with @Value
! ! ! note! ! !@PropertySourceReading does not supportYMLConfiguration File
3. @Environment read
All configuration profiles which can be used to obtain the Environment
@Service
public class UserServiceImpl{
@Autowired
private Environment env;
private void getUsername(){
String username = env.getProperty("user.username");
}
// 省略...
}
4. @ConfigurationProperties read
** This comment is even more Cock ... ** can be used in conjunction with the other notes
@Service
@ConfigurationProperties(prefix="user")
public class UserServiceImpl{
private String username;
private String password;
private int age;
private double salary;
// 省略...
}
5. @ConfigurationProperties + @PropertySource used with annotations
If I create a new folder in the resources folder config files
you create a profile two.properties contents remain unchanged, so I can use
@Service
@PropertySource(value="config/two.properties")
@ConfigurationProperties(prefix="user")
public class UserServiceImpl{
private String username;
private String password;
private int age;
private double salary;
// 省略...
}
To sum up four kinds of notes can read from the configuration file, custom parameters can be used with each other