-
Notifications
You must be signed in to change notification settings - Fork 35
Fusion
Yizzuide edited this page Nov 3, 2020
·
3 revisions
Fusion has a set of capabilities for changing method execution, including modifying return values, conditional execution with failure feedback.
Fusion具有改变方法执行的一系列功能,包含修改返回值、条件执行与失败反馈。
<dependency>
<groupId>com.github.yizzuide</groupId>
<artifactId>milkomeda-spring-boot-starter</artifactId>
<version>${milkomeda-last-version}</version>
</dependency>
@EnableFusion
@SpringBootApplication
public class MilkomedaDemoApplication {
public static void main(String[] args) {
SpringApplication.run(MilkomedaDemoApplication.class, args);
}
}
// Add the `@FusionHandler` annotation to the class to implement the self-registration of the processor
// 在类上添加注解 `@FusionHandler`以实现处理器的自注册
@FusionHandler
public class APIResponseDataHandler {
// Add the `@FusionAction` on method, `tag` of attribute method used for classification processing, method parameter must be `FusionMetaData`
// 在方法上添加`@FusionAction`注解,它的属性方法`tag`用于多分类处理,方法参数必须为`FusionMetaData`类型
@FusionAction(tag = "api")
public ReturnVO<?> apiAction(FusionMetaData<Product> metaData) {
// Return error if build with `FusionConverter.buildError`
// 如果使用`FusionConverter.buildError`构建的返回,就是失败类型的返回
if (metaData.isError()) {
return new ReturnVO<>().error(metaData.getMsg());
}
return new ReturnVO<>().ok(metaData.getReturnData());
}
}
// The modified method must return an Object
@Fusion(tag = "api")
@RequestMapping("product/pull")
public Object pull() {
// Return success data
// 返回成功的数据
// return productService.pull();
// Return error
// 返回错误
return FusionConverter.buildError("query fail");
}
The allowed
and fallback
attribute method of @Fusion
are support Spring EL,which first is condition, second is invoke method, fallback
will invoke replace original if allowed
is return false.
@Fusion
注解的属性allowed
和fallback
都支持Spring EL,第一个是条件表达式,第二个方法调用。如果allowed
返回为false,源业务方法会被替换为fallback
指定的方法调用
@Slf4j
@Service("productService")
public class ProductServiceImpl implements ProductService {
@Fusion(allowed = "@env.get('spring.profiles.active') == 'prod'", fallback = "@productService.pushNotCheck(#product, #delay)")
@Override
public void push(Product product, boolean delay) {
log.info("push product:{}", product.getName());
}
public void pushNotCheck(Product product, boolean delay) {
log.info("Not push at dev environment:{}, product.getName());
}
}