@PropertySource & @Value 配合使用配置文件步骤及注意事项

@PropertySource & @Value 配合使用配置文件

步骤:

  1. 需要使用@Value注解注入参数的类要加上@Component注解
  2. 需要使用@Value注解注入参数的类要加上@PropertySource
@PropertySource("classpath:config.properties")
/*
1. properties文件在resources下可以只写相对路径
2. 在外部需要写全路径
*/
  1. 注入值时使用@Value
@Value("${xxxx}")    

注意:

取不到值的可能原因:

  • 使用了new创建对象,需要用@Autowired装配
  • 对象不能使用static修饰

猜你喜欢

转载自blog.csdn.net/a928108554/article/details/126713249