java - Spring cacheManager is not singleton? -
i have problems spring annotationconfigapplicationcontext.
the main aim create spring configuration can run on application server or standalone. subtask create subcontext 1 used application on same appserver. have problem cachemanager.
that's code: aka 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() { homecoming contexta.getinstance().getcontext(); } /** * utilize {@link org.zib.test.b.contextb} */ public static <t> t getbean(class<t> bean) { homecoming contextb.getinstance().getcontext().getbean(bean); } /** * utilize {@link contextb} */ public static <t> t getbean(string name, class<t> bean) { homecoming contextb.getinstance().getcontext().getbean(name, bean); } @bean(name = "cachemanager") public cachemanager cachemanager() { // configure , homecoming implementation of spring's cachemanager spi simplecachemanager cachemanager = new simplecachemanager(); cachemanager.setcaches(arrays.aslist(new concurrentmapcache("default"))); homecoming cachemanager; } }
aka abstractcontext
package org.zib.test.a; import org.springframework.context.applicationcontext; import org.springframework.context.annotation.annotationconfigapplicationcontext; public class contexta { private applicationcontext applicationcontext; public static contexta instance; public static contexta getinstance() { synchronized (contexta.class) { if (instance == null) instance = new contexta(); } homecoming instance; } private contexta() { annotationconfigapplicationcontext applicationcontext = new annotationconfigapplicationcontext(); applicationcontext.register(configa.class); applicationcontext.registershutdownhook(); applicationcontext.refresh(); this.applicationcontext = applicationcontext; } public applicationcontext getcontext() { homecoming applicationcontext; } }
mode config (for example, weblogicconfig)
package org.zib.test.b; import org.springframework.cache.annotation.enablecaching; import org.springframework.context.applicationcontext; import org.springframework.context.annotation.*; import org.zib.test.a.configa; @configuration @enablecaching @import(configa.class) public class configb { @suppresswarnings("unuseddeclaration") public static applicationcontext getcontext() { homecoming contextb.getinstance().getcontext(); } /** * utilize {@link contextb} */ public static <t> t getbean(class<t> bean) { homecoming contextb.getinstance().getcontext().getbean(bean); } /** * utilize {@link contextb} */ public static <t> t getbean(string name, class<t> bean) { homecoming contextb.getinstance().getcontext().getbean(name, bean); } }
context:
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(); } homecoming 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() { homecoming applicationcontext; } }
and final - module config:
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() { homecoming contextc.getinstance().getcontext(); } /** * utilize {@link contextc} */ public static <t> t getbean(class<t> bean) { homecoming contextc.getinstance().getcontext().getbean(bean); } /*delte suggestion of tomasz nurkiewicz @bean(name = "cachemanager") public cachemanager cachemanager() { simplecachemanager cachemanager = new simplecachemanager(); cachemanager.setcaches(arrays.aslist(new concurrentmapcache("default"))); homecoming cachemanager; }*/ }
and context:
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(); } homecoming 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() { homecoming applicationcontext; } }
my test class:
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"); } }
i've spent lot of time solve problem - happy if help me
if understand correctly a
parent context 2 children: b
, c
. define cachemanager
twice: in parent configa
, in 1 kid configc
. different application contexts can have same beans, that's why 2 different instances.
in other words if inquire cachemanager
configc
, returns 1 defined in context. if inquire configb
or configa
, it'll homecoming 1 configa
(other instance).
they still singletons, within scope of single application context. btw can describe want accomplish pretty complex architecture?
java spring inversion-of-control
No comments:
Post a Comment