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

时间:2013-10-26 18:19:19

标签: java web-services jax-ws

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

1 个答案:

答案 0 :(得分:0)

在java进程中使用命令行选项-Dproperty=value(用于启动应用程序服务器)以设置系统属性值。 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");
    }

}

获取服务实现中的配置数据:

@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");
    }

}
相关问题