1, the introduction of
If a non-transactional method a of Service is called from a Controller and then transactional method B is called from a, does the b transaction take effect?
|
Like this, if the update method is called directly from the Controller layer, does the updateActual transaction take effect? If not, how can transactions be made effective?
2. What is agency?
An agent, simply, is a surrogate agent, a surrogate operator. In Java, there are generally two types, static proxy and dynamic proxy. Dynamic proxy is divided into CGLIB and JDK.
3. How to use it?
3.1 Static Proxy:
|
3.2 JDK Dynamic Proxy
|
3.3 Cglib Dynamic Proxy
|
4, the difference between
The name of the | note |
---|---|
Static agent | Simple, proxy mode is the theoretical basis of dynamic proxy. Commonly used in proxy mode |
JDK dynamic proxy |
The mapper file of Mybatis is a proxy file. The mapper file of Mybatis is a proxy file. Use reflection to do this. Dynamic bytecode generation technology is used. |
Cglib dynamic proxy | Classes can be proxyed directly, using bytecode technology, and final classes cannot be inherited. Dynamic bytecode generation technology is used. |
5. How is the bottom layer implemented?
We all know that dynamic proxies generate bytecode using dynamic bytecode generation technology, so what is the difference between the generated bytecode? What about the generated bytecode? How to generate bytecode?
5.1 JDK underlying implementation
The bytecode is finally generated by this line,
|
NewProxyInstance (ClassLoader paramClassLoader, Class
[] paramArrayOfClass, paramInvocationHandler, paramInvocationHandler) First get his constructor with class, then use constructor reflection to get an instance of him.
The ones in red are the most complicated. However, the cglib implementation principle is basically the same, the only difference is that the new class file generation method and results are different.
5.2 Cglib underlying implementation
Directly on the final bytecode generation code. It is important to note that this statement must be implemented after cglib dynamic proxy code, otherwise there is no instance of DefaultGeneratorStrategy.
|
Tracking enhancer. The create (); This code looks at how the bytecode is generated and converted into a class. The process is basically the same as JDK, with the main difference being the difference in bytecode generation.
5.3 summary
In the process of generating code here, caching is used. The JDK uses weakReference reference, while Cglib uses WeakHashMap directly, basically similar.
6. What exactly does the generated bytecode look like?
6.1 additional
|
6.2 the JDK
|
Cglib directly inherits the Factory interface, while the JDK implements its own top-level interface, inheriting the Proxy interface. It’s important to note that the JDK won’t be able to find its implementation by class because its implementation class is actually a Proxy class, not itself.
See the next section for more general meaning of this sentence.
7. What kind of proxy is used in Spring?
If a class has a top-level interface, the JDK dynamic proxy is used by default. If a class is directly a class, the Cglib dynamic proxy is used. Second, methods that do not need to be proxied, such as Aop, will not be proxied if they do not have the @Transactional annotation.
The problem is, how can we convince people if we only say the conclusion without any basis?
|
In this case, we implement a Service, let it implement a top-level interface, and then we use it in the Controller
@Autowired
private TestServiceImpl testServiceImpl;
Annotation to inject, at this point you will find that the startup times is wrong.
The error is also obvious:
|
@autoWired private TestService testServiceImpl;
Can start normally.
Prove that the code generated by the dynamic proxy is a TestService but not a TestServiceImpl. You use the JDK’s dynamic proxy.
Remove the transaction annotations and remove the interface implementation here and try again.
8. Is that all?
Under The Spring architecture, most implementations use dynamic proxies, and we can learn from their excellent source code to write our own projects.
For example: Mybatis mapper agent, paging plug-in agent implementation. Spring Aop, transaction annotations, etc.