bonjour tous,
Je viens vous soumettre un problème dont j'arrive pas à déterminer la cause après avoir lu et relu mon code et fouiller sur le net. J'ai une classe dao que j'injecte grâce à l'annotation @Autowired dans mes couches services qui sont elles même injectées dans mon controller. l'utilisation des l'instances de ces couches services dans mon controller provoque un "NullPointerException". Ma configuration semble pourtant bonne, je n'arrive pas à trouver ce qui cloche, je vous fournis mes classe car besoin d'aide.
ApplicationContext
controller
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 <?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:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd"> <context:annotation-config></context:annotation-config> <context:component-scan base-package="com.fluiding" /> <bean id="datasource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"> <property name="driverClassName" value="com.mysql.jdbc.Driver"></property> <property name="url" value="jdbc:mysql://localhost:3306/ma_bdd"></property> <property name="username" value="root"></property> <property name="password" value=""></property> </bean> <bean id="persistenceUnitManager" class="org.springframework.orm.jpa.persistenceunit.DefaultPersistenceUnitManager"> <property name="defaultDataSource" ref="datasource"></property> <property name="persistenceXmlLocations"> <list> <value>classpath*:META-INF/persistence.xml</value> </list> </property> </bean> <bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean"> <property name="persistenceUnitManager" ref="persistenceUnitManager"></property> <property name="persistenceUnitName" value="UP_P"></property> </bean> <bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager"> <property name="entityManagerFactory" ref="entityManagerFactory"></property> </bean> <tx:annotation-driven /> </beans>
mon DAO
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 @Controller public class ControllerWeb { @Autowired UserValidator validator; @Autowired ISignUPMetier metier; @Autowired IUserDetailsServiveDAO service; private static int manageRole = 0; @Autowired RoleFactory roleFactory; @RequestMapping( value = "/inscription" ) public String inscription( Model model ) { model.addAttribute( "user", new User() ); return "inscription"; } @RequestMapping( value = "/sendUser" ) public String sendUser( @ModelAttribute( "user" ) User user, BindingResult result, HttpServletRequest request, HttpServletResponse response ) { if ( manageRole == 0 ) { // roleFactory = new RoleFactory(); manageRole++; } String confirmMotDePasse = request.getParameter( "cmdp" ); validator.validate( user, result ); if ( !user.getMot_de_passe().equals( confirmMotDePasse ) || confirmMotDePasse == null ) { ObjectError error = new ObjectError( "confirmation", "mot de passe pas valide" ); result.addError( error ); } if ( result.hasErrors() ) { return "inscription"; } user.setRole( roleFactory.getRoleUser() ); metier.register( user ); return "succes"; } @RequestMapping( value = "/admin" ) public String logout() { return "admin"; } }
couche service (SignUpMetierImpl)
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 @Transactional @Repository public class SignUpDAOImpl implements ISignUpDAO { @PersistenceContext( name = "UP_P" ) private EntityManager em; @Override public void register( User user ) { em.persist( user ); } @Override public void createRole( Role role ) { em.persist( role ); }; } public List<User> userParRole( Role role ) { Query query = em.createQuery( "select u from User u where u.role=:x" ); query.setParameter( "x", role ); List<User> users = query.getResultList(); return users; } }
un autre service (RoleHandlerMetierImpl)
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 @Transactional @Service public class SignUpMetierImpl implements ISignUPMetier { @Autowired ISignUpDAO dao; @Override public void register( User user ) { dao.register( user ); } /********* la suite ********/ }
La classe RoleFactory
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 @Service public class RoleHandlerMetierImpl implements IRoleHandlerMetier { @Autowired ISignUpDAO dao; @Override public Role createRole( String roleName ) { Role role = new Role( roleName ); return role; } @Override public void persistRole( Role role ) { dao.createRole( role ); } @Override public List<Role> listRole() { return dao.listRole(); } @Override public List<User> userParRole( Role role ) { return dao.userParRole( role ); } }
Dès le demarrage du serveur j'ai une erreur lors de l'injection du bean "RoleFactory" à cause d'un "NullPointerException" dans son constructeur
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 @Component public class RoleFactory extends RoleHandlerMetierImpl { Role roleUser; Role roleAdmin; public RoleFactory() { roleUser = createRole( "USER_ROLE" ); roleAdmin = createRole( "ADMIN_ROLE" ); persistRole( roleUser ); persistRole( roleAdmin ); } public Role getRoleUser() { return roleUser; } public Role getRoleAdmin() { return roleAdmin; } }
Si j'instancie mon controller sans faire appel à ce constructeur, il est bien instancié mais les objets injectés sont null..
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 org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'controllerWeb': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: sap.ldee.metier.RoleFactory sap.ldee.web.ControllerWeb.roleFactory; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'roleFactory' defined in file [/local/home/fnjiokou/workspace/.metadata/.plugins/org.eclipse.wst.server.core/tmp0/wtpwebapps/applicationPredict/WEB-INF/classes/sap/ldee/metier/RoleFactory.class]: Instantiation of bean failed; nested exception is org.springframework.beans.BeanInstantiationException: Could not instantiate bean class [sap.ldee.metier.RoleFactory]: Constructor threw exception; nested exception is java.lang.NullPointerException at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:286) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1055) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:511) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:450) at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:290) at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222) at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:287) at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:189) at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:562) at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:871) at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:423) at org.springframework.web.context.ContextLoader.createWebApplicationContext(ContextLoader.java:272) at org.springframework.web.context.ContextLoader.initWebApplicationContext(ContextLoader.java:196) at org.springframework.web.context.ContextLoaderListener.contextInitialized(ContextLoaderListener.java:47) at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:4738) at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5181) at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150) at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1408) at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1398) at java.util.concurrent.FutureTask.run(FutureTask.java:262) at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145) at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615) at java.lang.Thread.run(Thread.java:745) Caused by: org.springframework.beans.factory.BeanCreationException: Could not autowire field: sap.ldee.metier.RoleFactory sap.ldee.web.ControllerWeb.roleFactory; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'roleFactory' defined in file [/local/home/fnjiokou/workspace/.metadata/.plugins/org.eclipse.wst.server.core/tmp0/wtpwebapps/applicationPredict/WEB-INF/classes/sap/ldee/metier/RoleFactory.class]: Instantiation of bean failed; nested exception is org.springframework.beans.BeanInstantiationException: Could not instantiate bean class [sap.ldee.metier.RoleFactory]: Constructor threw exception; nested exception is java.lang.NullPointerException at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:507) at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:84) at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:283) ... 22 more Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'roleFactory' defined in file [/local/home/fnjiokou/workspace/.metadata/.plugins/org.eclipse.wst.server.core/tmp0/wtpwebapps/applicationPredict/WEB-INF/classes/sap/ldee/metier/RoleFactory.class]: Instantiation of bean failed; nested exception is org.springframework.beans.BeanInstantiationException: Could not instantiate bean class [sap.ldee.metier.RoleFactory]: Constructor threw exception; nested exception is java.lang.NullPointerException at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateBean(AbstractAutowireCapableBeanFactory.java:946) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:892) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:479) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:450) at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:290) at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222) at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:287) at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:189) at org.springframework.beans.factory.support.DefaultListableBeanFactory.findAutowireCandidates(DefaultListableBeanFactory.java:825) at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:767) at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:685) at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:478) ... 24 more Caused by: org.springframework.beans.BeanInstantiationException: Could not instantiate bean class [sap.ldee.metier.RoleFactory]: Constructor threw exception; nested exception is java.lang.NullPointerException at org.springframework.beans.BeanUtils.instantiateClass(BeanUtils.java:141) at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:72) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateBean(AbstractAutowireCapableBeanFactory.java:939) ... 35 more Caused by: java.lang.NullPointerException at sap.ldee.metier.RoleHandlerMetierImpl.persistRole(RoleHandlerMetierImpl.java:28) at sap.ldee.metier.RoleFactory.<init>(RoleFactory.java:17) at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:57) at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45) at java.lang.reflect.Constructor.newInstance(Constructor.java:526) at org.springframework.beans.BeanUtils.instantiateClass(BeanUtils.java:126) ... 37 more
Merci pour toute aide
Partager