【问题标题】:Load time weaving in grails加载时间在 grails 中编织
【发布时间】:2011-11-06 18:48:14
【问题描述】:

我正在尝试在 Grails 项目中使用加载时间编织,以便能够序列化和反序列化对象并自动注入 spring 依赖项。经过一番搜索,我找到了easy example,这似乎按预期工作。但是在将相同的配置应用到一个简单的 Grails 项目之后,我得到了很多错误。例如:

[TomcatInstrumentableClassLoader@413a2870] error at org/springframework/web/servlet/theme/AbstractThemeResolver.java::0 class 'org.springframework.web.servlet.theme.AbstractThemeResolver' is already woven and has not been built in reweavable mode

为了测试这一点,我创建了一个新的 grails 项目并更改了 applicationContext.xml:

<beans xmlns="http://www.springframework.org/schema/beans"
   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
   xmlns:context="http://www.springframework.org/schema/context"
   xsi:schemaLocation="http://www.springframework.org/schema/beans
   http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
   http://www.springframework.org/schema/context
   http://www.springframework.org/schema/context/spring-context.xsd">

<context:spring-configured />
<context:load-time-weaver aspectj-weaving="autodetect" weaver-class="org.springframework.instrument.classloading.ReflectiveLoadTimeWeaver"/>

在这个文件中我还创建了一个新的 bean:

<bean class="be.testweaving.Person" scope="prototype">
    <property name="name" value="Timon"/>
</bean>

这定义了Person 类的原型,并将值Timon 注入name 属性。

我使用grails war 将其打包为战争,并将其部署在 tomcat 服务器上。这个 tomcat 在他的 lib 目录中有 org.springframework.instrument.tomcat-3.0.5.RELEASE.jar,在部署之后,我看到上面提到的大量错误列表。

有人能够在 Grails 中配置加载时间编织吗?

【问题讨论】:

  • 当我按照上面提到的示例并将其部署在同一个 tomcat 中时,我可以看到示例工作并且编织似乎工作正常。谁能告诉我是否有特定的 grails 阻止编织发生?
  • 你使用的是什么版本的 Grails?
  • 如果还没有解决:听起来像是类加载器问题。尝试将 grails 应用程序的所有依赖项放入您的 war 文件中,并从 tomcats lib 目录中删除所有与 tomcat 无关的 jar。还要注意在任何地方都使用相同版本的 Spring(您的 applicationContext.xml 表示使用 2.5,但提到了 3.0.5-RELEASE 的仪器版本)。
  • 加载时间编织需要 VM 上的 -javaagent 命令行参数。这是你设置的吗?

标签: spring grails aop


【解决方案1】:

你为什么不直接通过元类注入你的属性?

class ExampleBootStrap {
 def init = { servletContext ->
     Person.metaClass.constructor = {  
         def person = BeanUtils.instantiateClass(Person) 
         person.name = "Timon"
         person
     }
 }
}

【讨论】:

    猜你喜欢
    • 2018-06-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-05-10
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多