springboot中这种类注解标签里的字段wsdlLocation如何改成变量呀,我想测试环境跟生产环境用不同的地址?

@WebServiceClient(name = "PublishService", targetNamespace = "http://tempuri.org/", wsdlLocation = "https://aquavn-tt78admindemo.vnpt-invoice.com.vn/PublishService.asmx?wsdl")

public class PublishService

extends Service

{


回答:

首先写一个能读取配置文件的类加上@Component作为一个bean,方便我们拿配置文件的值

import org.springframework.beans.factory.annotation.Value;

import org.springframework.stereotype.Component;

/**

* @author Undest

* @date 2023-04-18 15:03

* 容器配置类 用于获得配置文件中的变量值

*/

@Component("contextConfig")

public class ContextConfig {

@Value("${wsdlLocation}")

public String wsdlLocation;

}

然后在你的配置文件里写上:
yml:

green_enterprise_server_ws_url: https://aquavn-tt78admindemo.vnpt-invoice.com.vn/PublishService.asmx?wsdl

或者properties:

green_enterprise_server_ws_url=https://aquavn-tt78admindemo.vnpt-invoice.com.vn/PublishService.asmx?wsdl

这样你就会发现,在需要用到这个配置变量的地方注入这个配置类的对象就可以使用了:

@Resource(name = "contextConfig")

ContextConfig contextConfig;

或者在一个非Spring管理的类里:

ContextConfig contextConfig = SpringUtils.getBean("contextConfig");//这里的工具类参照我之前的回答提供的

然后请你查看这篇文章:
[https://blog.csdn.net/qq_35443054/article/details/109155970]
核心的代码:

URL wsdlLocation = new URL("http://example.org/my.wsdl");

QName serviceName = new QName("http://example.org/sample", "MyService");

Service service = Service.create(wsdlLocation, serviceName);

就是把通过写注解的方式改成用代码显式传入参数的方式来创建。
我们只需要把负责配置的bean里public的变量这样使用就可以了:

URL wsdlLocation = new URL(contextConfig.wsdlLocation);


回答:

试试 wsdlLocation = "${配置项}"


回答:

你这个应该WS的注解吧,所以没有办法使用类似Spring的@Value类似的动态属性注入。
解决的思路大致两个方向吧

  1. 放弃注解,改成代码创建,然后借助@Value一类的方式做属性注入,需要对现有代码改造。
  2. 借助maven编译一类的方式修改配置的目标文件,比如location指向resource/a.wsdl,在编译的时候使用另一个文件,重命名为a.wsdl,然后放到resource下面,类似物理欺骗。需要配置maven的resource插件,进行文件复制

另外看你的代码,使用的是一个网络地址,而且是为了区分测试和生产环境,所以更简单一点,可以考虑不修改代码,而是使用host配置,nginx配置一类的反向代理,实现虽然代码内部请求的是a地址,但是实际指向的是b,网络层面的一种物理欺骗

以上是 springboot中这种类注解标签里的字段wsdlLocation如何改成变量呀,我想测试环境跟生产环境用不同的地址? 的全部内容, 来源链接: utcz.com/p/945141.html

回到顶部