In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly introduces how to configure the AOP of the Spring framework in Java, which has a certain reference value, and interested friends can refer to it. I hope you can learn a lot after reading this article.
What is AOP?
AOP (Aspect Oriented Programming) aspect-oriented programming is an important supplement to OOP (object-oriented programming). Object-oriented programming is the basis of Java programming. It establishes the hierarchical relationship between objects with encapsulation, inheritance and polymorphism, and pays attention to the concrete implementation of each object. Object orientation allows us to develop vertical relationships.
AOP focuses on horizontal relationships, that is, classes do not need specific relationships. It can insert some common operations (such as log handling, security verification, transaction handling, exception handling, performance statistics, etc.) into these unrelated classes, so that each class can only focus on its own core logic without having to deal with these general operations. This process is crosscutting. And these general operations are section Aspect.
To put it simply: AOP can encapsulate the logic code that has nothing to do with the core business of the class and is needed by multiple classes, and it can be called automatically when the object is needed, which reduces the repetitive code in the class, reduces the coupling between classes, and improves the maintainability of the program.
AOP terminology
1. Crosscutting concerns
Which methods are intercepted and what to do after interception? these concerns are called crosscutting concerns.
2. Section (aspect)
Class is the abstraction of the characteristics of an object, and aspect is the abstraction of crosscutting concerns.
3. Connection point (joinpoint)
The intercepted point, because Spring only supports join points of method types, so in Spring, join points refer to intercepted methods. In fact, join points can also be fields or constructors.
4. Pointcut (pointcut)
Definition of interception of connection points
5. Notification (advice)
The so-called notification refers to the code to be executed after intercepting the connection point. Notifications can be divided into five categories: pre -, post -, exception, final and surround notification.
6. Target object
The target object of the agent
7. Weave in (weave)
The process of applying a section to a target object and causing the creation of a proxy object
8. Introduce (introduction)
Without modifying the code, introduce that some methods or fields can be dynamically added to the class at run time
AOP application scenario
1) logging can be used to log the start and completion of method execution and the occurrence of exceptions
2) caching, calling the query database for the first time, putting the query results into the memory object, and the second call, returning directly from the memory object, without the need to query the database
3) transaction, open the transaction before calling the method, commit and close the transaction after calling the method
Wait
Configuration of AOP
1) Import Maven dependencies
Junit
Junit
4.12
Test
Org.springframework
Spring-context
4.3.14.Release
Org.springframework
Spring-core
4.3.14.RELEASE
Org.springframework
Spring-beans
4.3.14.RELEASE
Org.springframework
Spring-context-support
4.3.14.RELEASE
Org.springframework
Spring-expression
4.3.14.RELEASE
Org.springframework
Spring-aop
4.3.14.RELEASE
Org.aspectj
Aspectjrt
1.8.13
Org.aspectj
Aspectjweaver
1.8.13
Org.springframework
Spring-test
4.3.14.RELEASE
Commons-logging
Commons-logging
1.1.2
Log4j
Log4j
1.2.17
Write Service interfaces and implementation classes, where only the simulation operation is completed
Public interface AdminService {
Void saveAdmin ()
Void updateAdmin ()
Void deleteAdmin ()
Void selectAdmin ()
}
Public class AdminServiceImpl implements AdminService {
@ Override
Public void saveAdmin () {
System.out.println ("Invoke saveAdmin")
}
@ Override
Public void updateAdmin () {
System.out.println ("Invoke updateAdmin")
}
@ Override
Public void deleteAdmin () {
System.out.println ("Invoke deleteAdmin")
}
@ Override
Public void selectAdmin () {
System.out.println ("Invoke selectAdmin")
}
}
3) write a custom enhancement class whose methods correspond to the five enhancement notifications of AOP, which are:
1) pre-notification before: execute before method call
2) Post-notification after-returning: it will be executed after the method is called, but will not be executed if an exception occurs.
3) Post-notification after: it is executed after the method is called, and if an exception occurs, it also executes.
4) notify after-throwing of exception: exception execution occurs
5) orbit notification around: execute before and after method invocation
/ * *
* Custom enhancement classes
* /
Public class MyAdvise {
Public void before () throws Throwable {
System.out.println ("this is before")
}
Public void afterReturning () {
System.out.println ("this is afterReturning")
}
Public void after () {
System.out.println ("this is after")
}
Public void afterThrowing () throws Throwable {
System.out.println ("this is afterThrowing")
}
Public Object around (ProceedingJoinPoint point) throws Throwable {
System.out.println ("this is around-pre-execution")
Object obj = point.proceed ()
System.out.println ("this is around-post execution")
Return obj
}
}
4) Spring configuration file
5) run the test
@ RunWith (SpringJUnit4ClassRunner.class)
@ ContextConfiguration ("classpath:applicationContext-aop.xml")
Public class TestAOP {
@ Test
Public void test () {
ApplicationContext app = new
ClassPathXmlApplicationContext ("applicationContext-aop.xml")
AdminService as = (AdminService) app.getBean ("adminService")
As.deleteAdmin ()
}
}
You can see that the notification method in MyAdvise is called when any method of the Service class is executed
Thank you for reading this article carefully. I hope the article "how to configure the AOP of Spring Framework in Java" shared by the editor will be helpful to everyone. At the same time, I also hope you will support us and pay attention to the industry information channel. More related knowledge is waiting for you to learn!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.