-1

Im creating a application in java SE witht spring framework. I have defined some frames in my application context:

ApplicationContext actx = new FileSystemXmlApplicationContext(args[0]);

context.xml:

<bean id="NewRouteFrame" class="newRoute.frame.NewRouteFrame" 
      lazy-init="true" 
/>

<bean id="NewControllerFrame" class="newController.frame.NewControllerFrame" 
      lazy-init="true" 
/>

and sometimes i would like to create a new object-frame (destroy old frame and create new one).

Im using dispose method after window will closed (i dont want cache frames):

 setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

but spring still caching a frame. When i create normal frame using keyword

new MyJFrame();

disposing works correct. But throught spring context not!. I can set scope="prototype" in context.xml than spring will create new Frame, but old frame still existsing resulting many same frames. How it is possible to destroy object?. I would like to destroy object from spring context using listener close window operation for example:

addWindowListener(new java.awt.event.WindowAdapter() {
        @Override
        public void windowClosing(java.awt.event.WindowEvent e) {
            // here destroy bean from context, but how???
        }
    });

}

I will great full for help in this issue

UPDATE

i not use @Authowire annotation. I get simple beans using:

    appContext.getBean(MyFrame.class);

My problem is that i don`t know how to delete already existing object (frame) in appContext and after this again call new MyJFrame (fresh) object. How i wrote above i can use scope="prototype" but after this i get alot of same frames. So this is not satisfity me

user3719630
  • 83
  • 2
  • 9
  • I think you can find answer [here](http://stackoverflow.com/questions/6855811/how-can-i-remove-a-singleton-spring-bean-from-applicationcontext) – terma Jul 20 '14 at 22:52

3 Answers3

1

Best way to reslove your problem will be not removing bean from application context but use JDialog instead JFrame.

Resulting that, user cannot focus other opened frames (where exacly u have implemented buttons to open actual frame) and cannot create new JFrame until not close actual focused (opened frame).

Example: User open you NewRouteFrame (but JDialog implementation) than close it, and than want again open same frame, than spring give new NewRouteFrame (JDialog implementation) for user. Now user want open same frame (not closing actual opened) but he cannot (JDialog blocks focusable other frames in application) until not close actual frame.

For more information about JDialog go to:

http://docs.oracle.com/javase/tutorial/uiswing/components/dialog.html 
Michał Ziembiński
  • 1,124
  • 2
  • 10
  • 31
0

I can set scope="prototype" in context.xml than spring will create new Frame, but old frame still existing resulting many same frames. How it is possible to destroy object?

When you use prototype-scope, it is up to you to dispose of the object. Spring will not and cannot dispose of prototype-scoped beans for you. When you call getBean to retrieve a prototype-scoped bean, you need to retain a reference to that object and dispose of it correctly.

skaffman
  • 398,947
  • 96
  • 818
  • 769
0

You can use prototype scope and Spring's destroy-method configuration to tell Spring to call a specific method when it is done with the object. The method must be defined in that class.

See: http://www.mkyong.com/spring/spring-init-method-and-destroy-method-example/

Example:

 <bean id="NewControllerFrame" class="newController.frame.NewControllerFrame" 
      scope="prototype" destroy-method="cleanup" />

If it doesn't exist in that class, and you can't change it, you'll have to write a proxy object. Example:

public class MyJFrameProxy extends JFrame
{
    private MyJFrame proxiedObject;

    public void setVisible(boolean visible) 
    {
       proxiedObject.setVisible(visible);
    }

    public void show()
    {
       proxiedObject.show();
    }

    public void cleanup() 
    {
       proxiedObject.setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
    }
 }

You can then pool those proxies, if you like, using Spring object pooling.

Brad
  • 2,261
  • 3
  • 22
  • 32