📜  Spring Boot AOP围绕建议

📅  最后修改于: 2021-01-11 05:19:29             🧑  作者: Mango

Spring Boot AOP咨询

周围的建议由@Around注释表示。它在连接点之前和之后执行。这是最有力的建议。它还为最终用户提供了更多控制权,使其可以处理ProceedingJoinPoint。

让我们围绕应用程序中的建议实施。

Spring Boot咨询建议示例

步骤1:打开Spring Initializr http://start.spring.io

步骤2:提供群组名称。我们提供了组名com.javatpoint。

第3步:提供工件ID。我们提供了工件ID aop-around-advice-example实例。

步骤4:添加Spring Web依赖项。

步骤5:点击Generate(生成)按钮。当我们单击Generate按钮时,它将所有规范包装在jar文件中,并将其下载到本地系统。

步骤6:解压缩下载的jar文件。

步骤7:使用以下步骤导入文件夹:

文件->导入->现有Maven项目->下一步->浏览文件夹aop-around-advice-example- >完成。

步骤8:打开pom.xml文件并添加以下AOP依赖项。它是使用Spring AOPAspectJ进行面向方面的编程的入门。


org.springframework.boot
spring-boot-starter-aop


pom.xml



4.0.0

org.springframework.boot
spring-boot-starter-parent
2.2.2.RELEASE
 

com.javatpoint
aop-around-advice-example
0.0.1-SNAPSHOT
aop-around-advice-example
Demo project for Spring Boot

1.8



org.springframework.boot
spring-boot-starter-web


org.springframework.boot
spring-boot-starter-aop


org.springframework.boot
spring-boot-starter-test
test


org.junit.vintage
junit-vintage-engine







org.springframework.boot
spring-boot-maven-plugin




步骤9:创建一个名称为com.javatpoint.service的包。

步骤10:在上面的包中创建一个名为BankService的类

在此类中,我们定义了一个名为displayBalance()的方法。它检查帐号。如果帐号匹配则返回总金额,否则返回一条消息。

BankService.java

package com.javatpoint.service;
import org.springframework.stereotype.Service;
@Service
public class BankService 
{
public void displayBalance(String accNum) 
{
System.out.println("Inside displayBalance() method");
if(accNum.equals("12345")) 
{
System.out.println("Total balance: 10,000");
}
else 
{
System.out.println("Sorry! wrong account number.");
}
}
}

步骤11:创建另一个名为com.javatpoint.aspect的包。

步骤12:在上面的包中创建一个名为BankAspect的类。

在下面的类中,我们定义了两个名为logDisplayingBalance()aroundAdvice()方法的方法。

BankAspect.java

package com.javatpoint.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;
//Enables the spring AOP functionality in an application
@Aspect
@Component
public class BankAspect
{
//Displays all the available methods i.e. the advice will be called for all the methods
@Pointcut(value= "execution(* com.javatpoint.service.BankService.*(..))")
private void logDisplayingBalance() 
{ 
}
//Declares the around advice that is applied before and after the method matching with a pointcut expression
@Around(value= "logDisplayingBalance()")
public void aroundAdvice(ProceedingJoinPoint jp) throws Throwable 
{
System.out.println("The method aroundAdvice() before invokation of the method " + jp.getSignature().getName() + " method");
try 
{
jp.proceed();
} 
finally 
{

}
System.out.println("The method aroundAdvice() after invokation of the method " + jp.getSignature().getName() + " method");
}
}

步骤13:打开AopAroundAdviceExampleApplication.java文件,并添加一个注释@EnableAspectJAutoProxy。

该注释支持处理标有AspectJ的@Aspect注释的组件。它与@Configuration批注一起使用。

ConfigurableApplicationContext是一个接口,除了ApplicationContext中的应用程序上下文客户端方法外,它还提供了用于配置应用程序上下文的工具。

AopAroundAdviceExampleApplication.java

package com.javatpoint;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.EnableAspectJAutoProxy;
import com.javatpoint.service.BankService;
@SpringBootApplication
//@EnableAspectJAutoProxy annotation enables support for handling the components marked with @Aspect annotation. It is similar to tag in the xml configuration.
@EnableAspectJAutoProxy
public class AopAroundAdviceExampleApplication 
{
public static void main(String[] args) 
{
ConfigurableApplicationContext context = SpringApplication.run(AopAroundAdviceExampleApplication.class, args);
// Fetching the employee object from the application context.
BankService bank = context.getBean(BankService.class);
// Displaying balance in the account
String accnumber = "12345";
bank.displayBalance(accnumber);
// Closing the context object
context.close();
}
}

创建所有软件包和类之后,项目目录如下所示:

现在,运行该应用程序。

步骤14:打开AopAroundAdviceExampleApplication.java并将其作为Java应用程序运行。

在上面的输出中,我们看到方法aroundAdvice()被调用两次。首先,在执行displayBalance()方法之前,其次,在执行displayBalance()方法之后。它被称为咨询。