gpt4 book ai didi

java - 将 Spring AOP 应用于来自不同应用程序上下文的 bean

转载 作者:行者123 更新时间:2023-12-01 13:37:09 24 4
gpt4 key购买 nike

是否可以将 Spring AOP 应用于在与 AOP 配置不同的应用程序上下文中声明的 bean?我有 2 个应用程序上下文:dataApplicationContext.xmlwebApplicationContext.xml。我想在webApplicationContext.xml中声明一个方面来拦截dataApplicationContext.xml

中定义的bean的方法执行

dataApplicationContext.xml 包含在从 main(String args[]) 入口点启动的主 applicationContext.xml 上下文文件中。 webApplicationContext.xml 由部署在 Jetty 嵌入式实例中的 ContextLoaderListener 单独加载。

ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext( new String[] { "applicationContext.xml"} );
ctx.registerShutdownHook();

Server server = ctx.getBean(Server.class);

DispatcherServlet dispatcherServlet = new DispatcherServlet();
dispatcherServlet.setContextConfigLocation("classpath:webApplicationContext.xml");

ServletHolder servletHolder = new ServletHolder(dispatcherServlet);
ServletContextHandler context = new ServletContextHandler();
context.setContextPath("/");
context.addEventListener(new ContextLoaderListener());
context.setInitParameter("contextConfigLocation",
"classpath*:**/webApplicationContext.xml");
context.addServlet(servletHolder, "/*");
context.setSessionHandler(new SessionHandler());

谢谢。

最佳答案

如果你的 Spring 应用程序加载了两个上下文文件,它应该可以开箱即用。如果没有,您有 2 个选择:

  • 创建一个包含两个上下文文件的父上下文文件。
  • 包含一个上下文文件和另一个上下文文件。因此,在本例中,我假设您的 webApplicationContext.xml 将包含 dataApplicationContext.xml。

关于java - 将 Spring AOP 应用于来自不同应用程序上下文的 bean,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21185838/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com