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 :
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 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); }
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 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); } }
Voici mon generic Manager pour Info :
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 dexposer 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; } }
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 :
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
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> * <bean id="userManager" class="com.mycompany.service.impl.GenericManagerImpl"> * <constructor-arg> * <bean class="com.mycompany.dao.hibernate.GenericDaoHibernate"> * <constructor-arg value="com.mycompany.model.User"/> * <property name="sessionFactory" ref="sessionFactory"/> * </bean> * </constructor-arg> * </bean> * </pre> * <p/> * <p>If you're using iBATIS instead of Hibernate, use: * <pre> * <bean id="userManager" class="com.mycompany.service.impl.GenericManagerImpl"> * <constructor-arg> * <bean class="com.mycompany.dao.ibatis.GenericDaoiBatis"> * <constructor-arg value="com.mycompany.model.User"/> * <property name="dataSource" ref="dataSource"/> * <property name="sqlMapClient" ref="sqlMapClient"/> * </bean> * </constructor-arg> * </bean> * </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); // } }
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
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 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); }
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
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); } }
Auriez vous une idée pour m'aider s'il vous plait? cela doit être un problème d'injection quelque part peut-être ...
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 dexposer 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; } }
Merci par avance de vos réponses.
Partager