首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >在纯Java应用程序中生成NoSuchMethodError的HystrixCommandAspect

在纯Java应用程序中生成NoSuchMethodError的HystrixCommandAspect
EN

Stack Overflow用户
提问于 2018-06-23 03:00:13
回答 1查看 1.1K关注 0票数 -1

我正在尝试在我的Java应用程序中使用Hystrix,它是一个非 spring java应用程序。

在POM中使用以下Maven依赖项来启用Hystrix命令:

代码语言:javascript
复制
  <dependency>
      <groupId>com.netflix.hystrix</groupId>
      <artifactId>hystrix-javanica</artifactId>
      <version>1.5.8</version>
    </dependency>

    <dependency>
      <groupId>com.netflix.hystrix</groupId>
      <artifactId>hystrix-core</artifactId>
      <version>1.5.12</version>
    </dependency>

    <dependency>
      <groupId>com.netflix.rxjava</groupId>
      <artifactId>rxjava-core</artifactId>
      <version>0.20.7</version>
    </dependency>

使用以下依赖项启用AspectJ:

代码语言:javascript
复制
<dependency>
  <groupId>org.aspectj</groupId>
  <artifactId>aspectjrt</artifactId>
  <version>1.8.7</version>
</dependency>
<dependency>
  <groupId>org.aspectj</groupId>
  <artifactId>aspectjweaver</artifactId>
  <version>1.8.7</version>
</dependency>

在META-INF中创建了一个aop.xml,配置如下:

代码语言:javascript
复制
<aspectj>

  <aspects>
    <aspect name="com.netflix.hystrix.contrib.javanica.aop.aspectj.HystrixCommandAspect" />
  </aspects>

  <weaver options="-verbose">
    <include within="*" />
  </weaver>
</aspectj>

在我的服务类中使用了Hystrix命令:

代码语言:javascript
复制
import org.apache.felix.scr.annotations.Component;
import org.apache.felix.scr.annotations.Service;

@Component
@Service
public class TestHystrix 

    @HystrixCommand(commandKey = "testHystrix", threadPoolKey = "testHystrix", commandProperties = {
            @HystrixProperty(name = "hystrix.command.testHystrix.execution.isolation.thread.timeoutInMilliseconds", value = "30") }, threadPoolProperties = {
                    @HystrixProperty(name = "hystrix.threadpool.testHystrix.maximumSize", value = "3") })
public void  testHystrix() {

添加了以下JVM参数:

代码语言:javascript
复制
-DWeavingMode=compile

但在Junit测试和应用程序运行时,它都会导致以下错误:

代码语言:javascript
复制
java.lang.NoSuchMethodError: com.netflix.hystrix.contrib.javanica.aop.aspectj.HystrixCommandAspect.aspectOf()Lcom/netflix/hystrix/contrib/javanica/aop/aspectj/HystrixCommandAspect;

请帮帮忙。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-06-27 01:53:26

我能够通过使用下面的AspectJ插件配置和上面的maven依赖来解决这个问题:

代码语言:javascript
复制
 <plugin>
        <groupId>org.codehaus.mojo</groupId>
        <artifactId>aspectj-maven-plugin</artifactId>
        <version>1.7</version>
        <configuration>
          <complianceLevel>1.8</complianceLevel>
          <source>1.8</source>
          <target>1.8</target>
         <!--  <showWeaveInfo>true</showWeaveInfo>
          <verbose>true</verbose>-->
          <Xlint>ignore</Xlint> 
          <encoding>UTF-8 </encoding>
          <!-- Provide the Source information.  -->          
          <!-- <aspectLibraries>
            <aspectLibrary>
              <groupId>com.netflix.hystrix</groupId>
              <artifactId>hystrix-javanica</artifactId>
            </aspectLibrary>
          </aspectLibraries> -->
          <!--Weaving already compiled JAR artifacts  -->
          <weaveDependencies>
            <weaveDependency>
              <groupId>com.netflix.hystrix</groupId>
              <artifactId>hystrix-javanica</artifactId>
            </weaveDependency>
          </weaveDependencies>
        </configuration>
        <executions>
          <execution>
            <goals>
              <!-- use this goal to weave all your main classes -->
              <goal>compile</goal>
              <!-- use this goal to weave all your test classes -->
              <goal>test-compile</goal>
            </goals>
          </execution>
        </executions>
      </plugin>

此插件将启用编译后编织,有关更多详细信息,请参阅一篇非常好的文章@ http://www.baeldung.com/aspectj https://www.mojohaus.org/aspectj-maven-plugin/examples/weaveJars.html与此插件,aop.xml和-DWeavingMode=compile也不是必需的

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

https://stackoverflow.com/questions/50994164

复制
相关文章

相似问题

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