Acessar DAO através do converter JSF com Spring

1 resposta
PaduaAlves

Pessoal, estou desenvolvendo um sisteminha pra estudasr JSF, Spring e JPA. Tenho um SelectOneMenu que é populado através do banco. Consigo pegar a lista e preencher o SelectOneMenu, o problema está no meu método getAsObject do converter. Dentro desse método preciso ter acesso ao meu DAO para fazer uma consulta através do id retornado da tela e montar o meu objeto. O problema é que meu DAO é gerenciado pelo spring e eu não consigo injeta-lo no converter. Como eu posso resolver meu problema?

1 Resposta

R

Crie essas duas classes:

package context; 
import org.springframework.context.ApplicationContext; 

/**
 * This class provides application-wide access to the Spring ApplicationContext.
 * The ApplicationContext is injected by the class "ApplicationContextProvider".
 *
 * @author Siegfried Bolz
 */ 

public class AppContext { 
	private static ApplicationContext ctx; 

	/** 
	 * Injected from the class "ApplicationContextProvider" which is automatically
	 * loaded during Spring-Initialization.
	 */ 
	public static void setApplicationContext(ApplicationContext applicationContext) { 
		ctx = applicationContext; 
	} 

	/**
	 * Get access to the Spring ApplicationContext from everywhere in your Application.
	 *
	 * @return
	 */ 
	public static ApplicationContext getApplicationContext() { 
		return ctx; 
	} 

}
package context; 
   
       
   
    import org.springframework.beans.BeansException; 
   
      import org.springframework.context.ApplicationContext; 
   
      import org.springframework.context.ApplicationContextAware; 
   
       
   
      /**
   
       * This class provides an application-wide access to the
   
       * Spring ApplicationContext! The ApplicationContext is
  
       * injected in a static method of the class "AppContext".
  
       *
  
       * Use AppContext.getApplicationContext() to get access
  
    * to all Spring Beans.
  
       *
  
       * @author Siegfried Bolz
  
       */ 
  
      public class ApplicationContextProvider implements ApplicationContextAware { 
    
          public void setApplicationContext(ApplicationContext ctx) throws BeansException { 
  
              // Wiring the ApplicationContext into a static method 
  
              AppContext.setApplicationContext(ctx); 
  
          } 
  
      } // .EOF

Declare no application-context.xml:

<bean id="contextApplicationContextProvider" class="context.ApplicationContextProvider" />

Exemplo de uso:

ApplicationContext ctx = AppContext.getApplicationContext();  
MeuBean bean = (MeuBean) ctx.getBean("meuBean");
Criado 21 de fevereiro de 2011
Ultima resposta 21 de fev. de 2011
Respostas 1
Participantes 2