美文网首页
单例模式

单例模式

作者: 原来_1361 | 来源:发表于2019-04-08 14:32 被阅读0次

spring单例模式

spring的默认bean注入为单例,以下通过简单的例子进行测试:

  1. 创建2个类,分别为singleton单例和prototype类型
@Component
@Scope(ConfigurableListableBeanFactory.SCOPE_SINGLETON)// 默认为单例,这里可以不写
public class SingletonUser {
    private String name;
    // 省略getter/setter方法...
}

protype类型的user

@Component
@Scope(ConfigurableListableBeanFactory.SCOPE_PROTOTYPE)
public class PrototypeUser {

    private String name;
    // 省略getter/setter方法...
}

  1. 通过不同service进行注入,改变其值
@Service
public class UserService1 {

    @Autowired
    private PrototypeUser prototypeUser;
    @Autowired
    private SingletonUser singletonUser;

    public void testProto(){
        prototypeUser.setName("李四");
        System.out.println(prototypeUser.getName());
    }

    public void testSingleton(){
        singletonUser.setName("X");
        System.out.println(singletonUser.getName());
    }
    public PrototypeUser getPrototypeUser() {
        return prototypeUser;
    }
    public SingletonUser getSingletonUser() {
        return singletonUser;
    }
}

userService2

@Service
public class UserService2 {

    @Autowired
    private PrototypeUser prototypeUser;
    @Autowired
    private SingletonUser singletonUser;

    public void testProto(){
        prototypeUser.setName("张三");
        System.out.println(prototypeUser.getName());
    }

    public void testSingleton(){
        singletonUser.setName("Y");
        System.out.println(singletonUser.getName());
    }
    public PrototypeUser getPrototypeUser() {
        return prototypeUser;
    }
    public SingletonUser getSingletonUser() {
        return singletonUser;
    }
}

  1. 编写测试类
@RunWith(SpringRunner.class)
@SpringBootTest
public class SpringbootThreadApplicationTests {
    @Test
    public void contextLoads() {
    }
    @Autowired
    private UserService1 userService1;
    @Autowired
    private UserService2 userService2;
    /**
     * 测试单例
     */
    @Test
    public void testUser(){
        userService1.testProto();
        userService2.testProto();
        userService1.testSingleton();
        userService2.testSingleton();
        /**
         * 为false,说明是不同对象,多例
         */
        System.out.println(userService1.getPrototypeUser().equals(userService2.getPrototypeUser()));
        /**
         * 为true,说明是同一个对象,单例
         */
        System.out.println(userService1.getSingletonUser().equals(userService2.getSingletonUser()));
    }
}

运行结果:

李四
张三
X
Y
false
true

结论:

  1. 可以对注入的对象进行赋值;
  2. spring默认采用的注入对象是单例的,即userService1和userService2两个实例得到的singletonUser是相同的,但是得到的prototypeUser 确实不同的。

相关文章

  • 【设计模式】单例模式

    单例模式 常用单例模式: 懒汉单例模式: 静态内部类单例模式: Android Application 中使用单例模式:

  • Android设计模式总结

    单例模式:饿汉单例模式://饿汉单例模式 懒汉单例模式: Double CheckLock(DCL)实现单例 Bu...

  • 2018-04-08php实战设计模式

    一、单例模式 单例模式是最经典的设计模式之一,到底什么是单例?单例模式适用场景是什么?单例模式如何设计?php中单...

  • 设计模式之单例模式详解

    设计模式之单例模式详解 单例模式写法大全,也许有你不知道的写法 导航 引言 什么是单例? 单例模式作用 单例模式的...

  • Telegram开源项目之单例模式

    NotificationCenter的单例模式 NotificationCenter的单例模式分析 这种单例模式是...

  • 单例模式Java篇

    单例设计模式- 饿汉式 单例设计模式 - 懒汉式 单例设计模式 - 懒汉式 - 多线程并发 单例设计模式 - 懒汉...

  • IOS单例模式的底层原理

    单例介绍 本文源码下载地址 1.什么是单例 说到单例首先要提到单例模式,因为单例模式是单例存在的目的 单例模式是一...

  • 单例

    iOS单例模式iOS之单例模式初探iOS单例详解

  • 单例模式

    单例模式1 单例模式2

  • java的单例模式

    饿汉单例模式 懒汉单例模式

网友评论

      本文标题:单例模式

      本文链接:https://www.haomeiwen.com/subject/jawuiqtx.html