Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/338.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 我的JAX-WS Web服务中的全局测试变量_Java_Web Services_Jax Ws - Fatal编程技术网

Java 我的JAX-WS Web服务中的全局测试变量

Java 我的JAX-WS Web服务中的全局测试变量,java,web-services,jax-ws,Java,Web Services,Jax Ws,我想知道如何在JAX-WSWeb服务中设置系统属性(在服务启动时),以便在任何类中执行system.getProperty()来获取它 在java进程(用于启动应用程序服务器)中使用命令行选项设置系统属性值。e、 g: java -Dbrandon.f="some string" SomeClass 您可以通过以下方式进行恢复: String value = System.getProperty("brandon.f"); 更新 如果您想加载配置文件并在JAX-WS中使用它,如果您愿意,可

我想知道如何在JAX-WSWeb服务中设置系统属性(在服务启动时),以便在任何类中执行system.getProperty()来获取它

在java进程(用于启动应用程序服务器)中使用命令行选项设置系统属性值。e、 g:

java -Dbrandon.f="some string" SomeClass
您可以通过以下方式进行恢复:

String value = System.getProperty("brandon.f");

更新 如果您想加载配置文件并在JAX-WS中使用它,如果您愿意,可以使用侦听器在应用程序上下文中加载它。e、 g:

@WebListener
public class CacheListener implements ServletContextListener {

    public void contextInitialized(ServletContextEvent sce) {
        // Bean for store the configuration data.
        Map<String, String> map = new HashMap<String, String>();

        // Load file and read it.

        // Store the bean in application context.
        ServletContext context = sce.getServletContext();
        context.setAttribute("myconfig", dummyCache);
    }

    public void contextDestroyed(ServletContextEvent sce) {
        ServletContext context = sce.getServletContext();
        context.removeAttribute("myconfig");
    }

}
@WebListener
公共类CacheListener实现ServletContextListener{
public void contextInitialized(ServletContextEvent sce){
//用于存储配置数据的Bean。
Map Map=newhashmap();
//加载文件并读取它。
//将bean存储在应用程序上下文中。
ServletContext=sce.getServletContext();
setAttribute(“myconfig”,dummyCache);
}
公共无效上下文已销毁(ServletContextEvent sce){
ServletContext=sce.getServletContext();
删除属性(“myconfig”);
}
}
要获取服务实现中的配置数据,请执行以下操作:

@WebService
public class Test {

    @Resource
    private WebServiceContext svcCtx;

    @WebMethod(operationName = "testCode")
    public String testCode(@WebParam(name = "key") String key) {
        MessageContext msgCtx = svcCtx.getMessageContext();
        ServletContext ctx = (ServletContext) 
                msgCtx.get(MessageContext.SERVLET_CONTEXT);
        Map<String, String> map = (Map<String, String>) 
                ctx.getAttribute("myconfig");
        return map.get("key");
    }

}
@WebService
公开课考试{
@资源
私有webservicecontextsvcctx;
@WebMethod(operationName=“testCode”)
公共字符串测试代码(@webgram(name=“key”)字符串键){
MessageContext msgCtx=svcCtx.getMessageContext();
ServletContext ctx=(ServletContext)
msgCtx.get(MessageContext.SERVLET\u CONTEXT);
地图=(地图)
ctx.getAttribute(“myconfig”);
返回map.get(“key”);
}
}

好的,加载整个配置文件怎么样。我应该在哪里做呢?你可以用一个听者。