2016-08-10 3 views
0

Ich versuche, JSF mit Feder und inject Serviceklassen auf meinem Managed Beanausgefallenes IOC Injection - Frühling und JSF Integration

Mein Managed Bean zu integrieren:

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(){} 

} 

mein application:

<?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> 

Antwort

1

folgendes Ihre applicationContext.xml hinzufügen:

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

und

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

Also, Ihr applicationContext.xml sollte wie folgt aussehen.

<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> 

Fügen Sie Ihrem Projekt die spring-context-Bibliothek hinzu.

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

Wenn Sie Maven verwenden.

+0

Es besagt, dass es die BasicDataSource-Klasse nicht finden kann. Sie verfügen nicht über diese Abhängigkeit von Ihrem Projekt: commons-dbcp commons-dbcp 1.4

+0

Eine Sache, nicht den Titel der Frage ändern. Es wird die Antwort für die Frage irrelevant machen. Wenn Sie ein Beispielprojekt sehen möchten, das Sie verwenden können, besuchen Sie diesen Link https://github.com/rburawes/spring-hibernate-demo. Ich habe mein Testprojekt hochgeladen, das ich vor langer Zeit erstellt habe. Ich habe gerade die Abhängigkeiten aktualisiert. –

+0

Du meinst pom.xml richtig? :) BTW, ersetzen Sie es durch 'org.apache.commons.dbcp2.BasicDataSource'. Ich habe es aktualisiert, um den dbcp zu verwenden. –

0

Falsche Kombination

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

Sonderbare Kombination

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

Beide haben alle in Stackoverflow diskutiert.

+0

was ist das? http://www.journaldev.com/7112/jsf-spring-integration-example-tutorial –

Verwandte Themen