class=”markdown_views prism-dracula”>
Summary on the Spring Framework (3. Spring AOP)
3. Spring AOP
Spring AOP
3.1. Basic concepts of Spring AOP
3.2. Dynamic proxy
3.3. Common terms of AOP
3.4. XML-based configuration development
3.5. Annotation-based development
3.1. Basic concepts of Spring AOP
AOP (Aspect-Oriented Programming) is aspect-oriented programming, a technology that achieves unified maintenance of program functions through pre-compilation and runtime dynamic agents. It complements OOP (Object-Oriented Programming, Object-Oriented Programming) and provides a perspective of abstract software structure different from OOP. In OOP, the class is used as the basic unit of the program, while the basic unit in AOP is Aspect (aspect). AOP can be used to isolate each part of the business logic, so that the coupling degree between the parts of the business logic is reduced, the reusability of the program is improved, and the efficiency of development is improved at the same time.
Simply speaking, it is to extract the repeated code of our program, and when it needs to be executed, use dynamic proxy technology to enhance our existing methods without modifying the source code.
3.2. Dynamic proxy
There are two commonly used methods for dynamic proxy
(1), JDK dynamic proxy (interface-based dynamic proxy)
1. Create an interface and Implementation class
public interface IActor {
/**
* Basic show
* @param money
*/
public void basicAct(float money);
/**
* Dangerous show
* @param money
*/
public void dangerAct(float money);
}
/**
* an actor
*/
// Implementing the interface means that it has the method implementation in the interface. That is: meet the requirements of the brokerage company
public class Actor implements IActor{
public void basicAct(float money){
System.out.println("Get money and start basic performance:"+money);
}
public void dangerAct(float money){
System.out.println("Get the money and start a dangerous show:"+money);
}
}
2. Create a proxy class
public class Client {
public static void main);
} catch (SQLException e) {
e.printStackTrace();
} }
@After
effect:
Think of the current method as the final notification.
Attributes:
value: used to specify the pointcut expression, you can also specify a reference to the pointcut expression
//Release resources
@After("execution(* com.itheima.service.impl .*.*(..))")
public void release() {
try {
dbAssit.releaseConnection();
} catch (Exception e) {
e.printStackTrace();
}
}
@Around
effect:
Think of the current method as a wrap around advice.
Attributes:
value: used to specify the pointcut expression, and can also specify a reference to the pointcut expression.
/**
* Surround notifications
* @param pjp
* @return
*/
@Pointcut
effect:
specify pointcut expression
Attributes:
value: the content of the specified expression
@Pointcut("execution(* com.itheima.service.impl .*.*(..))")
private void pt1() {
}
@Around("pt1()")
public Object transactionAround(ProceedingJoinPoint pjp) {
//Define the return value
Object rtValue = null;
try {
//Get the parameters required for method execution
Object[] args = pjp.getArgs();
//Pre-notification: open transaction
beginTransaction();
//execution method
rtValue = pjp.proceed(args);
//post notification: submit transaction
commit();
}catch(Throwable e) {
//Exception notification: rollback transaction
rollback();
e.printStackTrace();
}finally {
//Final notification: release resources
release();
}
return rtValue;
}
References: [1] Chen Heng, Lou Oujun, Zhang Lijie. Java EE Framework Integration and Development Introduction to Practice [M]. Tsinghua University Press, 2018-:.39-45
The case comes from the dark horse programmer Spring teaching
ctuation”>);
}catch(Throwable e) {
//Exception notification: rollback transaction
rollback();
e.printStackTrace();
}finally {
//Final notification: release resources
release();
}
return rtValue;
}
References: [1] Chen Heng, Lou Oujun, Zhang Lijie. Java EE Framework Integration and Development Introduction to Practice [M]. Tsinghua University Press, 2018-:.39-45
The case comes from the dark horse programmer Spring teaching