繁体   English   中英

在没有web.xml的web应用程序中使用CayenneRuntime

[英]using CayenneRuntime in webapplication without web.xml

我有一个Wicket应用程序,我正在尝试实现可以​​远程更改的单独配置。 无论如何,那是最终目标。

我想要做的是通过手动启动Cayenne使其工作,而不是使用web.xml文件。 我尝试了很多不同的方法,但是我不确定我是否完全理解上下文如何应用于所有线程。

我尝试在我的Application类中创建ServerRuntime。 我还尝试了每个页面使用的自定义BasePage类。 我可以通过在BasePage上执行以下操作来完成某种工作,但这是不一致的:

public class BasePage ....
    public static ServerRuntime runtime = new ServerRuntime("cayenne-config.xml");//This is in my BasePage class, but I've also tried this in the Application class

    @Override
    protected void init() { 
BaseContext.bindThreadObjectContext(Application.runtime.getContext());//This is in my BasePage class
    }

就像我说的那样,但并不一致。 我不断出错

BaseContext.getThreadObjectContext();

错误是这样的:

java.lang.IllegalStateException: Current thread has no bound ObjectContext.

我似乎找不到太多的信息。 我尝试做类似的事情,并使用它们也访问运行时,但是没有任何东西能够始终如一地工作。

WebUtil.setCayenneRuntime(this.getServletContext(), runtime);
BaseContext.bindThreadObjectContext(WebUtil.getCayenneRuntime(((Application)getApplication()).getServletContext()).getContext());

任何帮助将不胜感激。

我想出了一种自行完成此操作的方法。

我正在扩展CayenneFilter并覆盖init方法。

在这里,我几乎复制了他们的确切代码。 我将能够在此处检查任何配置并加载正确的xml文件。 这显然不是解决方案,但绝对是向前迈出的一步,可能是我最终做到这一点的方式。

无论哪种方式,这都是我已经测试过的工作方式。

@WebFilter(filterName = "cayenne-config", displayName = "cayenne-config", urlPatterns = {"/*"})
public class TestFilter extends CayenneFilter
{
    @Override
    public void init(FilterConfig config) throws ServletException
    {
        this.checkAlreadyConfigured(config.getServletContext());
        this.servletContext = config.getServletContext();
        WebConfiguration configAdapter = new WebConfiguration(config);
        Collection modules = configAdapter.createModules(new Module[]{new WebModule()});
        ServerRuntime runtime = new ServerRuntime("cayenne-test.xml", (Module[])modules.toArray(new Module[modules.size()]));
        WebUtil.setCayenneRuntime(config.getServletContext(), runtime);
    }
}

我认为不需要注释(我在web.xml文件中都指定了注释),但是我想我将其保留在此处,以便您可以看到它正在更改。

如果我能找到一种方法来更改config(FilterConfig)值(init参数),那么我可以将其更改为我要使用的xml文件的名称,而不必覆盖整个方法。 我不知道该怎么做,但是稍后再看。

如果还有其他更好的答案,我很想听听。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM