栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

集成jsf,spring,hibernate。如何将Spring bean注入JSF管理的bean中?

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

集成jsf,spring,hibernate。如何将Spring bean注入JSF管理的bean中?

The naive solution for non-JSF developers would be to simply initialize the
variables inside the getter to resolve the

null
value in the attributes.
This is:

public List getListeAnnees() {    listeAnnees = getAnneeMetier().getAllAnnees();    return listeAnnees;}public AnneeMetier getAnneeMetier() {    if (anneeMetier == null) {        anneeMetier = new AnneeMetierImpl();    }    return anneeMetier;}

But this may generate lot of overhead from server in case

AnneeMetier#getAllAnnees()
retrieves the data from database. This is
explained here: Why JSF calls getters multipletimes

To solve this, you do two things:

  1. Define the right scope of your bean.
  2. Initialize the necessary data for work using
    @PostConstruct
    annotated method.

And this would result in:

  1. Defining the scope as
    @ViewScoped
    (explained in the link above).
  2. Initializing
    listeAnnees
    in
    @PostConstruct
    method.
  3. Remove any business logic from getters/setters

So the pre would look like this:

@ManagedBean@ViewScopedpublic class AnneeBean {    private AnneeMetier anneeMetier;    private List<Annee> listeAnnees;    @PostConstruct    public void init() {        anneeMetier = new AnneeMetierImpl();        listeAnnees = anneeMetier.getAllAnnees();    }    public List getListeAnnees() {        return listeAnnees;    }    public void setListeAnnees(List listeAnnees) {        this.listeAnnees = listeAnnees;    }    public AnneeMetier getAnneeMetier() {        return anneeMetier;    }    public void setAnneeMetier(AnneeMetier anneeMetier) {        this.anneeMetier = anneeMetier;    }}

BUT since you’re trying to integrate JSF with Spring, you have to take
into account that Spring has not yet full support of JSF 2

@ViewScoped

annotation. For this case, you
have/need to implement it yourself. There are plenty examples on the net about
this, and looks that the most popular is from
Cagatay’s. In this way, you’ll be able to gain power
from both sides. And your bean will look like this:

@Component@Scope("view")public class AnneeBean {    @Autowired    private AnneeMetier anneeMetier;    private List<Annee> listeAnnees;    @PostConstruct    public void init() {        listeAnnees = anneeMetier.getAllAnnees();    }    public List getListeAnnees() {        return listeAnnees;    }    public void setListeAnnees(List listeAnnees) {        this.listeAnnees = listeAnnees;    }}

Since you’re learning Spring, the best bet would be to enable component scan
and use annotations to configure your spring beans. Do the following:

  • Remove any bean configuration in applicationContext.xml.
  • Add this configuration to enable bean scan annotations:
        <!--         These will enable component scan by annotation configuration        rather than XML configuration. One per package    -->    <context:component-scan base-package="dao" />    <context:component-scan base-package="model.services" />

Or if all your classes are inside one root package.

    <!--    Assuming there's a root package for your packages like this<context:component-scan base-package="com.myproject.dao" /><context:component-scan base-package="com.myproject.model.services" />--><context:component-scan base-package="com.myproject" />
  • Start configuring your Spring managed beans by annotations:
        @Repository    public class AnneeHibernateDao implements AnneeDao{        //...    }    @Service    public class AnneeMetierImpl implements AnneeMetier{        @Autowired        private AnneeDao anneeDao;        //...    }

Compile your project and run it.



转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/506939.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号