preface

Recently in view of the Internet company interview asked knowledge points, summed up the Java programmer interview involves most of the interview questions and answers to share with you, I hope to help you review before the interview and find a good job, but also save you on the Internet to search for information time to learn.

Content covers: Java, MyBatis, ZooKeeper, Dubbo, Elasticsearch, Memcached, Redis, MySQL, Spring, SpringBoot, SpringCloud, RabbitMQ, Kafka, Linux and other technology stacks.

Full version Java interview questions address: Java backend questions integration

The proxy pattern

Proxy mode: Provides a proxy for other objects to control access to that object.

This paragraph is quite official, but I prefer to use my own language to understand it: for example, if object A wants to do something, it will do it by itself before there is no proxy, and after A is the proxy class B of A will do it.

Proxying is essentially adding a layer of processing before and after the original instance, which is AOP’s primary contours.

Principle and practice of static proxy

Static proxy mode: Static proxy means that the bytecode file of the proxy class exists before the program runs, and the relationship between the proxy class and the original class is determined before the program runs.

Without further discussion, let’s take a look at the code. To make it easier to read, the blogger merges a separate class file into the interface, and readers can directly copy the code to run:

Test results:

A static proxy ensures that the business class only needs to focus on the logic itself, and that an interface of the proxy object serves only one type of object. If there are many methods to be propped, it is necessary to propped for each method.

Furthermore, if you add a method, all proxy classes need to implement the method in addition to the implementation class. Increased code maintenance costs. So how to solve it? The answer is to use dynamic proxies.

Dynamic proxy principle and practice

Dynamic proxy mode: The source code for dynamic proxy classes is generated dynamically during program execution through mechanisms such as JVM reflection, and the relationship between proxy classes and delegate classes is determined at runtime. Examples are as follows:

In the code that creates test class objects in the run test class

Instead, the JDK dynamically generates a class to implement the interface, hiding the process:

The premise of using dynamic proxies generated by the JDK is that the target class must have an interface to implement. The Cglib proxy is the solution to the problem that JDK dynamic proxies cannot be used if a class does not implement an interface.

Cglib is implemented with a dynamically generated subclass inheritance goal, dynamically building a subclass in memory at runtime as follows:

Cglib is used on the premise that the target class cannot be final. Because final modified classes cannot be inherited.

Now let’s look at the definition of AOP: section-oriented programming, the core principle of which is to use the dynamic proxy pattern to add logic before and after method execution or when exceptions occur.

From the definition and the previous code we can see three things:

  • AOP is based on the dynamic proxy pattern.

  • AOP is method level.

  • AOP can separate business code from concern code (duplicate code) and inject concern code dynamically as business code is executed. The aspect is the class formed by the code of concern.

Principle and practice of Spring AOP

As mentioned above, the JDK proxy and the Cglib proxy are two dynamic proxies. The excellent Spring framework integrates both approaches in the bottom layer, so we don’t need to worry about implementing dynamic proxies ourselves. So how does Spring generate proxy objects?

1. When creating container objects, generate proxy objects based on the classes intercepted by pointcut expressions.

2. If the target object has an implementation interface, use a JDK proxy. If the target object does not implement an interface, the Cglib proxy is used. It then gets the proxied object from the container and implants the methods of the “aspect” class at run time. In the DefaultAopProxyFactory class, we can find this statement in the Spring source code.

[image uploaded…(image-807F5A-1618380051399-2)]

The simple fact is, if you have an interface, use a Jdk proxy, and if you don’t, use Cglib, which is exactly what I said earlier. If the target class does not implement an interface, and the class is final, then Spring AOP cannot be programmed!

With that in mind, we’ll now implement Spring AOP manually ourselves:

XML configuration file for Spring:

Copy the code

Pointcut expressions are not covered here. Here are the code’s test results. Now that we’ve covered Spring AOP, we return to our opening question: What do we do with it?

  • Spring declarative transaction management configuration

  • Parameter verification of the Controller layer

  • Use Spring AOP to implement MySQL database read and write separation case analysis

  • Before executing a method, determine whether permissions are available.