您好,登錄后才能下訂單哦!
這篇文章主要介紹“Java面向切面編程AOP怎么實現(xiàn)”,在日常操作中,相信很多人在Java面向切面編程AOP怎么實現(xiàn)問題上存在疑惑,小編查閱了各式資料,整理出簡單好用的操作方法,希望對大家解答”Java面向切面編程AOP怎么實現(xiàn)”的疑惑有所幫助!接下來,請跟著小編一起來學習吧!
一:背景
Spring的AOP的存在目的是為了解耦。AOP可以讓一組類共享相同的行為。在OOP中只能通過繼承類和實現(xiàn)接口,來是代碼的耦合度增強,且類繼承只能為單繼承,阻礙更多行為添加到一組類上,AOP彌補了OPP的不足。
二:概述 Spring支持AspectJ的注解方式切面編程
1.使用@Aspect 聲明一個切面。
2.使用@After,@Before,@Around 定義建言(advice),可直接將攔截規(guī)則(切點)作為參數(shù)。
3.其中@After,@Before,@Around參數(shù)的攔截規(guī)則為切點(PointCut) ,為了使切點復(fù)用,可使用@PointCut 專門定義攔截規(guī)則,然后在@After,@Before,@Around的參數(shù)中調(diào)用。
4.其中符合條件的每一個被攔截處為連接點(JoinPoint)
三:代碼實例
1.pom.xml
點擊(此處)折疊或打開
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aop</artifactId>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjrt</artifactId>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
</dependency>
2.攔截規(guī)則的注解
點擊(此處)折疊或打開
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface Action {
String name();
}
2.注解的被攔截類
點擊(此處)折疊或打開
@Service
public class DemoAnnotationService {
@Action(name = "注解式攔截的add操作")
public void add() {
System.out.println("======DemoAnnotationService方法add()=========");
}
}
3.方法規(guī)則被攔截類
點擊(此處)折疊或打開
@Service
public class DemoMethodService {
public String add() throws Exception{
System.out.println("======DemoMethodService方法add()=========");
int i=100/0;
return "SUCCESS";
}
}
4.編寫切面
點擊(此處)折疊或打開
@Aspect
@Component
public class LogAspect {
@Pointcut("@annotation(com.gemdale.gmap.spring.boot.demo.Action)")
public void annotationPointCut() {
}
@After("annotationPointCut()")
public void after(JoinPoint joinPoint) {
MethodSignature signature = (MethodSignature) joinPoint.getSignature();
Method method = signature.getMethod();
Action action = method.getAnnotation(Action.class);
System.out.println("注解式攔截 " + action.name());
}
@Before("execution(* com.gemdale.gmap.spring.boot.demo.DemoMethodService.*(..))")
public void methodBefore(JoinPoint joinPoint) {
MethodSignature signature = (MethodSignature) joinPoint.getSignature();
Method method = signature.getMethod();
System.out.println("before方法規(guī)則式攔截 " + method.getName());
}
@After("execution(* com.gemdale.gmap.spring.boot.demo.DemoMethodService.*(..))")
public void methodAfter(JoinPoint joinPoint) {
MethodSignature signature = (MethodSignature) joinPoint.getSignature();
Method method = signature.getMethod();
System.out.println("after方法規(guī)則式攔截 " + method.getName());
}
@AfterReturning(pointcut = "execution(* com.gemdale.gmap.spring.boot.demo.DemoMethodService.*(..))", returning = "result")
public void methodAfterResult(JoinPoint joinPoint, Object result) {
MethodSignature signature = (MethodSignature) joinPoint.getSignature();
Method method = signature.getMethod();
System.out.println("after result方法規(guī)則式攔截 " + method.getName() + "result=" + result);
}
@AfterThrowing(pointcut = "execution(* com.gemdale.gmap.spring.boot.demo.DemoMethodService.*(..))", throwing = "e")
public void methodAfterException(JoinPoint joinPoint, Exception e) {
MethodSignature signature = (MethodSignature) joinPoint.getSignature();
Method method = signature.getMethod();
System.out.println("after exception方法規(guī)則式攔截 " + method.getName() + " e=" + e.getMessage());
}
}
5.配置類
點擊(此處)折疊或打開
@Configuration
@ComponentScan("com.gemdale")
@EnableAspectJAutoProxy
public class AppliactionConfig {
}
6.執(zhí)行類
點擊(此處)折疊或打開
public class Start {
public static void main(String[] args) throws Exception{
AnnotationConfigApplicationContext configApplicationContext = new AnnotationConfigApplicationContext(
AppliactionConfig.class);
// UseFunctionService
// useFunctionService=configApplicationContext.getBean(UseFunctionService.class);
// System.out.println(useFunctionService.sayHello("Gengchong"));
DemoAnnotationService demoAnnotationService = configApplicationContext.getBean(DemoAnnotationService.class);
DemoMethodService demoMethodService = configApplicationContext.getBean(DemoMethodService.class);
demoAnnotationService.add();
demoMethodService.add();
configApplicationContext.close();
}
}
到此,關(guān)于“Java面向切面編程AOP怎么實現(xiàn)”的學習就結(jié)束了,希望能夠解決大家的疑惑。理論與實踐的搭配能更好的幫助大家學習,快去試試吧!若想繼續(xù)學習更多相關(guān)知識,請繼續(xù)關(guān)注億速云網(wǎng)站,小編會繼續(xù)努力為大家?guī)砀鄬嵱玫奈恼拢?/p>
免責聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。