Java SpringCacheManager不是singleton?

Java SpringCacheManager不是singleton?,java,spring,inversion-of-control,Java,Spring,Inversion Of Control,我对spring注释ConfigApplicationContext有一些问题 主要目的是创建一个Spring配置,它可以在应用服务器上运行,也可以独立运行。从同一AppServer上的另一个应用程序使用的子任务生成子文本。 但是在cacheManager上有一些问题 这是我的密码: 又名AbstractConfig package org.zib.test.a; import org.springframework.cache.CacheManager; import org.spring

我对spring注释ConfigApplicationContext有一些问题

主要目的是创建一个Spring配置,它可以在应用服务器上运行,也可以独立运行。从同一AppServer上的另一个应用程序使用的子任务生成子文本。 但是在cacheManager上有一些问题

这是我的密码: 又名AbstractConfig

package org.zib.test.a;


import org.springframework.cache.CacheManager;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.cache.concurrent.ConcurrentMapCache;
import org.springframework.cache.support.SimpleCacheManager;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.Arrays;


@Configuration
@EnableCaching
public class ConfigA {

    @SuppressWarnings("UnusedDeclaration")
    public static ApplicationContext getContext() {
        return ContextA.getInstance().getContext();
    }


/**
 * use {@link org.zib.test.b.ContextB}
 */
public static <T> T getBean(Class<T> bean) {
    return ContextB.getInstance().getContext().getBean(bean);
}
/**
 * use {@link ContextB}
 */
public static <T> T getBean(String name, Class<T> bean) {
    return ContextB.getInstance().getContext().getBean(name, bean);
}

    @Bean(name = "cacheManager")
    public CacheManager cacheManager() {
        // configure and return an implementation of Spring's CacheManager SPI
        SimpleCacheManager cacheManager = new SimpleCacheManager();
        cacheManager.setCaches(Arrays.asList(new ConcurrentMapCache("default")));
        return cacheManager;
     }

 }
模式配置(例如WebLogicConfig)

和最终-模块配置:

package org.zib.test.c;


import org.springframework.cache.CacheManager;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.cache.concurrent.ConcurrentMapCache;
import org.springframework.cache.support.SimpleCacheManager;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.Arrays;


@Configuration
@EnableCaching
public class ConfigC {

    @SuppressWarnings("UnusedDeclaration")
    public static ApplicationContext getContext() {
        return ContextC.getInstance().getContext();
    }
    /**
     * use {@link ContextC}
     */
    public static <T> T getBean(Class<T> bean) {
        return ContextC.getInstance().getContext().getBean(bean);
    }

    /*DELTE BY SUGGESTION OF Tomasz Nurkiewicz
    @Bean(name = "cacheManager")
    public CacheManager cacheManager() {
        SimpleCacheManager cacheManager = new SimpleCacheManager();
        cacheManager.setCaches(Arrays.asList(new ConcurrentMapCache("default")));
        return cacheManager;
    }*/
}
我的测试班:

package org.zib.test;

import org.springframework.cache.CacheManager;
import org.zib.test.a.ConfigA;
import org.zib.test.c.ConfigC;

public class Test {
    public static void main(String[] args) {
        Object cm1 = ConfigC.getBean(CacheManager.class);
        Object cm2 = ConfigA.getBean(CacheManager.class);

        if (cm1 == cm2)
            System.out.println("equals");
        else
            System.out.println("unequal");
    }
}

我已经花了很多时间来解决这个问题-如果我正确理解
a
是一个有两个孩子的父上下文:
B
C
,如果有人能帮助我,我会很高兴的。定义
cacheManager
两次:在父
ConfigA
和一个子
ConfigC
中。不同的应用程序上下文可以有相同的bean,这就是为什么会有两个不同的实例

换句话说,如果您从
ConfigC
请求
cacheManager
,它将返回在该上下文中定义的。但是如果您从
ConfigB
ConfigA
请求它,它将返回
ConfigA
中的一个(其他实例)


它们仍然是单例,但在单个应用程序上下文的范围内。顺便说一句,你能描述一下你想用这个相当复杂的体系结构实现什么吗?

删除第二个cacheManager声明后同样的问题关于目标:我有一个模块化的解决方案。这个解决方案可以在应用服务器上执行,也可以独立执行(因此有不同的方法来实现某些对象,例如数据源等),但我在ConfigA中包含了这个解决方案的公共部分。之后,每个模块都有自己的特定bean配置。我同意这很复杂,但对我来说——非常符合逻辑
package org.zib.test.b;

import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.zib.test.a.ContextA;

public class ContextB {
    private ApplicationContext applicationContext;

    public static ContextB instance;

    public static ContextB getInstance() {
        synchronized (ContextB.class) {
            if (instance == null)
                instance = new ContextB();
        }
        return instance;
    }

    private ContextB() {
        AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext();
        applicationContext.register(ConfigB.class);
        applicationContext.setParent(ContextA.getInstance().getContext());
        applicationContext.registerShutdownHook();
        applicationContext.refresh();
        this.applicationContext = applicationContext;
    }

    public ApplicationContext getContext() {
        return applicationContext;
    }
}
package org.zib.test.c;


import org.springframework.cache.CacheManager;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.cache.concurrent.ConcurrentMapCache;
import org.springframework.cache.support.SimpleCacheManager;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.Arrays;


@Configuration
@EnableCaching
public class ConfigC {

    @SuppressWarnings("UnusedDeclaration")
    public static ApplicationContext getContext() {
        return ContextC.getInstance().getContext();
    }
    /**
     * use {@link ContextC}
     */
    public static <T> T getBean(Class<T> bean) {
        return ContextC.getInstance().getContext().getBean(bean);
    }

    /*DELTE BY SUGGESTION OF Tomasz Nurkiewicz
    @Bean(name = "cacheManager")
    public CacheManager cacheManager() {
        SimpleCacheManager cacheManager = new SimpleCacheManager();
        cacheManager.setCaches(Arrays.asList(new ConcurrentMapCache("default")));
        return cacheManager;
    }*/
}
package org.zib.test.c;

import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.zib.test.a.ContextA;
import org.zib.test.b.ContextB;

public class ContextC {
    private ApplicationContext applicationContext;

    public static ContextC instance;

    public static ContextC getInstance() {
        synchronized (ContextC.class) {
            if (instance == null)
                instance = new ContextC();
        }
        return instance;
    }

    private ContextC() {
        AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext();
        applicationContext.register(ConfigC.class);
        applicationContext.setParent(ContextA.getInstance().getContext());
        applicationContext.registerShutdownHook();
        applicationContext.refresh();
        this.applicationContext = applicationContext;
    }

    public ApplicationContext getContext() {
        return applicationContext;
    }
}
package org.zib.test;

import org.springframework.cache.CacheManager;
import org.zib.test.a.ConfigA;
import org.zib.test.c.ConfigC;

public class Test {
    public static void main(String[] args) {
        Object cm1 = ConfigC.getBean(CacheManager.class);
        Object cm2 = ConfigA.getBean(CacheManager.class);

        if (cm1 == cm2)
            System.out.println("equals");
        else
            System.out.println("unequal");
    }
}