首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用方面劫持来自不同servlet的类的静态方法

使用方面劫持来自不同servlet的类的静态方法
EN

Stack Overflow用户
提问于 2014-08-08 09:25:44
回答 2查看 1.2K关注 0票数 0

背景

我尝试在我的Spring应用程序中使用LightAdmin。我的根本问题是,它只支持一些基本的普通类型--例如,我使用了不支持的java.util.UUID (请看这里)。

由于这些都是静态方法,而且实际上没有任何POI来绑定自己的类型,所以我考虑使用方面进行绑定。

问题所在

但这又引起了另一个问题-我不能让我的方面劫持这个静态电话。LightAdmin作为一个单独的servlet工作,但是在同一个应用程序中,我的方面在applicationContext.xml中加载。

TypeHandler.java (方面代码):

代码语言:javascript
复制
@Aspect
public class TypeHandler
{
    @Before("execution(static * org.lightadmin.core.persistence.metamodel.DomainTypeAttributeType.forType(..))")
    public void myBefore()
    {
        System.out.println("HIJACKED!");
    }
}

applicationContext.xml片段:

代码语言:javascript
复制
<aop:aspectj-autoproxy/>

<context:component-scan
    base-package="my.website.web.backend"
    use-default-filters="true"
>
    <context:include-filter type="annotation" expression="org.aspectj.lang.annotation.Aspect"/>
</context:component-scan>
<context:load-time-weaver/>

我将Tomcat7与org.springframework.instrument.classloading.tomcat.TomcatInstrumentableClassLoader类加载器结合使用。

在日志中我可以看到:

代码语言:javascript
复制
...
11:02:55.198 [localhost-startStop-1] INFO  org.springframework.context.weaving.DefaultContextLoadTimeWeaver - Using a reflective load-time weaver for class loader: org.springframework.instrument.classloading.tomcat.TomcatInstrumentableClassLoader
...
INFO: Initializing Spring FrameworkServlet 'backend'
11:03:11.677 [localhost-startStop-1] INFO  org.springframework.web.servlet.DispatcherServlet - FrameworkServlet 'backend': initialization started
11:03:11.687 [localhost-startStop-1] INFO  org.springframework.web.context.support.XmlWebApplicationContext - Refreshing WebApplicationContext for namespace 'backend-servlet': startup date [Fri Aug 08 11:03:11 CEST 2014]; parent: Root WebApplicationContext
11:03:11.689 [localhost-startStop-1] INFO  org.springframework.beans.factory.xml.XmlBeanDefinitionReader - Loading XML bean definitions from ServletContext resource [/WEB-INF/backend-servlet.xml]
11:03:11.770 [localhost-startStop-1] INFO  org.springframework.web.servlet.DispatcherServlet - FrameworkServlet 'backend': initialization completed in 93 ms
sie 08, 2014 11:03:11 AM org.apache.catalina.core.ApplicationContext log
INFO: Initializing Spring FrameworkServlet 'lightadmin-dispatcher'
11:03:11.772 [localhost-startStop-1] INFO  org.springframework.web.servlet.DispatcherServlet - FrameworkServlet 'lightadmin-dispatcher': initialization started
11:03:11.773 [localhost-startStop-1] INFO  org.springframework.web.context.support.AnnotationConfigWebApplicationContext - Refreshing WebApplicationContext for namespace 'lightadmin-dispatcher-servlet': startup date [Fri Aug 08 11:03:11 CEST 2014]; parent: Root WebApplicationContext
...

(/WEB-INF/backend-servlet.xml为空)

在为我的UUID字段调用org.lightadmin.core.persistence.metamodel.DomainTypeAttributeType.forType()之后,应用程序就会失败,而不会被方面劫持。

我已经尝试过添加@Component注释,在applicationContext.xml中手动注册bean等等-没有效果。我认为方面本身是由beans工厂加载的,因为如果配置无效的切入点,就会有例外。

问题

所以我的两个问题是:

  1. 还有其他方法可以让这个操作(不需要修改LightAdmin源代码,我想不惜一切代价避免这种情况)吗?
  2. 如果没有,那如何使这个方面发挥作用?
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2014-09-04 12:02:47

请使用最新重新设计的LightAdmin版本和一组支持的扩展类型(UUIDJoda-Time)。它被升级到Spring4.0.X,这是最新的Spring数据/REST,并包含了一系列缺陷修复。

顺便说一句,您不再需要在发布时使用LightAdmin Nexus存储库了。它现在可以直接从Maven Central获得。

票数 2
EN

Stack Overflow用户

发布于 2014-08-20 12:16:43

Spring不能用于静态方法。为此,您必须使用完整的AspecJ。您可以在如何在Spring中拦截静态方法?中找到另一个引用。

定义切入点时也没有例外,因为它的语法在AspectJ中是有效的,只是Spring不能处理它。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/25200112

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档