【问题标题】:Spring - prototype bean inside a singleton beanSpring - 单例 bean 中的原型 bean
【发布时间】:2017-08-03 08:25:06
【问题描述】:

Spring 具有 bean 类型/范围,例如
-singleton bean(每个应用程序上下文只有一个 bean)
-prototype bean(每个请求一个新的 bean)

现在,如果在单例 bean 中有对原型 bean 的引用,有没有办法在对单例 bean 的每个请求时获取一个新的原型 bean(在单例 bean 中)。
如果是,配置是什么样的?

【问题讨论】:

  • 你为什么需要这个?

标签: java spring spring-bean


【解决方案1】:

有这样一种查找方法:

import org.springframework.beans.factory.annotation.Lookup;
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

public final class Main {

    public static void main(String[] args) {
        ApplicationContext applicationContext = new AnnotationConfigApplicationContext(Config.class);
        for (int i = 0; i < 10; i++) {
            System.out.println(i + ".- call: " + applicationContext.getBean(Singleton.class));
        }
    }

    @ComponentScan("foo")
    @Configuration
    public static class Config {

    // It's important to define SingletonBase component with @Component annotation and not here, If you define SingletonBase initialization here, It'll not work!

        @Bean
        @Scope(BeanDefinition.SCOPE_PROTOTYPE)
        public Prototype prototype() {
            return new PrototypeBase();
        }
    }

    public interface Prototype {

        public long getInstanceId();
    }

    public interface Singleton {

        public Prototype getPrototype();
    }

    // It's important define SingletonBase component as this, If you define SingletonBase initialization inside a factory it'll not work!
    @Component
    public static class SingletonBase implements Singleton {

        private static long instanceIdGenerator = 1L;
        private long instanceId = generateId();

        private static synchronized long generateId() {
            return instanceIdGenerator++;
        }

        public SingletonBase() {
            System.out.println("Singleton initialized!");
        }

        @Override
        public String toString() {
            return "SingletonBase{" + "instanceId=" + instanceId + ", prototypeId=" + getPrototype().getInstanceId() + '}';
        }

        @Override
        @Lookup
        public Prototype getPrototype() {
            return null;
        }
    }

    public static class PrototypeBase implements Prototype {

        private static long instanceIdGenerator = 1L;
        private long instanceId = generateId();

        private static synchronized long generateId() {
            return instanceIdGenerator++;
        }

        public PrototypeBase() {
            System.out.println("Prototype initialized!");
        }

        public long getInstanceId() {
            return instanceId;
        }
    }
}

打印出来:

Singleton initialized!
Prototype initialized!
0.- call: SingletonBase{instanceId=1, prototypeId=1}
Prototype initialized!
1.- call: SingletonBase{instanceId=1, prototypeId=2}
Prototype initialized!
2.- call: SingletonBase{instanceId=1, prototypeId=3}
Prototype initialized!
3.- call: SingletonBase{instanceId=1, prototypeId=4}
Prototype initialized!
4.- call: SingletonBase{instanceId=1, prototypeId=5}
Prototype initialized!
5.- call: SingletonBase{instanceId=1, prototypeId=6}
Prototype initialized!
6.- call: SingletonBase{instanceId=1, prototypeId=7}
Prototype initialized!
7.- call: SingletonBase{instanceId=1, prototypeId=8}
Prototype initialized!
8.- call: SingletonBase{instanceId=1, prototypeId=9}
Prototype initialized!
9.- call: SingletonBase{instanceId=1, prototypeId=10}   

【讨论】:

  • 很好,这比我的回答要好。
【解决方案2】:

在单例bean中注入ApplicationContext,使用getBean方法获取原型。

@Autowired
private ApplicationContext ctx;

public void request() {
    MyProptotypeBean mpb = this.ctx.getBean(MyProptotypeBean.class);//new instance for evety call to the method
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-01-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-04-11
    • 2014-09-19
    • 2013-02-09
    • 2013-06-14
    相关资源
    最近更新 更多