Announcement Announcement Module
Collapse
No announcement yet.
JSF and spring annotated managed bean Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • JSF and spring annotated managed bean

    Lets say that I have managed bean having property that is spring bean. I know there is spring specific variable resolver and that works just great but how about using annotations instead of xml and el in faces-config.xml. What I mean is I want to use for example @Autowired annotations in managed beans. Is there any work around or plans to get this working in future. I like the way Jboss Seam is doing this but at the moment I don't want to use that framework.

  • #2
    JSF InjectionProvider

    I got this working by making class implementing com.sun.faces.spi.InjectionProvider interface and using there AutowireCapableBeanFactory class. Configuration can be done for example adding context param in web.xml.

    Example:

    <context-param>
    <param-name>com.sun.faces.injectionProvider</param-name>
    <param-value>test.SpringProvider</param-value>
    </context-param>

    I see this could be in spring-web module by default because I think this way is more better than configuring spring specific el resolver.

    Comment


    • #3
      Hi There,
      I have similar problem. Would you mind sharing your solution i.e.
      • Do i need to download any jar file to use JSFInjectionProvider?
        Does it work with JSF 1.2 amd MyFaces 1.3 ?
        What is "test.SpringProvider" you are refering in <context-param> tage of web.xml file

      Thanks for your help

      Comment


      • #4
        Sample

        Hi,

        This works only with reference implementation. You could also use Spring AspectJ features. If interested go to http://dev.itmill.com/wiki/Articles/SpringIntegration and read end of that article (starting More Simple Example with Spring 2.5.x). I wrote it there some time ago.

        Anyway here is an example what you asked for:
        Code:
        package test;
        
        import javax.faces.context.FacesContext;
        import javax.servlet.ServletContext;
        
        import org.apache.commons.logging.Log;
        import org.apache.commons.logging.LogFactory;
        import org.springframework.beans.factory.config.AutowireCapableBeanFactory;
        import org.springframework.web.context.WebApplicationContext;
        import org.springframework.web.context.support.WebApplicationContextUtils;
        
        import com.sun.faces.spi.InjectionProvider;
        import com.sun.faces.spi.InjectionProviderException;
        import com.sun.faces.vendor.WebContainerInjectionProvider;
        
        public class SpringInjectionProvider implements InjectionProvider {
        
        	private static final WebContainerInjectionProvider con = new WebContainerInjectionProvider();
        	private static final Log logger = LogFactory.getLog(SpringInjectionProvider.class);
        	
        	public void inject(Object managedBean) throws InjectionProviderException {
        		if(logger.isDebugEnabled()) {
        			logger.debug("inject: " + managedBean.getClass());
        		}
        		con.inject(managedBean);
        	}
        
        	public void invokePostConstruct(Object managedBean)
        			throws InjectionProviderException {
        		if(logger.isDebugEnabled()) {
        			logger.debug("invokePostConstruct: " + managedBean.getClass());
        		}
        		ServletContext context = (ServletContext)FacesContext.getCurrentInstance().getExternalContext().getContext();
        		if(logger.isDebugEnabled()) {
        			logger.debug("context: " + context);
        		}
        		WebApplicationContext wCtx = WebApplicationContextUtils.getWebApplicationContext(context);
        		if(logger.isDebugEnabled()) {
        			logger.debug("spring context: " + wCtx);
        		}
        		if(wCtx != null) {
        			if(logger.isDebugEnabled()) {
        				logger.debug("trying to autowire");
        			}
        			try {
        				wCtx.getAutowireCapableBeanFactory().autowireBeanProperties(managedBean, AutowireCapableBeanFactory.AUTOWIRE_AUTODETECT, false);
        			} catch(Exception ex)  {
        				logger.error("Autowire exception", ex);
        			}
        		}
        		con.invokePostConstruct(managedBean);
        	}
        
        	public void invokePreDestroy(Object managedBean) throws InjectionProviderException {
        		if(logger.isDebugEnabled()) {
        			logger.debug("invokePreDestroy: " + managedBean.getClass());
        		}
        		con.invokePreDestroy(managedBean);
        	}
        }

        Comment


        • #5
          You could also use Spring AOP and Configurable annotation. I think that's the best solution if you can use load time weaver on your application server.

          Here is an example I referenced earlier. It works only with reference impl.
          Code:
          package test;
          
          import javax.faces.context.FacesContext;
          import javax.servlet.ServletContext;
          
          import org.springframework.beans.factory.config.AutowireCapableBeanFactory;
          import org.springframework.web.context.WebApplicationContext;
          import org.springframework.web.context.support.WebApplicationContextUtils;
          
          import com.sun.faces.spi.InjectionProvider;
          import com.sun.faces.spi.InjectionProviderException;
          import com.sun.faces.vendor.WebContainerInjectionProvider;
          
          public class SpringInjectionProvider implements InjectionProvider {
          
          	private static final WebContainerInjectionProvider con = new WebContainerInjectionProvider();
          	
          	public void inject(Object managedBean) throws InjectionProviderException {
          		con.inject(managedBean);
          	}
          
          	public void invokePostConstruct(Object managedBean)
          			throws InjectionProviderException {
          		ServletContext context = (ServletContext)FacesContext.getCurrentInstance().getExternalContext().getContext();
          		WebApplicationContext wCtx = WebApplicationContextUtils.getWebApplicationContext(context);
          		if(wCtx != null) {
          			try {
          				wCtx.getAutowireCapableBeanFactory().autowireBeanProperties(managedBean, AutowireCapableBeanFactory.AUTOWIRE_AUTODETECT, false);
          			} catch(Exception ex)  {
          				ex.printStackTrace();
          			}
          		}
          		con.invokePostConstruct(managedBean);
          	}
          
          	public void invokePreDestroy(Object managedBean) throws InjectionProviderException {
          		con.invokePreDestroy(managedBean);
          	}
          }
          Last edited by hakalpet; Nov 30th, 2008, 03:34 AM.

          Comment

          Working...
          X