背景
我尝试使用LightAdmin在我的 Spring 应用程序中。我遇到的根本问题是,它只支持一些基本的普通类型 - 我使用 java.util.UUID
例如,不支持 ( see here )。
由于这些是静态方法,并且实际上没有任何 POI 来绑定(bind)自己的类型,因此我考虑使用方面。
问题
但这引发了另一个问题 - 我无法让我的方面劫持这个静态调用。 LightAdmin 作为一个单独的 servlet 工作,但在同一个应用程序中,我的方面加载在 applicationContext.xml
中.
TypeHandler.java
(方面代码):
@Aspect
public class TypeHandler
{
@Before("execution(static * org.lightadmin.core.persistence.metamodel.DomainTypeAttributeType.forType(..))")
public void myBefore()
{
System.out.println("HIJACKED!");
}
}
applicationContext.xml
片段:
<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
类加载器。
在日志中我可以看到:
...
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
为空)
调用 org.lightadmin.core.persistence.metamodel.DomainTypeAttributeType.forType()
后应用程序失败我的 UUID 字段不会被我的方面劫持。
我已经尝试添加 @Component
注解,手动注册bean在 applicationContext.xml
等等 - 没有效果。我认为切面本身是由 beans 工厂加载的,因为如果我配置了一个无效的切入点,我就会有一个异常(exception)。
问题
所以我的两个问题是:
- 是否有其他方法可以实现此功能(无需修改 LightAdmin 源代码,我想不惜一切代价避免这种情况)?
- 如果没有,那么如何使这方面发挥作用?
最佳答案
请使用最新重新设计的LightAdmin具有扩展的受支持类型集(UUID、Joda-Time)的版本。它已升级到 Spring 4.0.X,最新的 Spring DATA/REST,并包含大量缺陷修复。
顺便说一句,您不再需要使用 LightAdmin Nexus 存储库进行发布。 现在可以直接从 Maven Central 获取它。
关于java - 使用切面劫持不同 servlet 中类的静态方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25200112/