What is the aop

AOP (aspect-orientedProgramming, aspect-oriented programming) can be said to be OOP (Object-oriented Programing, object-oriented programming) complement and perfect. OOP allows you to define relationships from top to bottom, but not from left to right. For example, the logging function. Logging code tends to be spread horizontally across all object hierarchies, regardless of the core functionality of the object to which it is spread. This scattering of extraneous code is called cross-cutting code, and in OOP design it leads to a lot of code duplication, which is not conducive to reuse of modules.

AOP, on the other hand, uses a technique called “crosscutting” to peel apart the insides of wrapped objects and encapsulate common behavior that affects multiple classes into a reusable module called “Aspect,” or Aspect. The so-called “aspect”, simply speaking, is to encapsulate the logic or responsibility that has nothing to do with business, but is called by business modules together, so as to reduce the repetitive code of the system, reduce the degree of coupling between modules, and facilitate the future operability and maintainability.

Aop usage scenarios

Categories of AOP frameworks

  • AspectJ
  • JBoss AOP
  • Spring AOP

There are many things you can do with AOP.

  • Performance monitoring, recording call time before and after method invocation, method execution too long or timeout alarm.
  • A caching proxy that caches the return value of a method and fetches it directly from the cache the next time the method is executed.
  • Software cracking, the use of AOP to modify the software validation class of the judgment logic.
  • Log. Record system logs before and after method execution.
  • Workflow systems, which need to mix business code and process engine code to execute together, can be separated using AOP and dynamically hook up business.
  • Before the method is executed, verify whether it has the permission to execute the current method. If it does not, throw an exception with no permission to execute, which is captured by the business code.

Look at the differences between traditional coding and using AOP

The core concept

Some of the terms commonly used to describe AOP are Adivce, Pointcut, Join point, Aspect, Introduction, Weaving, Advice, and so on.

A simple example

The annotation-based approach is much cleaner and more convenient than XML configuration.

@Aspect public class TransactionDemo { @Pointcut(value="execution(* com.yangxin.core.service.*.*.*(..) )") public void point(){ } @Before(value="point()") public void before(){ System.out.println("transaction begin"); } @AfterReturning(value = "point()") public void after(){ System.out.println("transaction commit"); } @Around("point()") public void around(ProceedingJoinPoint joinPoint) throws Throwable{ System.out.println("transaction  begin"); joinPoint.proceed(); System.out.println("transaction commit"); }}Copy the code

Configuration in applicationContext.xml.

<aop:aspectj-autoproxy />
<bean id = "transactionDemo" class = "com.yangxin.core.transaction.TransactionDemo" />
Copy the code

Spring aop principle

As you can see from the previous introduction, an AOP proxy is actually an object dynamically generated by an AOP framework that can be used as a target object. An AOP proxy contains all the methods of the target object, but there are differences between the methods in an AOP proxy and the methods of the target object: THE AOP methods add enhanced processing at specific pointcuts and call back the methods of the target object.

Spring’s AOP proxies are generated and managed by Spring’s IoC container, and their dependencies are also managed by the IoC container. Thus, AN AOP proxy can directly target other Bean instances in the container, and this relationship can be provided by the dependency injection of the IoC container.

There are only three parts of AOP development that require programmer involvement:

  • Define common business components.
  • Define pointcuts, where a pointcut may crosscut multiple business components.
  • Define enhancements, which are processing actions woven into the AOP framework for common business components.

To clarify the relationship, let’s start with a class diagram.

Two dynamic proxy modes

Spring implements AOP by default with the dynamic proxy mechanism, using CGlib when dynamic proxies are not available (proxy classes have no interfaces).

Spring provides two ways to generate proxy objects: JDKProxy and Cglib, and it is up to AopProxyFactory to decide which method to use based on the configuration of the AdvisedSupport object. The default strategy is to use JDK dynamic proxy technology if the target class is an interface, otherwise use Cglib to generate the proxy.

JDK dynamic proxy

  • JDK dynamic proxies mainly involve two classes in the java.lang.Reflect package: Proxy and InvocationHandler. InvocationHandler is an interface that dynamically marshals crosscutting logic and business logic by implementing the interface that defines crosscutting logic and invokes the code of the target class through reflection.

  • Proxy uses InvocationHandler to dynamically create an instance that conforms to an interface and generate a Proxy object for the target class.

CGLib dynamic proxy

  • CGLib is a powerful high-performance, high-quality Code Generation Library, which can extend Java classes and realize Java interfaces at run time. CGLib encapsulates ASM, which can dynamically generate new classes at run time. Compare this to JDK dynamic proxies: JDK creation of proxies is limited to creating proxy instances for interfaces, whereas dynamic proxies can be created using CGLib for classes that do not define business methods through interfaces.

Intellectual development

Through the above analysis, whether we have a familiar feeling, seems to be similar to the function of interceptor, filter. The question then arises as to how AOP relates to interceptors and filters.

Let’s start by reviewing interceptors and filters. TestFilter1 and TestFilter2 are registered in web.xml. You then configure the BaseInterceptor and TestInterceptor in the Spring configuration file. The result is shown below. As you can see from the figure, both interceptors and filters crosscut business methods, seemingly in line with AOP thinking.

Filter Filter: intercepts web access urls. Interceptor: Intercepts.action urls that end in.action and intercepts action access. Spring AOP interceptor: Intercepts access only to Spring managed beans (business layer Services)

Write in the last

The next article will write about Spring Cache, again in mind mapping style. Visual learning, so that Java is no longer difficult.

Finally, welcome to pay attention to my wechat public account Java mind Map, download map source files, as well as more Java mind map and project materials for you to learn, take you into the world of memory brain map.

Read the previous article

  • Spring Mind Map, make Spring not difficult (MVC)
  • Spring Mind Map, make Spring not difficult (IOC)
  • Spring Mind Map, make Spring not difficult (1)