Skip to content

@Lazy injection point is resolved from BeanFactory for every invocation on proxy #28173

@kevin-wang001

Description

@kevin-wang001

I found that, when I use @Lazy on @Resource fields, Spring injects a lazy proxy.

The lazy proxy's TargetSource is not static, so every time I invoke a method on the lazy proxy, it calls beanFactory.resolveDependency() to resolve the target object.

Why doesn't Spring set the lazy proxy's TargetSource to static?

If so, it will be faster when invoking the lazy proxy's methods.

For example, in CommonAnnotationBeanPostProcessor:

protected Object buildLazyResourceProxy(final LookupElement element, final @Nullable String requestingBeanName) {
    TargetSource ts = new TargetSource() {
        @Override
        public Class<?> getTargetClass() {
            return element.lookupType;
        }
        @Override
        public boolean isStatic() {
            // this place: why not use "true"?
            return false;
        }
        @Override
        public Object getTarget() {
            return getResource(element, requestingBeanName);
        }
        @Override
        public void releaseTarget(Object target) {
        }
    };

// ...

} 

Metadata

Metadata

Assignees

Labels

in: coreIssues in core modules (aop, beans, core, context, expression)status: declinedA suggestion or change that we don't feel we should currently apply

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions