我在示例之后注射了示例。Ff4jConfiguration.class
@Bean
@ConditionalOnMissingBean
public FF4j getFF4j() {
return new FF4j("ff4j.xml");
}应用程序加载程序也作了更改:
@Import( {..., Ff4jConfiguration.class})
@AutoConfigureAfter(Ff4jConfiguration.class)我的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>我的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");
}
}在运行时,我看到ff4j bean 正确地注入了具有相应属性的:
ff4j.check("check-no-logging")
> result=false
ff4j.check("check-logging")
> result=true我希望方法log2永远不会被调用,但是它是(所有使用的方法都被调用了,没有被忽略)。有人能帮我一下我做错了什么吗?
发布于 2017-10-09 23:07:32
注释Flip意味着定位在接口上,而不是在bean上。其原因是强制人们在使用AOP时为同一方法创建不同的实现。(以后需要清洗时更简单)。
我可以提出两个解决方案。第一个似乎很明显,但是如果您没有多个实现..。
@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");
}
}
}第二种方法是确实使用AOP,您必须:
org.ff4j.aop中的自动代理。但是它已经通过添加自动配置依赖来完成了。@Flip注释放在接口上并创建不同的实现:下面是一个代码示例:
@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已经被复制,这2种工作解决方案可以在这里获得:https://github.com/clun/ff4j-samples/tree/master/ff4j-sample-sergii
https://stackoverflow.com/questions/46525622
复制相似问题