【发布时间】:2020-02-07 15:57:53
【问题描述】:
我上课了:
@WebServiceClient(name = "${service.name}", targetNamespace = "${service.namespace}", wsdlLocation = "${service.wsdlLocation}")
public class ExampleService extends Service {
@Value("${service.wsdlLocation}")
private static String wsdlLocation;
}
它是 mvn 项目的一部分,我从本地 maven repo 编译并使用它作为对我的其他 spring-boot 应用程序的依赖项,该应用程序具有 yml 配置:
service:
name: name
namespace: namespace
wsdlLocation: wsdlLocation
有没有办法让这个 ExampleService 类使用“父”项目的配置?
编辑:
出现了两个答案,但我觉得我没有问清楚。我现在要做的是在我的“父”项目中使用类 ExampleService 并让它看到该父项目的配置。到目前为止,例如我在父项目代码中编写如下:
ExampleService exampleService = new ExampleService();
System.out.println(exampleService.getWsdlLocation());
Null 被打印出来。所以我正在寻找解决方案。
编辑 2: 这是我的课。 父项目:
package com.example.kris.parentservice;
import com.example.kris.childservice.ExampleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("/example")
public class ExampleController {
// @Autowired
private ExampleService exampleService;
@Value(value = "${service.wsdlLocation}")
private String wsdlLocation;
@GetMapping(value = "/example")
public void example() {
exampleService = new ExampleService();
System.out.println("ExampleService field: " + exampleService.getWsdlLocation());
System.out.println("@Value: " + wsdlLocation);
}
}
子项目:
package com.example.kris.childservice;
import org.springframework.beans.factory.annotation.Value;
public class ExampleService {
@Value("${service.wsdlLocation}")
private String wsdlLocation;
public String getWsdlLocation() {
return wsdlLocation;
}
}
运行父控制器后输出:
ExampleService field: null
@Value:test.wsdl.location
【问题讨论】:
-
好像 yml 没有加载,您可以在 application.properties 中添加相同的属性以确保选择该属性。
-
Yaml 已加载,我可以在父项目中创建的类中使用其他值。问题在于项目中的类作为依赖项加载。
-
@KrzysztofTkacz 每当您编辑问题时,请尝试向回答的人发表评论,我没有注意到编辑,编辑我的答案,因为 cmets 中的代码变得混乱
标签: java spring spring-boot configuration yaml