Atitit aop spring5.2 demo與流程總結 目錄 1.1. 定義切面MyAllAspect 1 1.2. 定義普通型別userservice 1 1.3. 設定切面到某個方法上ex
Atitit aop spring5.2 demo與流程總結
目錄
1.3. 設定切面到某個方法上execution(* aoppkg.UserService.login(..) 2
@Component
@Aspect // AOP 切面u
@SuppressWarnings
public class MyAllAspect {
// 切入點(Pointcut):用於定義通知應該切入到哪些連線點上
@AfterReturning(value = "execution(* aoppkg.UserService.login(..))", returning = "result")
public Object afterReturning(JoinPoint joinPoint
System.out.println("log.....");
return result;
}
}
//aoppkg.userService.login
@Component
public class UserService {
public int login
return i + 3;
}
@AfterReturning(value = "execution(* aoppkg.UserService.login(..))", returning = "result")
}
@EnableTransactionManagement
@EnableAspectJAutoProxy
@Configuration /** 該註解表示這個類是一個Spring的配置類 **/
@ComponentScan(basePackages = {
"aoppkg" }) /***
* 該註解表示啟用spring的元件掃描功能,並且配置了掃描包net.xqlee.project.
* demo下的所有類
**/
public class AppConfig implements TransactionManagementConfigurer {
public class start {
public static void main(String[] args) {
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(AppConfig.class);
// 獲取通過註解注入容器的UserService
UserService userService1 = context.getBean(UserService.class);
// 呼叫userService的方法執行
userService1.login(6);
}
}