【发布时间】:2022-01-09 18:08:17
【问题描述】:
我在阅读配置时遇到了一个奇怪的问题,我见过的解决方案似乎都不起作用。这是我的代码:
@SpringBootApplication
@EnableConfigurationProperties
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
这是我的属性类
@Component
@ConfigurationProperties(prefix = "my")
@Data
@ToString
@NoArgsConstructor
@AllArgsConstructor
public class MyProperties {
private String host;
private int port;
}
然后我使用@Autowired 在我的班级中使用 MyProperties 类:
@Autowired
private MyProperties props;
但是,我的 props 对象为 null。
奇怪的是,这完美地通过了测试:
@SpringBootTest
class ApplicationTests {
@Autowired
private MyProperties props;
@Test
void test_configuration() {
Assertions.assertEquals(props.getHost(), "xx.xx.xx.xx");//pass!
Assertions.assertEquals(props.getPort(), xxxxxx);//pass!
}
}
它完全拒绝工作,@Value 注入也是如此。我可能会错过什么?
编辑
这是我如何在 MyProperties 上使用 @Autowired 的完整代码(我已包含 @Value,但它也不起作用)
@Slf4j
@Component //also tried @Configurable, @Service
public class MyService {
@Autowired
private MyProperties props;
@Value("localhost")
public String host;
public void post() {
log.info(host + props);// =null and null
}
}
EDIT2
但是,我注意到在控制器上,它工作得非常好:
@Slf4j
@RestController
@Service
public class Main {
@Autowired
private MyProperties props;
@Value("localhost")
private String host;
@GetMapping("/post")
public void post() {
log.info(host + props);//=it's perfect!
new MyService().post();// calling MyService - where @Autowired or @Value is failing
}
}
【问题讨论】:
-
你能告诉我们更多关于你在哪里自动装配道具的代码吗?只是为了确保这不是 stackoverflow.com/questions/19896870/… 的重复。
-
@g00glen00b 添加了一些代码;尝试了您共享链接中答案的建议,问题仍然存在。但是,在 RestController 中,它工作得很好。 (并在测试中)
-
你如何与
MyService互动?您能否也显示该代码(您调用myService.post())。 -
@g00glen00b 我在 RestController 示例代码中展示过
标签: spring-boot spring-boot-configuration