简体   繁体   English

spring项目中使用AOP注解@Flip时FF4J不翻转

[英]FF4J does not flip when using AOP annotation @Flip in spring project

I've injected ff4j following example .我在下面的例子中注入了ff4j Ff4jConfiguration.class : Ff4jConfiguration.class

@Bean
@ConditionalOnMissingBean
public FF4j getFF4j() {
    return new FF4j("ff4j.xml");
}

and application loader was also changed:应用程序加载器也发生了变化:

@Import( {..., Ff4jConfiguration.class})
@AutoConfigureAfter(Ff4jConfiguration.class)

my ff4j.xml :我的ff4j.xml

<?xml version="1.0" encoding="UTF-8" ?>
<ff4j xmlns="http://www.ff4j.org/schema/ff4j"
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xsi:schemaLocation="http://www.ff4j.org/schema/ff4j http://ff4j.org/schema/ff4j-1.4.0.xsd">
    <features>
        <feature uid="occurrence-logging" enable="false"/>
        <feature uid="check-no-logging" enable="false"/>
        <feature uid="check-logging" enable="true"/>
    </features>
</ff4j>

My bean to verify ff4j我的bean验证ff4j

@Component
public class JustToCheck {
    @Autowired
    private FF4j ff4j;

    @Flip(name="occurrence-logging")
    public void log() {
        System.out.println("hello");
    }

    @Flip(name="check-no-logging")
    public void log2() {
        System.out.println("hello2");
    }

    @Flip(name="check-logging")
    public void log3() {
        System.out.println("hello3");
    }
}

In runtime I see ff4j bean injected correctly with correspond properties:在运行时,我看到ff4j bean正确注入了相应的属性:

 ff4j.check("check-no-logging")
 > result=false

 ff4j.check("check-logging")
 > result=true

I expect method log2 will be never called, but it is (All used methods were called, none ignored).我希望方法log2永远不会被调用,但它是(所有使用的方法都被调用,没有被忽略)。 Can someone help me what I've done wrong here please?有人可以帮助我在这里做错什么吗?

The annotation Flip is meant to be positionned on an Interface and not on bean.注释Flip旨在定位在接口上而不是 bean 上。 The reason is to enforce people to create different implementations for the same method when using AOP.原因是强制人们在使用 AOP 时为相同的方法创建不同的实现。 (simpler when cleaning is required later). (稍后需要清洁时更简单)。

I can propose 2 solutions.我可以提出 2 个解决方案。 The first seems obvious but if you don't have multiple implementations...第一个看起来很明显,但如果你没有多个实现......

@Component
public class JustToCheck {

    @Autowired
    private FF4j ff4j;
    
    public void log2() {
        if (ff4j.check("check-no-logging")) {
            System.out.println("hello2"); 
        } else {
            System.out.println("As check-no-logging is disable... do nothin");
        }
    }
}

The second is to indeed use AOP and you have to :第二个是确实使用 AOP,你必须:

  1. Add the Autoproxy located in package org.ff4j.aop in your Spring Context.在 Spring Context 中添加位于包org.ff4j.aop中的org.ff4j.aop But it's done already by adding autoconfigure dependency.但它已经通过添加自动配置依赖来完成。

  2. Put @Flip annotation on a interface and create different implementations:@Flip注释放在接口上并创建不同的实现:

Here is a code sample:这是一个代码示例:

@Component
public interface JustToCheck {
    @Flip(name="check-no-logging", alterBean="just-to-check")
    void log2();
}

@Component("just-to-check")
public class JustToCheckImpl implements JustToCheck {
   public void log2() {
       System.out.println("hello2");
    }
}

@Component("just-to-check-mock")
public class JustToCheckMock implements JustToCheck {

  public void log2() {
    System.out.println("As check-no-logging is disable... do nothing");
  }
}

Bug has been reproduced and the 2 working solutions are available here : https://github.com/clun/ff4j-samples/tree/master/ff4j-sample-sergii错误已重现,2 个工作解决方案可在此处获得: https : //github.com/clun/ff4j-samples/tree/master/ff4j-sample-sergii

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM