我目前正在开展一个maven项目,但每当我尝试部署我的战争时,我都会得到以下例外:
ERROR [DispatcherPortlet:276] Context initialization failed org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.springframework.context.weaving.AspectJWeavingEnabler#0': Initialization of bean failed; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'loadTimeWeaver': Initialization of bean failed; nested exception is java.lang.IllegalStateException: ClassLoader [org.apache.catalina.loader.WebappClassLoader] does NOT provide an 'addTransformer(ClassFileTransforme r)' method. Specify a custom LoadTimeWeaver or start your Java virtual machine with Spring's agent: -javaagent:org.springframework.instrument.jar
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:527)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:456)
at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:294)
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:225)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:291)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:197)
at org.springframework.context.support.AbstractApplicationContext.getBean(AbstractApplicationContext.java:1097)
at org.springframework.context.support.AbstractApplicationContext.invokeBeanFactoryPostProcessors(AbstractApplicationContext.java:661)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:446)
at org.springframework.web.portlet.FrameworkPortlet.createPortletApplicationContext(FrameworkPortlet.java:356)
at org.springframework.web.portlet.FrameworkPortlet.initPortletApplicationContext(FrameworkPortlet.java:294)
at org.springframework.web.portlet.FrameworkPortlet.initPortletBean(FrameworkPortlet.java:268)
at org.springframework.web.portlet.GenericPortletBean.init(GenericPortletBean.java:120)
我根本不明白我什么时候有适当的原因为什么会出现这个错误。请在POM.xml中查看我的声明
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.4</version>
<configuration>
<forkMode>once</forkMode>
<argLine>
-javaagent:"path\spring-instrument-3.1.0.RELEASE.jar"
</argLine>
<useSystemClassloader>true</useSystemClassloader>
</configuration>
</plugin>
请指导。
答案 0 :(得分:2)
如果您正在使用tomcat bundle并在Tomcat中启用load-time-weaving
,请将Spring Instrument Tomcat添加到Tomcat的lib
目录以及context.xml
内META-INF
内的<Context path="/test">
<Loader loaderClass="org.springframework.instrument.classloading.tomcat.TomcatInstrumentableClassLoader"/>
</Context>
tomcat的目录:
Filter filter = new Filter(getContext()) {
@Override
protected int beforeHandle(Request request, Response response) {
try {
JsonRepresentation jrep = new JsonRepresentation(request.getEntity());
JSONObject jo = jrep.getJsonObject();
String token = jo.getString("token");
System.out.println(token);
request.setEntity(new JsonRepresentation(jo));
} catch (JSONException | IOException e) {
throw new ResourceException(Status.SERVER_ERROR_INTERNAL);
}
return super.beforeHandle(request, response);
}
};
请参阅tomcat配置中的Spring Docs。
注意:tomcat的配置因版本而异。
答案 1 :(得分:0)
虽然长时间回答这个问题,但我仍在回答这个问题。其他人可能会发现它很有用。 如果您正在使用Tomcat,请尝试在catalina.bash中为windows或catalina.sh添加以下内容以用于Linux服务器:
set CATALINA_OPTS=%CATALINA_OPTS% -javaagent:"C:\path\to\spring-instrument-3.1.1.RELEASE.jar"
请记住,它仅适用于低于7.0.55的Tomcat版本。之后它不会接受它。你需要在context.xml中添加它,如下所述load-time-weaver(aspectj-weaver)在apache tomcat 7.0.55中不起作用。