2011-10-09 1 views
1

백 엔드에 경량 HTTP 서버 (Jetty)를 배포하는 응용 프로그램이 있습니다.
서블릿은 기본적으로 MySQL 데이터베이스를 업데이트합니다.
나는 스프링을 사용하여 모든 것을 연결했다.lazy-init = "true"입니다. 이유는 무엇입니까? 왜 lazy-init을 true로 설정하지 않고 Swing 앱에서 Spring 빈을 얻을 수 없습니까?

지금까지 확인해보세요.

나는 서버와 상호 작용하는 데 사용할 UI가 있습니다.
UI가 로컬에서 실행되는 경우 현재 로그인 한 사용자 인 JTree에 표시하려고합니다.

그래서 메인 프레임을 시작할 때 서버를 시작하기로 결정했습니다.

그 외에도 Observer-Observable을 사용하고 로그인을 허용하는 코드를 가지고있는 모든 로그인 사용자를 표시하기 위해 UI의 JTree를 업데이트하려면 Observable이 필요합니다.

ConnectionListener

SwingUtilities 통해 물론 EDT 스레드 -in를 업데이트 것이다 Jtree의합니다 ( DefaultMutableTreeNode 루트에 대한 참조를 갖는)을 ArrayBlockingQueue 및 배경 스레드에 저장 들어오는 접속 통지된다.

내 문제는 다음과 같은 : 내가 할 경우

:

public class AdminFrame extends JFrame { 
     public static ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext("myBeans");  

public static void main(String[] args){  
     EventQueue.invokeLater(new Runnable() { 
       public void run() { 
     //Show GUI 
     } 
     } 
    } 

GUI를 그리는 것이 아니라, 내가 번부터 봄이 시작하는 것이 이해할 수있는 서버가 반환하지 않는 스레드를 시작합니다. 다음과 같이

그래서 나는 백 엔드 스레드에서 봄을 시작합니다

public class ServerThread extends Thread { 
    @Override 
    public void run() { 
    ClassPathXmlApplicationContext ctx = ApplicationCtx.getApplicationCtx(); 
    } 
} 

public class ApplicationCtx { 

    private static ClassPathXmlApplicationContext ctx; 
    private ApplicationCtx(){} 
    public static synchronized ClassPathXmlApplicationContext getApplicationCtx(){ 
    if(ctx == null){ 
    ctx = new ClassPathXmlApplicationContext("myBeans.xml"); 
    } 
    return ctx; 
} 

}

GUI는 지금 표시를하고 확인을 보인다하지만 나는 실종 공고.

문제 :

1) 내가 통지를 등록하면 다음과 같습니다 :

public class AdminFrame extends JFrame { 
    public static void main(String[] args) { 
     ServerThread lightweightServer = new ServerThread(); 
     lightweightServer.start(); 
     Thread.sleep(9000);//Temporary solution to make sure the server is started up before GUI 
     ConnectHandler connectHandler = (ConnectHandler) ApplicationContext.getApplicationCtx().getBean("connectHandler"); 
     connectHandler.addObserver(new ConnectionListener()); 
     EventQueue.invokeLater(new Runnable() { 
     public void run() { 
       //SHOW GUI 
       } 
     } 

그래픽 사용자 인터페이스가 표시되지 않습니다 새로운 연결에 대한 알림을받을 수 없습니다

. 회선을 삭제하면 :

ConnectHandler connectHandler = (ConnectHandler) ApplicationContext.getApplicationCtx().getBean("connectHandler"); 

GUI가 나타납니다.

2) 내가 EDT 내부에 등록한 경우, 즉 AdminFrame의 생성자 안에 GUI가 표시되지 않습니다.
즉,나는 다음 줄을 제거하면

public class AdminFrame extends JFrame { 
    public static void main(String[] args) { 
     ServerThread lightweightServer = new ServerThread(); 
     lightweightServer.start(); 
     Thread.sleep(9000);//Temporary solution to make sure the server is started up before 
     EventQueue.invokeLater(new Runnable() { 
      public void run() { 

     AdminFrame frame = new AdminFrame(); 
     //Other code 

     } 

는 다시 GUI가 표시되지만 내가 등록 할 수 없습니다 이런 식으로 알림을받을 :

ConnectHandler connectHandler = (ConnectHandler) ApplicationContext.getApplicationCtx().getBean("connectHandler"); 

이 스레드 문제가 있어야하지만 난 무엇을 이해할 수 없다 문제.
ConnectHandler 스레드를 반환하지 않는 Spring 응용 프로그램 컨텍스트에 대한 두 번째 호출이 왜 발생합니까?
여기서 내가 뭘 잘못하고 있니?


업데이트 : 나는 부두 서버를 시작하는 빈에 Spring 설정 파일의 속성 lazy-init="true"를 추가하면 문제가 해결

.
그러나 나는 그것이 그것을 해결하는 이유 또는 무엇이 문제인지 이해할 수 없습니다.

감사

답변

1
  • Serializable 또는 Observate 필요한 wraping의 GUI는 (대부분의 경우)에 코드를 rellated invokeAndWait()

  • 출력에서 ​​SwingWorker (보장하지만 난 등의 행위에서 난 것이 아니니 기대 경우를 보았다 때문에) 또는 Runnable#Thread GUI 관련 코드를 강요하는 데 필요합니다. invokeLater()

  • ca GUI를 준비하고 (거기에 EDT가 존재합니다), 가시성은

입니다.

JFrame#pack(); 
JFrame#setVisible(true); 

. 당신이 invokeXxx(); 내부 호출하거나 게으른 초기화를 설정하는 것이 진정한 문제를 해결하기 위해 경우에 가장 javax.swing.Action()

+0

나는이 문제를 안다. 나는 왜'ConnectHandler connectHandler = (ConnectHandler) ApplicationContext.getApplicationCtx(). getBean'이 GUI가 나타나지 않게하는지 이해하지 못한다. EDT.I 빈을 얻고 싶습니다. Spring 컨텍스트가 EDT 외부에서 초기화 된 경우 중요합니까? Spring은 GUI와 관련이 없으며 주로 백엔드의 Jetty 서버를 연결합니다. – Cratylus

+0

@ user384706 일부 코드는 스트림, 연결, unclipped 놓친 스트림, 파일, 연결에 대한 닫히지 않은 및 I/O 수준에서 여전히 활성, 또는 초기화되지 않은 클래스를 호출하는 실수, null, 다음 GUI null, 솔루션 SwingExplorer , 무엇이든 초기화되지 않으면 JProfiler, 많은 행운을 누린다. – mKorbel

+0

'lazy-init' 관찰은 어떨까요?'lazy-init'을 false로 설정하면 코드가 제대로 작동합니다. 게시물 – Cratylus

0

에서 것이 Container에 대한

, 난 그 완전히 해결되지 생각합니다.

기본적으로 모든 Bean은 시작시 초기화되므로 Bean이 올바르게 초기화되지 않은 경우 시작할 때 문제가 발생합니다.

false로 설정하면 bean이 호출되지 않고 어떤 issuce도 처리하지 않습니다. 따라서 문제는 해결되지 않지만 연기됩니다.

관련 문제