Java Spring@Value不加载属性
我有两个类,它们都使用配置属性 这个装Java Spring@Value不加载属性,java,spring,properties,configuration,Java,Spring,Properties,Configuration,我有两个类,它们都使用配置属性 这个装 package com.cegeka.campus; @RestController @RequestMapping("api/outlook") public class OutlookController { @Value("${frontend.url}") private String frontendUrl; ..... } 但是这个返回空值: package com.cegeka.campus; public cl
package com.cegeka.campus;
@RestController
@RequestMapping("api/outlook")
public class OutlookController
{
@Value("${frontend.url}")
private String frontendUrl;
.....
}
但是这个返回空值:
package com.cegeka.campus;
public class AuthHelper
{
@Value("${backend.authorize.url}")
private static String redirectUrl;
...
}
我们的主要课程如下所示:
@SpringBootApplication
@ComponentScan("com.cegeka.campus")
@EnableJpaRepositories("com.cegeka.campus")
@EntityScan("com.cegeka.campus")
public class CampusApplication extends SpringBootServletInitializer
{
public static void main(String[] args)
{
SpringApplication.run(CampusApplication.class, args);
}
}
frontend:
url: http://hi02549:8080/courseka
backend:
authorize:
url: http://hi02549:8080/campus/api/outlook/authorize
为什么我不能加载backend.authorize.url?@Value根本不注入
属性文件(application.yml)如下所示:
@SpringBootApplication
@ComponentScan("com.cegeka.campus")
@EnableJpaRepositories("com.cegeka.campus")
@EntityScan("com.cegeka.campus")
public class CampusApplication extends SpringBootServletInitializer
{
public static void main(String[] args)
{
SpringApplication.run(CampusApplication.class, args);
}
}
frontend:
url: http://hi02549:8080/courseka
backend:
authorize:
url: http://hi02549:8080/campus/api/outlook/authorize
添加此方法并重试:
@Bean
public static PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer() {
return new PropertySourcesPlaceholderConfigurer();
}
我可以在类AuthHelper中看到两个问题:
@组件注释
重定向URL
是静态的,无法注入。解决方案是使字段非静态或私有静态字符串重定向URL;
@值(${backend.authorize.url}”)
公共void setRedirectUrl(字符串url){
重定向url=url;
}
先生,你是个英雄。