Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Example Analysis of Spring @ Lazy delay injection in Java

2025-04-07 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

Editor to share with you the example analysis of Spring @ Lazy delay injection in Java. I believe most people don't know much about it, so share this article for your reference. I hope you will learn a lot after reading this article. Let's take a look at it.

First, a simple small example

The code is as follows:

Servicepublic class NormalService1 {@ Autowired @ Lazy private MyService myService; public void doSomething () {myService.getName ();}}

The purpose is to delay loading. When NormalService1 carries out attribute injection, don't worry if MyService has not generated bean, it will inject a proxy, but in the actual run time, it will get the actual MyService in the Spring container. In some cases, this annotation is useful because of the spring life cycle.

Second, source code interpretation 1. Inject

The code is as follows (DefaultListableBeanFactory#resolveDependency):

Public Object resolveDependency (DependencyDescriptor descriptor, @ Nullable String requestingBeanName, @ Nullable Set autowiredBeanNames, @ Nullable TypeConverter typeConverter) throws BeansException {descriptor.initParameterNameDiscovery (getParameterNameDiscoverer ()); if (Optional.class = = descriptor.getDependencyType ()) {return createOptionalDependency (descriptor, requestingBeanName) } else if (ObjectFactory.class = = descriptor.getDependencyType () | | ObjectProvider.class = = descriptor.getDependencyType ()) {return new DependencyObjectProvider (descriptor, requestingBeanName) } else if (javaxInjectProviderClass = = descriptor.getDependencyType ()) {return new Jsr330Factory () .createDependencyProvider (descriptor, requestingBeanName) } else {/ / if @ Lazy is added to the injection attribute and lazy loading, spring will create a cglib proxy class Object result = getAutowireCandidateResolver () .getLazyResolutionProxyIfNecessary (descriptor, requestingBeanName) according to the specific type. If (result = = null) {result = doResolveDependency (descriptor, requestingBeanName, autowiredBeanNames, typeConverter);} return result;}

It is obvious that the getLazyResolutionProxyIfNecessary method will be executed, and if the @ Lazy annotation is added, the buildLazyResolutionProxy method will eventually be executed

Protected Object buildLazyResolutionProxy (final DependencyDescriptor descriptor, final @ Nullable String beanName) {Assert.state (getBeanFactory () instanceof DefaultListableBeanFactory, "BeanFactory needs to bea DefaultListableBeanFactory"); final DefaultListableBeanFactory beanFactory = (DefaultListableBeanFactory) getBeanFactory () TargetSource ts = new TargetSource () {@ Override public Class getTargetClass () {return descriptor.getDependencyType () } @ Override public boolean isStatic () {return false } @ Override public Object getTarget () {Object target = beanFactory.doResolveDependency (descriptor, beanName, null, null) / * * something valid * * / return target } @ Override public void releaseTarget (Object target) {}}; ProxyFactory pf = new ProxyFactory (); pf.setTargetSource (ts); Class dependencyType = descriptor.getDependencyType () If (dependencyType.isInterface ()) {pf.addInterface (dependencyType);} return pf.getProxy (beanFactory.getBeanClassLoader ());}

As you can see from the above code, a TargetSource is generated, then a proxy (CGLIB or JDK) is generated, and then injected into NormalService1 as a MyService object. So what does it mean to get the real MyService object only during execution?

two。 Use logic

The sample code in this article uses a CGLIB proxy, which is similar, because the injected MyService is a CGLIB proxy object, so the CglibAopProxy#DynamicAdvisedInterceptor#intercept method is called when the method is executed

So what is actually called here is the one above.

Object target = beanFactory.doResolveDependency (descriptor, beanName, null, null)

This method does not need to be taken seriously, the main function is to find MyService from the Spring container.

I have explained the @ Autowired principle and the @ Resource injection principle before. If you are not clear, you can see other articles in the column.

After taking it out, we will find that the target object we got is still an object of a CGLIB proxy.

Then when the method logic is executed

Because target is a CGLIB object, you go to the CglibAopProxy#DynamicAdvisedInterceptor#intercept method again.

The type of target object you get at this time is different.

It is the target object before our proxy. When we do invoke again at this time, we will carry on the general logic of dynamic proxy, first find all the advice that the method matches, then call it in turn, and finally invoke the execution of the method by target itself.

Summary

So you can find that @ Lazy only proxy the spring proxy object proxy again, but does not get the object when it is injected, but borrows the method invoke through the intercept method getTarget of proxy, and then makes the method call, delaying the injection of the object. After each call, you need to get the native proxy object from the Spring container.

The above is all the content of the article "sample Analysis of Spring @ Lazy delay injection in Java". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!

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.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report