IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

Spring Web Java Discussion :

Erreur "No unique bean of type is defined: expected single matching bean but found 2:"


Sujet :

Spring Web Java

  1. #1
    Membre du Club
    Profil pro
    Inscrit en
    Avril 2008
    Messages
    116
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Avril 2008
    Messages : 116
    Points : 66
    Points
    66
    Par défaut Erreur "No unique bean of type is defined: expected single matching bean but found 2:"
    Bonjour,

    je viens chercher de l'aide car j'ai un problème que je n'arrive pas à résoudre malgré ce que j'ai pu trouvé sur le net (pas toujours clair) et mes multiples essais :

    Avant d'avoir ce problème j'avais :

    Un ManagerUser :
    Son interface :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
     
    package com.cad.manager;
     
    import java.util.List;
     
    import com.cad.entities.Role;
    import com.cad.entities.User;
     
    public interface UserManager extends GenericManager<User, Long>{
     
    	public void saveUser(User user);
     
    	public List<User> getAllUsers();
     
    	public User getUserById(Long id);
     
     
     
    }
    son implémentation :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    package com.cad.manager.impl;
     
    import java.util.List;
     
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.stereotype.Service;
    import org.springframework.transaction.annotation.Transactional;
     
    import com.cad.dao.RoleDao;
    import com.cad.dao.UserDao;
    import com.cad.entities.Role;
    import com.cad.entities.User;
    import com.cad.manager.UserManager;
     
    @Service
    @Transactional(readOnly = true)
    public class UserManagerImpl extends GenericManagerImpl<User, Long> implements UserManager {
     
     
    	private UserDao userDao;
     
    	@Autowired
        public UserManagerImpl(UserDao userDao) {
            super(userDao);
            this.userDao = userDao;
        }
     
     
    	public void setUserDao(UserDao userDao) {
    		this.dao = userDao;
            this.userDao = userDao;
    	}
     
    	@Transactional(readOnly = false)
    	public void saveUser(User user) {
     
    		dao.save(user);
    	;
     
     
    	}
     
    	public List<User> getAllUsers() {
    		return userDao.findAll();
    	}
     
    	public User getUserById(Long id) {
    		return userDao.findById(id);
    	}
     
     
     
    }
    et son bean :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    package com.cad.bean;
     
    import java.util.HashMap;
    import java.util.List;
    import java.util.Map;
     
     
     
    import org.jasypt.util.password.ConfigurablePasswordEncryptor;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.context.annotation.Scope;
    import org.springframework.stereotype.Component;
     
    import com.cad.dao.impl.DAOException;
    import com.cad.entities.User;
    import com.cad.manager.UserManager;
     
     
     
    //permet d’exposer une classe en tant que WebBean.
    @Component
    @Scope
    public class UserBean {
     
        // =========================================================================
        // ATTRIBUTES
        // =========================================================================
        private User user;
     
        private UserManager manager;
     
     
     
     
     
        // =========================================================================
        // CONSTRUCTORS
        // =========================================================================
     
     
     
    	public UserBean() {
        }
     
       =========================================================================
        // GETTERS & SETTERS
        // =========================================================================
        public List<User> getAllUsers() {
        //public List getAllUsers() {
            return manager.getAllUsers();
        }
     
        public User getUser() {
            return user;
        }
     
        public void setUser(User user) {
            this.user = user;
        }
     
        @Autowired
        public void setManager(UserManager manager) {
            this.manager = manager;
        }
     
    }
    Voici mon generic Manager pour Info :

    son interface :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    package com.cad.manager;
     
    import java.io.Serializable;
    import java.util.List;
     
    public interface GenericManager<T, PK extends Serializable> {
     
        /**
         * Generic method used to get all objects of a particular type. This
         * is the same as lookup up all rows in a table.
         * @return List of populated objects
         */
        List<T> getAll();
     
        /**
         * Generic method to get an object based on class and identifier. An
         * ObjectRetrievalFailureException Runtime Exception is thrown if
         * nothing is found.
         *
         * @param id the identifier (primary key) of the object to get
         * @return a populated object
         * @see org.springframework.orm.ObjectRetrievalFailureException
         */
    //    T get(PK id);
     
        /**
         * Checks for existence of an object of type T using the id arg.
         * @param id the identifier (primary key) of the object to get
         * @return - true if it exists, false if it doesn't
         */
    //    boolean exists(PK id);
     
        /**
         * Generic method to save an object - handles both update and insert.
         * @param object the object to save
         * @return the updated object
         */
    //    T save(T object);
     
        /**
         * Generic method to delete an object
         * @param object the object to remove
         */
    //    void remove(T object);
     
        /**
         * Generic method to delete an object based on class and id
         * @param id the identifier (primary key) of the object to remove
         */
        void remove(PK id);
     
        /**
         * Generic method to search for an object.
         * @param searchTerm the search term
         * @param clazz type of class to search for.
         * @return a list of matched objects
         */
    //    List<T> search(String searchTerm, Class clazz);
        /**
         * Generic method to regenerate full text index of the persistent class T
         */
    //    void reindex();
     
        /**
         * Generic method to regenerate full text index of all indexed classes
         *
         * @param async
         *            true to perform the reindexing asynchronously
         */
    //    void reindexAll(boolean async);
    }

    son implémentation :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    package com.cad.manager.impl;
     
    import org.apache.commons.logging.Log;
    import org.apache.commons.logging.LogFactory;
    //import com.mycompany.dao.GenericDao;
    //import com.mycompany.service.GenericManager;
    import org.springframework.beans.factory.annotation.Autowired;
     
    import com.cad.dao.GenericDao;
    import com.cad.manager.GenericManager;
     
    import java.io.Serializable;
    import java.util.ArrayList;
    import java.util.List;
     
    /**
     * This class serves as the Base class for all other Managers - namely to hold
     * common CRUD methods that they might all use. You should only need to extend
     * this class when your require custom CRUD logic.
     * <p/>
     * <p>To register this class in your Spring context file, use the following XML.
     * <pre>
     *     &lt;bean id="userManager" class="com.mycompany.service.impl.GenericManagerImpl"&gt;
     *         &lt;constructor-arg&gt;
     *             &lt;bean class="com.mycompany.dao.hibernate.GenericDaoHibernate"&gt;
     *                 &lt;constructor-arg value="com.mycompany.model.User"/&gt;
     *                 &lt;property name="sessionFactory" ref="sessionFactory"/&gt;
     *             &lt;/bean&gt;
     *         &lt;/constructor-arg&gt;
     *     &lt;/bean&gt;
     * </pre>
     * <p/>
     * <p>If you're using iBATIS instead of Hibernate, use:
     * <pre>
     *     &lt;bean id="userManager" class="com.mycompany.service.impl.GenericManagerImpl"&gt;
     *         &lt;constructor-arg&gt;
     *             &lt;bean class="com.mycompany.dao.ibatis.GenericDaoiBatis"&gt;
     *                 &lt;constructor-arg value="com.mycompany.model.User"/&gt;
     *                 &lt;property name="dataSource" ref="dataSource"/&gt;
     *                 &lt;property name="sqlMapClient" ref="sqlMapClient"/&gt;
     *             &lt;/bean&gt;
     *         &lt;/constructor-arg&gt;
     *     &lt;/bean&gt;
     * </pre>
     *
     * @param <T>  a type variable
     * @param <PK> the primary key for that type
     * @author <a href="mailto:matt@raibledesigns.com">Matt Raible</a>
     *  Updated by jgarcia: added full text search + reindexing
     */
    public class GenericManagerImpl<T, PK extends Serializable> implements GenericManager<T, PK> {
        /**
         * Log variable for all child classes. Uses LogFactory.getLog(getClass()) from Commons Logging
         */
        protected final Log log = LogFactory.getLog(getClass());
     
        /**
         * GenericDao instance, set by constructor of child classes
         */
        @Autowired
        protected GenericDao<T, PK> dao;
     
     
        public GenericManagerImpl() {
        }
     
        public GenericManagerImpl(GenericDao<T, PK> genericDao) {
            this.dao = genericDao;
        }
     
        /**
         * {@inheritDoc}
         */
        public List<T> getAll() {
            return dao.findAll();
        }
     
    //    /**
    //     * {@inheritDoc}
    //     */
    //    public T get(PK id) {
    //        return dao.get(id);
    //    }
    //
    //    /**
    //     * {@inheritDoc}
    //     */
    //    public boolean exists(PK id) {
    //        return dao.exists(id);
    //    }
     
    //    /**
    //     * {@inheritDoc}
    //     */
    //    public T save(T object) {
    //        return dao.save(object);
    //    }
    //
    //    /**
    //     * {@inheritDoc}
    //     */
    //    public void remove(T object) {
    //        dao.remove(object);
    //    }
     
        /**
         * {@inheritDoc}
         */
        public void remove(PK id) {
            dao.remove(id);
        }
     
        /**
         * {@inheritDoc}
         * <p/>
         * Search implementation using Hibernate Search.
         */
    //    @SuppressWarnings("unchecked")
    //    public List<T> search(String q, Class clazz) {
    //        if (q == null || "".equals(q.trim())) {
    //            return getAll();
    //        }
    //
    //        return dao.search(q);
    //    }
    //
    //    /**
    //     * {@inheritDoc}
    //     */
    //    public void reindex() {
    //        dao.reindex();
    //    }
    //
    //    /**
    //     * {@inheritDoc}
    //     */
    //    public void reindexAll(boolean async) {
    //        dao.reindexAll(async);
    //    }
    }
    J'utilise les beans car mon faces-config.xml est comme cela :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    <?xml version='1.0' encoding='UTF-8'?>
    <faces-config xmlns="http://java.sun.com/xml/ns/javaee"
    	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    	xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-facesconfig_2_0.xsd"
    	version="2.0">
    	<application>
    		<el-resolver>org.springframework.web.jsf.el.SpringBeanFacesELResolver</el-resolver>
    	</application>
    </faces-config>

    Mais quand j'essaye de rajouter mes roles pour faire une authentification avec spring-security j'ai cette erreur:

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    No unique bean of type [com.cad.dao.GenericDao] is defined: expected single matching bean but found 2: [roleDao, userDao]

    Donc je pense que mon erreur est dut aux fichiers de roles :

    Son interface
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    package com.cad.manager;
     
    import java.util.List;
     
    import com.cad.entities.Role;
     
    /**
     * Business Service Interface to handle communication between web and
     * persistence layer.
     *
     * @author cad
     */
    public interface RoleManager extends GenericManager<Role, Long> {
        /**
         * {@inheritDoc}
         */
        List getRoles(Role role);
     
        /**
         * {@inheritDoc}
         */
        Role getRole(String rolename);
     
        /**
         * {@inheritDoc}
         */
        //Role saveRole(Role role);
     
        void saveRole(Role role);
     
        /**
         * {@inheritDoc}
         */
        void removeRole(String rolename);
    }
    Son implementation
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    package com.cad.manager.impl;
     
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.stereotype.Service;
     
    import com.cad.dao.RoleDao;
    import com.cad.entities.Role;
    import com.cad.manager.RoleManager;
     
    import java.util.List;
     
    /**
     * Implementation of RoleManager interface.
     *
     * @author <a href="mailto:dan@getrolling.com">Dan Kibler</a>
     */
    @Service("roleManager")
    public class RoleManagerImpl extends GenericManagerImpl<Role, Long> implements RoleManager {
        RoleDao roleDao;
     
        @Autowired
        public RoleManagerImpl(RoleDao roleDao) {
            super(roleDao);
            this.roleDao = roleDao;
        }
     
        /**
         * {@inheritDoc}
         */
        public List<Role> getRoles(Role role) {
        	//return dao.getAll();
            return dao.findAll();
        }
     
        /**
         * {@inheritDoc}
         */
        public Role getRole(String rolename) {
            return roleDao.getRoleByName(rolename);
        }
     
        /**
         * {@inheritDoc}
         */
    //    public Role saveRole(Role role) {
    //        return dao.save(role);
    //        //return dao.
    //    }
     
        public void saveRole(Role role) {
             dao.save(role);
            //return dao.
        }
     
        /**
         * {@inheritDoc}
         */
        public void removeRole(String rolename) {
            roleDao.removeRole(rolename);
        }
    }
    Et son bean que j'ai rajouté qui ne change rien :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    package com.cad.bean;
     
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.beans.factory.annotation.Qualifier;
    import org.springframework.context.annotation.Scope;
    import org.springframework.stereotype.Component;
     
    import com.cad.entities.Role;
    import com.cad.manager.RoleManager;
     
    //permet d’exposer une classe en tant que WebBean.
    @Component("RoleBean")
    @Scope
    public class RoleBean {
     
    	private Role role;
     
    	@Qualifier("roleManager")
    	//@Autowired
        private RoleManager manager;
     
    	public Role getRole() {
    		return role;
    	}
     
    	public void setRole(Role role) {
    		this.role = role;
    	}
     
    	public RoleManager getManager() {
    		return manager;
    	}
     
    	public void setManager(RoleManager manager) {
    		this.manager = manager;
    	}
     
    }
    Auriez vous une idée pour m'aider s'il vous plait? cela doit être un problème d'injection quelque part peut-être ...

    Merci par avance de vos réponses.

  2. #2
    Expert éminent sénior
    Avatar de tchize_
    Homme Profil pro
    Ingénieur développement logiciels
    Inscrit en
    Avril 2007
    Messages
    25 481
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 44
    Localisation : Belgique

    Informations professionnelles :
    Activité : Ingénieur développement logiciels
    Secteur : High Tech - Éditeur de logiciels

    Informations forums :
    Inscription : Avril 2007
    Messages : 25 481
    Points : 48 806
    Points
    48 806
    Par défaut
    Ici,

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
        @Autowired
        protected GenericDao<T, PK> dao;
    Tu lui demande de lier automatiquement un GenericDao. Mais tu as 2 generic Dao: roleDao et userDao. Du coup, Spring ignor lequel il doit mettre.

    Tu dois lui préciser lequel utiliser (avec un @Qualifier il me semble)

  3. #3
    Membre du Club
    Profil pro
    Inscrit en
    Avril 2008
    Messages
    116
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Avril 2008
    Messages : 116
    Points : 66
    Points
    66
    Par défaut
    Bonjour,

    Merci de ta réponse.
    Tu as tapé dans le mille cela fonctionne mais je ne vois pas pourquoi en choisir un et pas l'autre.
    Y'a t'il une règle?

    En tous cas merci déja car cela m'enlève une épine de sous le pied.

  4. #4
    Expert éminent sénior
    Avatar de tchize_
    Homme Profil pro
    Ingénieur développement logiciels
    Inscrit en
    Avril 2007
    Messages
    25 481
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 44
    Localisation : Belgique

    Informations professionnelles :
    Activité : Ingénieur développement logiciels
    Secteur : High Tech - Éditeur de logiciels

    Informations forums :
    Inscription : Avril 2007
    Messages : 25 481
    Points : 48 806
    Points
    48 806
    Par défaut
    heu, ben tu ne sais stocker qu'une valeur dans ton champ, donc faut bien décider laquelle

  5. #5
    Membre du Club
    Profil pro
    Inscrit en
    Avril 2008
    Messages
    116
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Avril 2008
    Messages : 116
    Points : 66
    Points
    66
    Par défaut
    Merci de ta réponse,
    Avec les deux cela marche mais je ne comprend toujours pas laquelle choisir et pas l'autre, peut-être qu'il y a un moyen pour que sa prenne soit l'un soit l'autre selon le besoin? (étant donné que c'est un generic DAO?)

  6. #6
    Expert éminent sénior
    Avatar de tchize_
    Homme Profil pro
    Ingénieur développement logiciels
    Inscrit en
    Avril 2007
    Messages
    25 481
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 44
    Localisation : Belgique

    Informations professionnelles :
    Activité : Ingénieur développement logiciels
    Secteur : High Tech - Éditeur de logiciels

    Informations forums :
    Inscription : Avril 2007
    Messages : 25 481
    Points : 48 806
    Points
    48 806
    Par défaut
    Non, pas possible. T et PK sont inconnu de Spring. L'information sur les generic n'est que partielle à la compilation.

  7. #7
    Rédacteur/Modérateur
    Avatar de andry.aime
    Homme Profil pro
    Inscrit en
    Septembre 2007
    Messages
    8 391
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : Ile Maurice

    Informations forums :
    Inscription : Septembre 2007
    Messages : 8 391
    Points : 15 059
    Points
    15 059
    Par défaut
    Citation Envoyé par tchize_ Voir le message
    Tu dois lui préciser lequel utiliser (avec un @Qualifier il me semble)
    Vaux mieux utiliser @Resource à la place de @Qualifier + @Autowired.

    A+.

  8. #8
    Membre du Club
    Profil pro
    Inscrit en
    Avril 2008
    Messages
    116
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Avril 2008
    Messages : 116
    Points : 66
    Points
    66
    Par défaut
    Bonjour,

    Merci de vos réponses;
    Le @Resource ne nécessite pas de choisir avec l'un ou l'autre et résoud mon problème aussi, donc correspond plus à mon problème. En tous cas encore merci pour votre aide à tous les deux.

+ Répondre à la discussion
Cette discussion est résolue.

Discussions similaires

  1. [Framework] Erreur "No qualifying bean of type found for dependency"
    Par shark59 dans le forum Spring
    Réponses: 5
    Dernier message: 04/06/2018, 00h55
  2. Réponses: 4
    Dernier message: 03/04/2012, 11h25
  3. [Framework] [Core][Autowiring] No unique bean of type [service.AdresseDAO] is defined
    Par xadimousalih dans le forum Spring
    Réponses: 3
    Dernier message: 17/11/2008, 17h27
  4. [JSF] Erreur "bean of type null"
    Par vallica dans le forum JSF
    Réponses: 5
    Dernier message: 27/03/2006, 11h57
  5. [VB]probleme double quote dans une ressource de type string
    Par JulienCEA dans le forum VB 6 et antérieur
    Réponses: 3
    Dernier message: 23/02/2006, 12h38

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo