我有一个奇怪的错误。我在我的intelliJIDEA IDE中运行了一个基本的Spring启动Web服务器(在jetty上)。
当我经营一个maven'包裹'命令,而是尝试从命令行运行Web服务器,它给我一个loadTimeWeaver错误。我不明白两个运行环境之间的区别,我觉得它应该可以工作。
在IDEA中,我在运行配置中指定以下内容:
主要类:package.Starter
VM选项:-javaagent:C:/Users/newb/EXT_LIBS/spring-instrument-4.0.3.RELEASE.jar -noverify
在命令行上,我尝试执行以下操作:
java -javaagent:C:/Users/newb/EXT_LIBS/spring-instrument-4.0.3.RELEASE.jar -noverify -jar target \ MySpringServer.war
但是我收到以下错误:
java.lang.reflect.InvocationTargetException
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at org.springframework.boot.loader.MainMethodRunner.run(MainMethodRunner.java:53)
at java.lang.Thread.run(Unknown Source)
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'entityManagerFactory' defined in class path resource [applicationContext.xml]: Invocation of init method failed; nested exception is java.lang.IllegalStateException: Cannot apply class transformer without LoadTimeWeaver specified
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1553)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:539)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:475)
at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:304)
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:228)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:300)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:195)
at org.springframework.context.support.AbstractApplicationContext.getBean(AbstractApplicationContext.java:973)
at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:750)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:482)
at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.refresh(EmbeddedWebApplicationContext.java:120)
at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:648)
at org.springframework.boot.SpringApplication.run(SpringApplication.java:311)
at org.springframework.boot.SpringApplication.run(SpringApplication.java:909)
at org.springframework.boot.SpringApplication.run(SpringApplication.java:898)
at au.net.iinet.operational.Starter.main(Starter.java:48)
... 6 more
Caused by: java.lang.IllegalStateException: Cannot apply class transformer without LoadTimeWeaver specified
at org.springframework.orm.jpa.persistenceunit.SpringPersistenceUnitInfo.addTransformer(SpringPersistenceUnitInfo.java:80)
at org.eclipse.persistence.jpa.PersistenceProvider.createContainerEntityManagerFactory(PersistenceProvider.java:348)
at org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean.createNativeEntityManagerFactory(LocalContainerEntityManagerFactoryBean.java:336)
at org.springframework.orm.jpa.AbstractEntityManagerFactoryBean.afterPropertiesSet(AbstractEntityManagerFactoryBean.java:318)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1612)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1549)
... 21 more
解决此loadTimeWeaver问题的最佳方法是什么?有没有理由它可以在我的IDE中运行,但不是在它之外?我已经走下了试图强行设置loadTimeWeaver的兔子洞,但它并不是微不足道的,当然它在我的IDE中工作的事实意味着我只有一些非常小的东西需要检查。
提前致谢。
修改
我找到了答案!以下问题对我非常有帮助:I use load time weaving in spring. How can i set class loader in jetty?
基本上,我还需要在命令行中指定-Xbootclasspath / a:C:/Users/newb/EXT_LIBS/spring-instrument-4.0.3.RELEASE.jar。现在它并没有给我那些加载时间织入错误!宇!
答案 0 :(得分:4)
这可以解决您的问题。
@Bean
public InstrumentationLoadTimeWeaver loadTimeWeaver() throws Throwable {
InstrumentationLoadTimeWeaver loadTimeWeaver = new InstrumentationLoadTimeWeaver();
return loadTimeWeaver;
}
在xml配置中也可以这样做。
找到一个新的library,它可以解决动态设置spring InstrumentationLoadTimeWeaver以支持方面而无需使用显式java代理启动JVM
<dependency>
<groupId>de.invesdwin</groupId>
<artifactId>invesdwin-instrument</artifactId>
<version>1.0.2</version>
</dependency>
Spring boot config
@SpringBootApplication
/**
* Make @Configurable work via @EnableLoadTimeWeaving.
* If it does not work, alternatively you can try:
* @ImportResource(locations = "classpath:/META-INF/ctx.spring.weaving.xml")
*/
@EnableLoadTimeWeaving
public class MySpringBootApplication {
public static void main(final String[] args) {
DynamicInstrumentationLoader.waitForInitialized(); //dynamically attach java agent to jvm if not already present
DynamicInstrumentationLoader.initLoadTimeWeavingContext(); //weave all classes before they are loaded as beans
SpringApplication.run(MySpringBootApplication.class, args); //start application, load some classes
}
}