English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
Around Advice는 @Around 애드벌스를 나타내는 애드벌스. 이는 연결점 전후에서 실행됩니다. 이는 가장 강력한 권고사항입니다. 또한 최종 사용자에게 더 많은 통제권을 제공하여, 그들은 처리할 수 있습니다 ProceedingJoinPoint.
애플리케이션에서의 권고사항을 실시하도록 합시다.
단계1: Spring Initializr를 열십시오 http://start.spring.io.
단계2: 제공합니다 그룹이름. 우리는 그룹 이름을 제공합니다 com.w3codebox.
단계3: 제공합니다 Artifact Id.Artifact Id를 제공합니다 aop-around-advice-example.
단계4: 추가 Spring Web 의존성.
단계5: 클릭 생성버튼. "생성" 버튼을 클릭하면 모든 규칙을 포장하여 jar 파일에서 내려받고 로컬 시스템에 저장하십시오.
번6단계: 추출다운로드한 jar 파일.
단계7다음 단계로 이동하여 import하십시오폴더:
파일-импорт-현재 Maven 프로젝트-다음 단계로 이동하십시오-폴더를 탐색하십시오 aop-around-advice-example -완료.
단계8: 열기 pom.xml 파일을 추가하고 다음과 같이 설정합니다 AOP 의존성입니다. 이는 사용 Spring AOP 과 AspectJ 面向方面编程의 입문을 위해 합니다.
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-aop</artifactId> </dependency> </dependencies>
pom.xml
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.2.2.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> <groupId>com.w3codebox</groupId> <artifactId>aop-around-advice-example</artifactId> <version>0.0.1-SNAPSHOT</version> <name>aop-around-advice-example</name> <description>Demo project for Spring Boot</description> <properties> <java.version>1.8</java.version> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-aop</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> <exclusions> <exclusion> <groupId>org.junit.vintage</groupId> <artifactId>junit-vintage-engine</artifactId> </exclusion> </exclusions> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>
단계9: 이름이 com.w3codebox.service의 패키지.
단계10: 위 패키지에서 codebox.aspect 패키지를 생성하십시오. BankService 의 종류.
}} 이 클래스에서 이름을 정의한 displayBalance() 메서드.
계정 번호를 확인합니다. 계정 번호가 일치하면 총 금액을 반환하고, 일치하지 않으면 메시지를 반환합니다.
package com.w3BankService.java codebox.service; import org.springframework.stereotype.Service; @Service { public class BankService { public void displayBalance(String accNum) System.out.println("Inside displayBalance() method");12345if(accNum.equals(" { ")) 10System.out.println("Total balance:") } ,000"); { else } } }
단계11: System.out.println("Sorry! wrong account number."); com.w3다른 하나를 생성하십시오
단계12: 위 패키지에서 codebox.aspect 패키지를 생성하십시오. BankAspect 클래스.
다음 클래스에서 두 개의 이름을 정의한 logDisplayingBalance()과 aroundAdvice()메서드의 메서드.
BankAspect.java
package com.w3codebox.aspect; import org.aspectj.lang.ProceedingJoinPoint; import org.aspectj.lang.annotation.Around; import org.aspectj.lang.annotation.Aspect; import org.aspectj.lang.annotation.Pointcut; import org.springframework.stereotype.Component; //애플리케이션에서 spring AOP 기능을 활성화합니다 @Aspect @Component public class BankAspect { //모든 메서드 호출에 알림을 설정하여 모든 사용 가능한 메서드를 표시 @Pointcut(value= "execution(* com.w3codebox.service.BankService.*(..))) private void logDisplayingBalance() { } //메서드와 절점 표현식이 일치하기 전과 후에 적용되는 around 알림을 선언합니다. @Around(value= "logDisplayingBalance()") public void aroundAdvice(ProceedingJoinPoint jp) throws Throwable { System.out.println("aroundAdvice() 메서드 호출 전" + jp.getSignature().getName() + " method "); try { jp.proceed(); } finally { } System.out.println("aroundAdvice() 메서드 호출 후" + jp.getSignature().getName() + " method "); } }
단계13: 열기 AopAroundAdviceExampleApplication.java 파일을 추가하고 애노테이션을 추가합니다 @EnableAspectJAutoProxy。
이 애노테이션은 AspectJ의 처리를 지원하는 것을 활성화합니다. @Aspect 댓글을 달린 컴포넌트. @Configuration 댓글과 함께 사용됩니다。
ConfigurableApplicationContext 는 인터페이스입니다. ApplicationContext中的应用 프로그램 컨텍스트 클라이언트 메서드 외에도应用程序 컨텍스트를 구성하는 도구를 제공합니다。
AopAroundAdviceExampleApplication.java
package com.w3codebox; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.context.ConfigurableApplicationContext; import org.springframework.context.annotation.EnableAspectJAutoProxy; import com.w3codebox.service.BankService; @SpringBootApplication //@EnableAspectJAutoProxy 注解支持处理标记为 @Aspect annotation 的组件。它类似于 xml 配置中的标记。 @EnableAspectJAutoProxy public class AopAroundAdviceExampleApplication { public static void main(String[] args) { ConfigurableApplicationContext context = SpringApplication.run(AopAroundAdviceExampleApplication.class, args); //애플리케이션 컨텍스트에서 employee 객체를 가져옵니다. BankService bank = context.getBean(BankService.class); //계정 잔액을 표시합니다 String accnumber = "12345"; bank.displayBalance(accnumber); //context 객체를 닫습니다 context.close(); } }
모든 패키지와 클래스를 생성한 후, 프로젝트 디렉토리는 다음과 같습니다:
이제, 애플리케이션을 실행합니다.
단계14: 열기 AopAroundAdviceExampleApplication.java 그리고 Java 실행 프로그램으로 만듭니다.
위의 출력에서, aroundAdvice() 메서드가 두 번 호출되는 것을 볼 수 있습니다. 먼저, 실행 중에 displayBalance()메서드가 실행되기 전에, 그 다음에 displayBalance()메서드가 끝나면. 컨설턴트로 불립니다.