Announcement Announcement Module
Collapse
No announcement yet.
two types of applicationcontexts Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • two types of applicationcontexts

    Hi!
    I have a major problem and I hope someone can help me!

    Requirements:
    I have a application where I need a back-end implementation and a front-end implementation (both in the same JVM). The back-end beans must be a singleton (in one JVM should be only one instance). The front-end beans have also a scope "singleton" but must be initialized more than one time in a JVM and have a dependency to a facade bean in the back-end.

    First try:
    I have two application contexts, and a create a new front-end application context for each time I need a new instance with a the back-end application context as parent application context.
    The Problem is that the back-end application context will be refreshed (so I have a new instance of it).

    The really last try I would think:
    The last way I would use, if no other way will be found, is to initialize the front-end application context not with a parent, but inject the back-end to the front-end after the application context is initialized.

    Question:
    Is there any better way to realize this scenario?

  • #2
    Here the stuff I tried:

    Code:
    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans"
    	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd">
    <bean id="backend" class="test.Backend"></bean>
    
    </beans>
    Code:
     <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans"
    	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd">
     <import resource="ac-backend.xml"/>
     
     <bean id="frontend" class="test.Frontend" >
       <property name="backend" ref="backend"></property>
     </bean>
    
     <bean id="mainBean" class="test.MainBean" >
       <property name="frontend" ref="frontend"></property>
     </bean>
    </beans>
    Code:
    public class Application {
      public static void main(String args[]) {
        ApplicationContext acb = new ClassPathXmlApplicationContext(new String[] {
            "test/ac-backend.xml"});
        ApplicationContext ac = new ClassPathXmlApplicationContext(new String[] {
            "test/applicationContext.xml"}, acb);
        
        MainBean mb = (MainBean) ac.getBean("mainBean");
        mb.doSomething();
        
        ac = new ClassPathXmlApplicationContext(new String[] {
            "test/applicationContext.xml"}, acb);
        mb = (MainBean) ac.getBean("mainBean");
        mb.doSomething();
      }
    }
    Result:
    MainBean: test.MainBean@d73c7a
    Frontend: test.Frontend@c39a20
    Backend: test.Backend@1bbd23f

    MainBean: test.MainBean@b25b9d
    Frontend: test.Frontend@19ce060
    Backend: test.Backend@4ecfdd


    You can see that the backend bean has a new id. :-(

    Comment


    • #3
      Take a look at SingletonBeanFactoryLocator.

      Comment


      • #4
        It looks as you have need in a custom scope for your front-end.

        Originally posted by tofu View Post
        Hi!
        I have a major problem and I hope someone can help me!

        Requirements:
        I have a application where I need a back-end implementation and a front-end implementation (both in the same JVM). The back-end beans must be a singleton (in one JVM should be only one instance). The front-end beans have also a scope "singleton" but must be initialized more than one time in a JVM and have a dependency to a facade bean in the back-end.

        First try:
        I have two application contexts, and a create a new front-end application context for each time I need a new instance with a the back-end application context as parent application context.
        The Problem is that the back-end application context will be refreshed (so I have a new instance of it).

        The really last try I would think:
        The last way I would use, if no other way will be found, is to initialize the front-end application context not with a parent, but inject the back-end to the front-end after the application context is initialized.

        Question:
        Is there any better way to realize this scenario?

        Comment


        • #5
          the problem is, that i have an existing application without spring. During refactoring I would bring in spring.
          I will look at the SinghletonBeanFactoryLocator - thanx dejanp!

          @al0:
          yes I need something like a session scope, but I cannot change the web.xml or something like that.The initialization of the application context will be done during loading a software module.

          Comment


          • #6
            I think I've get it!

            Important is that no import tag reference to the xml's of the other beanfactory.

            thats it

            thanx
            tofu

            Comment

            Working...
            X