jfinal-aop
introduction
jfinal內置的aop實現太優秀了,為了方便其他框架的項目中使用,獨立成了單獨的項目並進行了二次開發,命名為jfinal-aop
開源地址
https://github.com/litongjava/jfinal-aop
使用maven導入
<dependency>
<groupId>com.litongjava</groupId>
<artifactId>jfinal-aop</artifactId>
<version>1.1.7</version>
</dependency>
usage
切面使用
-
Cat類:
這是一個簡單的類,包含一個名為eat的方法。這個方法被@Before(Aspect1.class)註解修飾,表示在調用eat方法之前,會先執行Aspect1類的intercept方法。package com.issues01; import com.litongjava.jfinal.aop.Before; public class Cat { @Before(Aspect1.class) public String eat() { return "eat chat"; } } -
Aspect1類:
這是一個攔截器類,實現了Interceptor接口。在intercept方法中,它首先打印出 "Before Aspect1 invoking",然後獲取被攔截方法的相關信息,包括方法對象、方法名、參數和目標對象,並打印出這些信息。然後,它調用invocation.invoke()執行被攔截的方法,並將返回值設置為 "set new value"。最後,打印出 "After Aspect1 invoking"。package com.issues01; import com.litongjava.jfinal.aop.Interceptor; import com.litongjava.jfinal.aop.Invocation; import lombok.extern.slf4j.Slf4j; import java.lang.reflect.Method; import java.util.Arrays; @Slf4j public class Aspect1 implements Interceptor { @Override public void intercept(Invocation invocation) { System.out.println("Before Aspect1 invoking"); Method method = invocation.getMethod(); String methodName = invocation.getMethodName(); Object[] args = invocation.getArgs(); Object target = invocation.getTarget(); log.info("method:{}", method); log.info("methodName:{}", methodName); log.info("args:{}", Arrays.toString(args)); log.info("target:{}", target); Object invoke = invocation.invoke(); invocation.setReturnValue("set new value"); log.info("invoke:{}", invoke); System.out.println("After Aspect1 invoking"); } } CatMainTest類:
這是主類,包含main方法和index方法。在main方法中運行index方法。在index方法中,它首先打印出 Java 版本,然後獲取Cat類的實例,並調用eat方法,最後打印出eat方法的返回值。
package com.issues01;
import com.litongjava.jfinal.aop.Aop;
public class CatMainTest {
public static void main(String[] args) {
new CatMainTest().index();
//SimpleApp.run(CatMainTest.class.getName(), "index");
}
public void index() {
String javaVersion = System.getProperty("java.version");
System.out.println("java-version:" + javaVersion);
// ProxyManager.me().setProxyFactory(new CglibProxyFactory());
Cat cat = Aop.get(Cat.class);
String eat = cat.eat();
System.out.println("result:" + eat);
}
}
output
java-version:1.8.0_121
22:34:33.877 [main] DEBUG com.litongjava.jfinal.proxy.ProxyGenerator -
Generate proxy class "com.issues01.Cat$$EnhancerByJFinal":
package com.issues01;
import com.litongjava.jfinal.aop.Invocation;
public class Cat$$EnhancerByJFinal extends Cat {
public java.lang.String eat() {
Invocation inv = new Invocation(this, 1L,
args -> {
return Cat$$EnhancerByJFinal.super.eat(
);
}
);
inv.invoke();
return inv.getReturnValue();
}
}
Before Aspect1 invoking
22:34:34.250 [main] INFO com.issues01.Aspect1 - method:public java.lang.String com.issues01.Cat.eat()
22:34:34.251 [main] INFO com.issues01.Aspect1 - methodName:eat
22:34:34.251 [main] INFO com.issues01.Aspect1 - args:[]
22:34:34.251 [main] INFO com.issues01.Aspect1 - target:com.issues01.Cat$$EnhancerByJFinal@55a561cf
22:34:34.251 [main] INFO com.issues01.Aspect1 - invoke:eat chat
After Aspect1 invoking
result:set new value
掃描類並初始化
這段代碼主要包含四個部分:DemoService 接口,DemoServiceImpl 類,DemoController 類和 DemoApp 類。
DemoService接口:
這是一個簡單的接口,定義了一個Hello方法。
package com.issues02;
public interface DemoService {
public String Hello();
}
DemoServiceImpl類:
這是DemoService接口的實現類,它被@Service註解修飾,表示它是一個服務類。在Hello方法中,它返回了一個字符串 "Hello"。
package com.issues02;
import com.litongjava.jfinal.aop.annotation.Service;
@Service
public class DemoServiceImpl implements DemoService {
public String Hello(){
return "Hello";
}
}
DemoController類:
這是一個控制器類,被@Controller註解修飾。它包含一個DemoService類型的成員變量demoService,並使用@Autowired註解進行自動注入。在hello方法中,它調用了demoService的Hello方法,並返回了其結果。
package com.issues02;
import com.litongjava.jfinal.aop.Autowired;
import com.litongjava.jfinal.aop.Inject;
import com.litongjava.jfinal.aop.annotation.Controller;
import java.lang.annotation.Inherited;
@Controller
public class DemoController {
// @Inject
// private DemoService demoService;
@Autowired
private DemoService demoService;
public String hello() {
return demoService.Hello();
}
}
DemoApp類:
這是主類,包含main方法。在main方法中,它首先掃描DemoApp類所在的包,然後初始化註解。接着,它獲取DemoController類的實例,並調用hello方法,最後打印出hello方法的返回值。
package com.issues02;
import com.litongjava.jfinal.aop.Aop;
import com.litongjava.jfinal.aop.annotation.ComponentScan;
import com.litongjava.jfinal.aop.process.BeanProcess;
import com.litongjava.jfinal.aop.scaner.ComponentScanner;
import java.util.List;
@ComponentScan
public class DemoApp {
public static void main(String[] args) throws Exception {
List<Class<?>> scannedClasses = Aop.scan(DemoApp.class);
Aop.initAnnotation(scannedClasses);
DemoController demoController = Aop.get(DemoController.class);
String hello = demoController.hello();
System.out.println(hello);
Aop.close();
}
}
output
Hello