Ayman
Ayman

Reputation: 3

a Failed IOC Injection - Spring and JSF integration

I'm trying to integrate jsf with spring and inject service classes on my managed bean

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

}

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

Upvotes: 0

Views: 93

Answers (2)

Kukeltje
Kukeltje

Reputation: 12335

Wrong combination

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

Weird combination

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

Both have all been discussed in Stackoverflow.

Upvotes: 0

Rae Burawes
Rae Burawes

Reputation: 912

Add the following to your applicationContext.xml:

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

and

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

So, your applicationContext.xml should look like this.

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

Add the spring-context library to your project too.

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

If you're using Maven.

Upvotes: 1

Related Questions