【问题标题】:Spring managed JSF beans [duplicate]Spring管理的JSF bean [重复]
【发布时间】:2012-04-29 12:06:27
【问题描述】:

我正在使用 Spring 管理的 JSF bean,现在我必须使用注释标签 Ex.@scope of spring 或 JSF 吗?

我注意到作为 JSF 注释的 @ViewScoped 不起作用并且仍然表现为请求范围?

【问题讨论】:

    标签: spring jsf managed-bean


    【解决方案1】:

    如果您使用org.springframework.web.jsf.el.SpringBeanFacesELResolver 进行Spring + JSF 集成,则需要使用org.springframework.context.annotation.Scope 注释标记范围。

    【讨论】:

      【解决方案2】:

      Spring 中没有 View 作用域,但我们可以自定义实现这种作用域。参考thisthis

      【讨论】:

        【解决方案3】:

        阅读本文:本文信息:http://www.beyondjava.net/blog/integrate-jsf-2-spring-3-nicely/

        我这样做了:

        像这样为 JSF 支持 bean 定义一个抽象超类:

        public abstract class AutowireableManagedBean {
        
            protected Logger logger = LoggerFactory.getLogger(getClass());
        
            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);
            }
           ...
        }
        

        然后,让您的支持 bean 扩展该超类,您将能够自动装配 Spring bean 并使用 JSF 特定的视图范围:

        @ManagedBean
        @ViewScoped
        public class MyBackingBean extends AutowireableManagedBean {
        
            @Autowired
            private MyDao myDao;
        

        【讨论】:

          猜你喜欢
          • 2012-11-08
          • 2011-09-12
          • 2012-10-11
          • 2016-05-09
          • 2017-03-13
          • 2017-06-14
          • 2018-01-22
          • 2014-07-01
          • 2011-03-25
          相关资源
          最近更新 更多