@Scope失效问题

单例的Bean注入其它类型的@Scope属性有可能会失效,针对失效的情况,我们可以采取四种解决办法。

一.失效场景

1.准备多例Bean

java 复制代码
package com.example.springdemo.demos.a27;

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

/**
 * @author zhou
 * @version 1.0
 * @description TODO
 * @date 2025/10/15 21:56
 */
@Scope("prototype")
@Component
public class F1 {
}

2.E中注入F1

java 复制代码
package com.example.springdemo.demos.a27;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Component;

/**
 * @author zhou
 * @version 1.0
 * @description TODO
 * @date 2025/10/15 21:56
 */
@Component
public class E {
    @Autowired
    private  F1 f1;
    public F1 getF1(){
        return f1;
    }
}

3.容器中获取对象

java 复制代码
package com.example.springdemo.demos.a27;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.ComponentScan;

/**
 * @author zhou
 * @version 1.0
 * @description TODO
 * @date 2025/10/15 21:54
 */
@ComponentScan("com.example.springdemo.demos.a27")
public class a27Application {
    private static final Logger log = LoggerFactory.getLogger(a27Application.class);

    public static void main(String[] args) {
        AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(a27Application.class);
        E e = context.getBean(E.class);
        log.info("{}",e.getF1().getClass());
        log.info("{}",e.getF1());
        log.info("{}",e.getF1());
        log.info("{}",e.getF1());

    }
}

结果:

通过依赖注入的F1对象多次获取后都是同一个,Scope作用失效了。分析其原因是对于单例对象E来讲,依赖注入仅仅发生了一次,后续再也没有使用多例的F1,所以多次获取都是同一个对象。

二.解决办法

下面提供四种解决方法。

1.添加@Lazy注解

java 复制代码
package com.example.springdemo.demos.a27;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Component;

/**
 * @author zhou
 * @version 1.0
 * @description TODO
 * @date 2025/10/15 21:56
 */
@Component
public class E {
    @Lazy
    @Autowired
    private  F1 f1;
    public F1 getF1(){
        return f1;
    }
}

结果每次获取出不同的F1对象。使用@Lazy注解之后,注入便是F1的代理对象(注意看输出的F1对象的类型),e.getF1()使用了F1代理类的toString(),而每次使用代理对象的方法都会创建新的F1对象。

2.在Scope属性中设置proxyMode

设置proxyMode的值为ScopedProxyMode.TARGET_CLASS,可以为F1对象生成代理对象,与上面方法类似。

java 复制代码
package com.example.springdemo.demos.a27;

import org.springframework.context.annotation.Scope;
import org.springframework.context.annotation.ScopedProxyMode;
import org.springframework.stereotype.Component;

/**
 * @author zhou
 * @version 1.0
 * @description TODO
 * @date 2025/10/15 21:56
 */
@Scope(value = "prototype",proxyMode = ScopedProxyMode.TARGET_CLASS)
@Component
public class F1 {
}

3.使用ObjectFactory包装F1

java 复制代码
package com.example.springdemo.demos.a27;

import org.springframework.beans.factory.ObjectFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Component;

/**
 * @author zhou
 * @version 1.0
 * @description TODO
 * @date 2025/10/15 21:56
 */
@Component
public class E {
    @Autowired
    private ObjectFactory<F1> f1;
    public F1 getF1(){
        return f1.getObject();
    }
}

通过这种方式每次获取F1,工厂都可以生成多例对象。

4.注入ApplicationContext容器

每次从容器中获取F1对象自然是多例的。

java 复制代码
package com.example.springdemo.demos.a27;

import org.springframework.beans.factory.ObjectFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Component;

/**
 * @author zhou
 * @version 1.0
 * @description TODO
 * @date 2025/10/15 21:56
 */
@Component
public class E {
    @Autowired
    private ApplicationContext context;
    public F1 getF1(){
        return context.getBean(F1.class);
    }
}
相关推荐
白萤4 小时前
SpringBoot用户登录注册系统设计与实现
java·spring boot·后端
淘晶驰AK4 小时前
主流的 MCU 开发语言为什么是 C 而不是 C++?
c语言·开发语言·单片机
流星白龙6 小时前
【Qt】1.安装QT
开发语言·qt
励志不掉头发的内向程序员6 小时前
【Linux系列】解码 Linux 内存地图:从虚拟到物理的寻宝之旅
linux·运维·服务器·开发语言·学习
摇滚侠7 小时前
Spring Boot 3零基础教程,创建第一个 Spring Boot 3 应用,Spring Boot 3 外部配置,笔记03
java·spring boot·笔记
superxxd8 小时前
跨平台音频IO处理库libsoundio实践
开发语言·qt·音视频
没有bug.的程序员10 小时前
服务网格 Service Mesh:微服务通信的终极进化
java·分布式·微服务·云原生·service_mesh
_OP_CHEN10 小时前
C++基础:(十二)list类的基础使用
开发语言·数据结构·c++·stl·list类·list核心接口·list底层原理