2016-08-10 30 views
0

我试图用JSF春 注入服务类在我的托管bean失败的IOC注入 - Spring和JSF集成

我管理的Bean整合:

package web; 

import java.util.List; 
import javax.annotation.PostConstruct; 
import javax.enterprise.context.SessionScoped; 
import javax.faces.bean.ManagedBean; 
import mapping.*; 
import gestion.*; 

import org.springframework.beans.factory.annotation.Autowired; 
import org.springframework.stereotype.Component; 

@Component 
@ManagedBean(name="bean") 
@SessionScoped 
public class AjoutChamp { 
    private Module module; 
    private int m; 
    private int etape; 
    private int menu; 
    private int TypeChamp; 
    private int action; 
    private int selecteur; 
    private String valeur_selecteur; 
    private String contexte; 
    private String texte; 

    @Autowired 
    private GestionEtape gEtape; 
    @Autowired 
    private GestionModule gModule; 

    private List<Module> listeModule; 

    @PostConstruct 
    void init(){ 
     listeModule=gModule.selectAll(); 
    } 

    public int getM() { 
     return m; 
    } 
    public void setM(int m) { 
     this.m = m; 
    } 
    public List<Module> getListeModule() { 
     return listeModule; 
    } 
    public void setListeModule(List<Module> listeModule) { 
     this.listeModule = listeModule; 
    } 

    public GestionEtape getgEtape() { 
     return gEtape; 
    } 
    public void setgEtape(GestionEtape gEtape) { 
     this.gEtape = gEtape; 
    } 
    public GestionModule getgModule() { 
     return gModule; 
    } 
    public void setgModule(GestionModule gModule) { 
     this.gModule = gModule; 
    } 

    public Module getModule() { 
     return module; 
    } 
    public void setModule(Module module) { 
     this.module = module; 
    } 
    public int getEtape() { 
     return etape; 
    } 
    public void setEtape(int etape) { 
     this.etape = etape; 
    } 
    public int getMenu() { 
     return menu; 
    } 
    public void setMenu(int menu) { 
     this.menu = menu; 
    } 
    public int getTypeChamp() { 
     return TypeChamp; 
    } 
    public void setTypeChamp(int typeChamp) { 
     TypeChamp = typeChamp; 
    } 
    public int getAction() { 
     return action; 
    } 
    public void setAction(int action) { 
     this.action = action; 
    } 
    public int getSelecteur() { 
     return selecteur; 
    } 
    public void setSelecteur(int selecteur) { 
     this.selecteur = selecteur; 
    } 
    public String getValeur_selecteur() { 
     return valeur_selecteur; 
    } 
    public void setValeur_selecteur(String valeur_selecteur) { 
     this.valeur_selecteur = valeur_selecteur; 
    } 
    public String getContexte() { 
     return contexte; 
    } 
    public void setContexte(String contexte) { 
     this.contexte = contexte; 
    } 
    public String getTexte() { 
     return texte; 
    } 
    public void setTexte(String texte) { 
     this.texte = texte; 
    } 


    public AjoutChamp(){} 

} 

我的applicationContext:

<?xml version="1.0" encoding="UTF-8"?> 
<beans xmlns="http://www.springframework.org/schema/beans" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xmlns:aop="http://www.springframework.org/schema/aop" 
    xmlns:tx="http://www.springframework.org/schema/tx" 
    xmlns:context="http://www.springframework.org/schema/context" 
    xsi:schemaLocation=" 
     http://www.springframework.org/schema/beans 
     http://www.springframework.org/schema/beans/spring-beans.xsd 
     http://www.springframework.org/schema/tx 
     http://www.springframework.org/schema/tx/spring-tx.xsd 
     http://www.springframework.org/schema/aop 
     http://www.springframework.org/schema/aop/spring-aop.xsd 
     http://www.springframework.org/schema/context 
     http://www.springframework.org/schema/context/spring-context.xsd"> 

    <context:component-scan base-package="web"></context:component-scan> 
    <context:component-scan base-package="dao"></context:component-scan> 
    <context:component-scan base-package="gestion"></context:component-scan> 

    <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close"> 
     <property name="driverClassName" value="com.mysql.jdbc.Driver"/> 
     <property name="url" value="jdbc:mysql://localhost:3306/yo?useSSL=false"/> 
     <property name="username" value="root"/> 
     <property name="password" value=""/> 
    </bean> 

    <bean id="factory" class="org.springframework.orm.hibernate5.LocalSessionFactoryBean"> 
     <property name="dataSource" ref="dataSource"/> 
     <property name="hibernateProperties"> 
     <props> 
     <prop 
     key="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</prop> 
     <prop key="hibernate.show_sql">true</prop> 
     </props> 
     </property> 
     <property name="packagesToScan"> 
     <list> 
     <value>mapping</value> 
     </list> 
     </property> 
    </bean> 

    <bean id="transactionManager" 
      class="org.springframework.orm.hibernate5.HibernateTransactionManager"> 
     <property name="sessionFactory" ref="factory"/> 
    </bean> 
    <tx:annotation-driven transaction-manager="transactionManager"/> 

</beans> 

回答

1

以下内容添加到您的applicationContext.xml

xmlns:context="http://www.springframework.org/schema/context" 

http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd 

所以,你applicationContext.xml应该是这样的。

<beans xmlns="http://www.springframework.org/schema/beans" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xmlns:aop="http://www.springframework.org/schema/aop" 
    xmlns:tx="http://www.springframework.org/schema/tx" 
    xmlns:context="http://www.springframework.org/schema/context" 
    xsi:schemaLocation=" 
     http://www.springframework.org/schema/beans 
     http://www.springframework.org/schema/beans/spring-beans.xsd 
     http://www.springframework.org/schema/tx 
     http://www.springframework.org/schema/tx/spring-tx.xsd 
     http://www.springframework.org/schema/aop 
     http://www.springframework.org/schema/aop/spring-aop.xsd 
     http://www.springframework.org/schema/context 
     http://www.springframework.org/schema/context/spring-context.xsd"> 
     ... 
    </beans> 

将spring-context库也添加到您的项目中。

<dependency> 
    <groupId>org.springframework</groupId> 
    <artifactId>spring-context</artifactId> 
    <version><!-- version of Spring context. --></version> 
</dependency> 

如果您使用的是Maven。

+0

它说它无法找到BasicDataSource类。你不必在你的项目这种依赖性:公地DBCP公地DBCP 1.4

+0

一件事,不改变问题的标题。它会让答案与问题无关。如果您想看到您可以使用的示例项目,请访问此链接https://github.com/rburawes/spring-hibernate-demo。我已经上传了几年前创建的测试项目。我刚刚更新了依赖关系。 –

+0

你的意思是pom.xml正确吗?顺便说一句,用'org.apache.commons.dbcp2.BasicDataSource'取代它。我已经更新它来使用dbcp。 –

0

错误组合

import javax.enterprise.context.SessionScoped; 
import javax.faces.bean.ManagedBean; 

奇怪的组合

@Component 
@ManagedBean(name="bean") 
@SessionScoped 

两个已全部讨论#1。

+0

这个怎么样? http://www.journaldev.com/7112/jsf-spring-integration-example-tutorial –