How to use Spring services in JSF-managed beans?

后端 未结 1 1492
暗喜
暗喜 2020-12-20 06:05

JSF is very popular technology in Java world, however, cooperation with Spring is still painfull and requires \'nasty\' hacks. I have currently the problem with one of this

相关标签:
1条回答
  • 2020-12-20 06:51

    I experienced a lot of issues with org.springframework.web.jsf.el.SpringBeanFacesELResolver, too. Mostly related to unmatching object scopes (Spring has no equivalent to JSF's view scope and conversation scope). Some people also complain about serialization problems.

    I successfully applied the solution proposed in this article: http://www.beyondjava.net/blog/integrate-jsf-2-spring-3-nicely/.

    In my case, serialization, was not a problem and I was only concerned with bean scopes. I wished JSF to fully manage the backing beans lifecycle without interference with Spring beans lifecycle.

    I made JSF managed beans to load the Spring context and autowire themselves to access Spring-managed beans from the JSF context.

    I developed the following JSF bean superclass:

    public abstract class AutowireableManagedBean {
    
        protected AutowireCapableBeanFactory ctx;
    
        @PostConstruct
        protected void init() {
            logger.debug("init");
            ctx = WebApplicationContextUtils
                    .getWebApplicationContext(
                            (ServletContext) FacesContext.getCurrentInstance()
                                    .getExternalContext().getContext())
                    .getAutowireCapableBeanFactory();
            // The following line does the magic
            ctx.autowireBean(this);
        }
       ...
    }
    

    Then, my concrete JSF backing beans looked like this (I was able to use view scope without problems):

    @ManagedBean
    @ViewScoped
    public class MyBackingBean extends AutowireableManagedBean {
    
        @Autowired
        private MyDao myDao;
    
    0 讨论(0)
提交回复
热议问题