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 :

Problème exécution Spring et Hibernate


Sujet :

Spring Web Java

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

    Informations forums :
    Inscription : Avril 2008
    Messages : 116
    Par défaut Problème exécution Spring et Hibernate
    Bonjour,

    J'ai un problème en faisant un test Junit dans une application Web avec Spring et Hibernate.

    Mon erreur est la suivante :

    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
    java.lang.IllegalStateException: BeanFactory not initialized or already closed - call 'refresh' before accessing beans via the ApplicationContext
    	at org.springframework.context.support.AbstractRefreshableApplicationContext.getBeanFactory(AbstractRefreshableApplicationContext.java:172)
    	at org.springframework.context.support.AbstractApplicationContext.getBean(AbstractApplicationContext.java:1125)
    	at com.test.tests.TestUser.testSave(TestUser.java:57)
    	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    	at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    	at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    	at java.lang.reflect.Method.invoke(Unknown Source)
    	at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:45)
    	at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)
    	at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:42)
    	at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:20)
    	at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:28)
    	at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:30)
    	at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:263)
    	at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:68)
    	at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:47)
    	at org.junit.runners.ParentRunner$3.run(ParentRunner.java:231)
    	at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:60)
    	at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:229)
    	at org.junit.runners.ParentRunner.access$000(ParentRunner.java:50)
    	at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:222)
    	at org.junit.runners.ParentRunner.run(ParentRunner.java:300)
    	at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50)
    	at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
    	at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)
    	at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
    	at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
    	at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)

    Je vous joins mon code.

    Mon application-context.xml présent sous WEB-INF :

    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
    <?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:context="http://www.springframework.org/schema/context"
    	xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:jee="http://www.springframework.org/schema/jee"
    	xmlns:lang="http://www.springframework.org/schema/lang" xmlns:p="http://www.springframework.org/schema/p"
    	xmlns:tx="http://www.springframework.org/schema/tx" xmlns:util="http://www.springframework.org/schema/util"
    	xmlns:jdbc="http://www.springframework.org/schema/jdbc"
    	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
    		http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.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
    		http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee.xsd
    		http://www.springframework.org/schema/lang http://www.springframework.org/schema/lang/spring-lang.xsd
    		http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd
    		http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd 
    		http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc.xsd">
     
     
    	<!-- post-processors for all standard config annotations -->
    	<context:annotation-config />
    	<context:component-scan base-package="com.test" />
     
     
    	<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
    		<property name="driverClassName" value="com.mysql.jdbc.Driver" />
    		<property name="url" value="jdbc:mysql://localhost:3306/test_manager" />
    		<property name="username" value="root" />
    		<property name="password" value="" />
    	</bean>
     
    	<bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
    		<property name="dataSource" ref="dataSource" />
    		<!--  <property name="jpaDialect">
    			<bean class="${jpa.dialect}" />
    		</property>-->
    		<property name="packagesToScan" value="com.test.models" />
    		<property name="jpaVendorAdapter">
    			<bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
    				<property name="showSql" value="true" />
    				<property name="databasePlatform" value="org.hibernate.dialect.MySQLDialect" />
    				<!-- false = On ne genere pas la BDD au demarrage -->
    				<property name="generateDdl" value="true" />
    			</bean>
    		</property>
    	</bean>
     
    	<bean id="txManager" class="org.springframework.orm.jpa.JpaTransactionManager">
    		<property name="entityManagerFactory" ref="entityManagerFactory" />
    	</bean>
     
    	<!-- enable the configuration of transactional behavior based on annotations -->
    	<tx:annotation-driven transaction-manager="txManager" />
     
    </beans>

    Mon web.xml :

    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
    <?xml version="1.0" encoding="UTF-8"?>
    <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:jsp="http://java.sun.com/xml/ns/javaee/jsp" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
      <display-name>Test</display-name>
      <welcome-file-list>
        <welcome-file>accueil</welcome-file>
      </welcome-file-list>
     
     
     
      <context-param>
         <param-name>contextConfigLocation</param-name>
         <param-value>/WEB-INF/application-context.xml</param-value>
     
      </context-param>
     
      <listener>
         <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
      </listener>
     
     
      <jsp-config>
        <jsp-property-group>
          <url-pattern>*.jsp</url-pattern>
          <include-prelude>/WEB-INF/views/taglibs.jsp</include-prelude>
        </jsp-property-group>
      </jsp-config>
      <servlet>
        <servlet-name>Accueil</servlet-name>
        <servlet-class>com.test.servlets.Accueil</servlet-class>
      </servlet>
      <servlet>
        <servlet-name>Inscription</servlet-name>
        <servlet-class>com.test.servlets.Inscription</servlet-class>
      </servlet>
      <servlet-mapping>
        <servlet-name>Accueil</servlet-name>
        <url-pattern>/accueil</url-pattern>
      </servlet-mapping>
      <servlet-mapping>
        <servlet-name>Inscription</servlet-name>
        <url-pattern>/inscription</url-pattern>
      </servlet-mapping>
     
     
    </web-app>
    Mon bean User :

    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.test.models;
     
    import javax.persistence.Column;
    import javax.persistence.Entity;
    import javax.persistence.Id;
    import javax.persistence.GeneratedValue;
    import javax.persistence.GenerationType;
    import javax.persistence.Table;
     
    /**
     * @author
     *
     */
    @Entity
    @Table(name="T_User")
    public class User {
     
    	@Id
    	@GeneratedValue(strategy=GenerationType.AUTO)
    	private Long Id;
     
    	@Column(name="nom", length=50)
    	private String Nom;
     
     
     
     
     
    	public Long getId() {
    		return Id;
    	}
    	public void setId(Long id) {
    		Id = id;
    	}
    	public String getNom() {
    		return Nom;
    	}
    	public void setNom(String nom) {
    		Nom = nom;
    	}
    	/**
             * 
             */
    	public User() {
    		// TODO Auto-generated constructor stub
    	}
     
    	public User(Long id, String nom) {
    		super();
    		this.Id = id;
    		this.Nom = nom;
    	}
     
    	public User(String name) {
    		this.Nom = name;
    	}
     
    }
    Sa 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
    package com.test.dao.impl;
     
    import javax.persistence.EntityManager;
    import javax.persistence.PersistenceContext;
     
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.stereotype.Repository;
     
    import com.test.dao.UserDAO;
    import com.test.models.User;
     
    @Repository("UserDAO")
    public class UserJPADAO extends GenericJPADAO<User, Long> implements UserDAO {
     
    	@PersistenceContext
    	private EntityManager entityManager;
     
     
    	//public class UserJPADAO extends Dao<User> implements UserDAO {
    	public UserJPADAO() {
    		super(User.class);
    	}
     
     
     
    }
    Mon service user :
    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
     
    package com.test.service.impl;
     
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.beans.factory.annotation.Qualifier;
    import org.springframework.context.annotation.Scope;
    import org.springframework.stereotype.Service;
    import org.springframework.transaction.annotation.Transactional;
     
    import com.test.dao.UserDAO;
    import com.test.dao.impl.UserJPADAO;
    import com.test.models.User;
    import com.test.service.IService;
     
     
     
     
     
    @Service("userService")
     
    public class UserService implements IService<User>{
     
    	@Autowired
    	private UserDAO userDAO;
     
     
     
     
    	/**
             * Default Constructor
             */
    	public UserService(){
     
    	}
     
    	private UserJPADAO userJPADAO;
     
    	 public UserService(UserJPADAO userJPADAO) { 
    	   this.userJPADAO=userJPADAO; 
    	 }
     
    	 @Transactional
    	 public void save(User user){
     
    		 userDAO.save(user);
    		}
     
     
     
    	@Override
    	public User newInstance() {
    		// TODO Auto-generated method stub
    		return new User();
    	}
     
     
    }
    Et pour finir ma classe test qui pose ce fameux problème :

    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
    package com.immo.tests;
     
     
    import org.junit.After;
    import org.junit.Before;
    import org.junit.Test;
    import org.junit.runner.RunWith;
    import org.springframework.web.context.support.XmlWebApplicationContext;
     
    import com.immo.models.User;
    import com.immo.service.impl.UserService;
     
    public class TestUser {
     
        XmlWebApplicationContext   context = new XmlWebApplicationContext();
     
     
     
        @Before
        public void setUp() throws Exception {
     
     
        }
     
        /**
         * @throws java.lang.Exception
         */
        @After
        public void tearDown() throws Exception {
            context.close();
        }
     
     
     
        public TestUser() {
            // TODO Auto-generated constructor stub
        }
     
        @Test
        public void testSave() {
     
     
     
            UserService userService = context.getBean(UserService.class);
            User user=new User();
            user.setNom("Test");
            userService.save(user);
     
        }
     
    }
    J'espère qu'avec cela vous pourrez me donner un coup de main car je sèche vraiment sur cela (au point que je me suis peut-être un peu embrouillé en tout seul a force de chercher).



    Merci par avance de vos réponse.

  2. #2
    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
    Par défaut
    Bonjour,

    Pourquoi ne pas utiliser Spring-test?
    Tu utilises seulement des annotations ensuite pour appeler les fichiers de configuration et injecter les dépendances.

    A+.

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

    Informations forums :
    Inscription : Avril 2008
    Messages : 116
    Par défaut
    Bonjour,
    Merci de ta réponse.

    Donc j'ai mis sa au-dessus e ma classe test
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
     
    @RunWith(SpringJUnit4ClassRunner.class)
    @ContextConfiguration("/com/test/tests/application-context.xml")
    Mais avec cela comment je fais pour récupérer mon userService?
    Qu'est ce qui peut remplacer cette ligne de code?
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
     
    UserService userService =   context.getBean(UserService.class);
    Merci par avance de ta réponse.

  4. #4
    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
    Par défaut
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    @Autowired
    private UserService userService;
    et,
    @ContextConfiguration("/com/test/tests/application-context.xml")
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    @ContextConfiguration("classpath:/com/test/tests/application-context.xml")
    A+.

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

    Informations forums :
    Inscription : Avril 2008
    Messages : 116
    Par défaut
    Encore merci de té réponse. Je suis désolé mais je ne m'en sort pas après modification j'ai donc 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
    package com.test.tests;
     
     
    import org.junit.After;
    import org.junit.Before;
    import org.junit.Test;
    import org.junit.runner.RunWith;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.test.context.ContextConfiguration;
    import org.springframework.test.context.junit4.AbstractTransactionalJUnit4SpringContextTests;
    import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
    import org.springframework.web.context.support.XmlWebApplicationContext;
     
    import com.test.dao.UserDAO;
    import com.test.dao.impl.UserJPADAO;
    import com.test.models.User;
    import com.test.service.impl.UserService;
     
    @RunWith(SpringJUnit4ClassRunner.class)
    @ContextConfiguration("classpath:/com/test/tests/application-context.xml")
    public class TestUser extends AbstractTransactionalJUnit4SpringContextTests {
     
    	@Autowired
    	private UserService userService;
     
    	@Before
    	public void setUp() throws Exception {
     
    	}
     
    	/**
             * @throws java.lang.Exception
             */
    	@After
    	public void tearDown() throws Exception {
     
    	}
     
     
     
    	public TestUser() {
    		// TODO Auto-generated constructor stub
    	}
     
    	@Test
    	public void testSave() {
     
    		User user=new User();
    		user.setNom("Test");
    		userService.save(user);
     
     
    	}
     
     
    }
    Mais j'ai cette erreur quand je lance mon test :

    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
    java.lang.IllegalStateException: Failed to load ApplicationContext
    	at org.springframework.test.context.TestContext.getApplicationContext(TestContext.java:308)
    	at org.springframework.test.context.support.DependencyInjectionTestExecutionListener.injectDependencies(DependencyInjectionTestExecutionListener.java:109)
    	at org.springframework.test.context.support.DependencyInjectionTestExecutionListener.prepareTestInstance(DependencyInjectionTestExecutionListener.java:75)
    	at org.springframework.test.context.TestContextManager.prepareTestInstance(TestContextManager.java:333)
    	at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.createTest(SpringJUnit4ClassRunner.java:220)
    	at org.springframework.test.context.junit4.SpringJUnit4ClassRunner$1.runReflectiveCall(SpringJUnit4ClassRunner.java:301)
    	at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)
    	at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.methodBlock(SpringJUnit4ClassRunner.java:303)
    	at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:240)
    	at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:47)
    	at org.junit.runners.ParentRunner$3.run(ParentRunner.java:231)
    	at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:60)
    	at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:229)
    	at org.junit.runners.ParentRunner.access$000(ParentRunner.java:50)
    	at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:222)
    	at org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61)
    	at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:70)
    	at org.junit.runners.ParentRunner.run(ParentRunner.java:300)
    	at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:180)
    	at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50)
    	at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
    	at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)
    	at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
    	at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
    	at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)
    Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'userService': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private com.test.service.impl.UserService com.test.service.impl.UserService.userService; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No matching bean of type [com.test.service.impl.UserService] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
    	at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:288)
    	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1120)
    	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:522)
    	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:461)
    	at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:295)
    	at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:223)
    	at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:292)
    	at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:194)
    	at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:607)
    	at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:932)
    	at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:479)
    	at org.springframework.test.context.support.AbstractGenericContextLoader.loadContext(AbstractGenericContextLoader.java:84)
    	at org.springframework.test.context.support.AbstractGenericContextLoader.loadContext(AbstractGenericContextLoader.java:1)
    	at org.springframework.test.context.TestContext.loadApplicationContext(TestContext.java:280)
    	at org.springframework.test.context.TestContext.getApplicationContext(TestContext.java:304)
    	... 24 more
    Caused by: org.springframework.beans.factory.BeanCreationException: Could not autowire field: private com.test.service.impl.UserService com.test.service.impl.UserService.userService; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No matching bean of type [com.test.service.impl.UserService] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
    	at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:514)
    	at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:87)
    	at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:285)
    	... 38 more
    Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No matching bean of type [com.test.service.impl.UserService] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
    	at org.springframework.beans.factory.support.DefaultListableBeanFactory.raiseNoSuchBeanDefinitionException(DefaultListableBeanFactory.java:949)
    	at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:818)
    	at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:730)
    	at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:486)
    	... 40 more

    Je pense que c'est l'AUTOWIRED qu'il n'aime pas, mais je ne sais pas comment paré à cela.

    Merci par avance de ta réponse.

  6. #6
    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
    Par défaut
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    import com.test.service.IService;
    ....
    @Autowired
    private IService userService;
    A+.

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

    Informations forums :
    Inscription : Avril 2008
    Messages : 116
    Par défaut
    Merci de ta réponse mais cette solution ne change rien.
    donc maintenant j'ai :

    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
    package com.test.tests;
     
     
    import org.junit.After;
    import org.junit.Before;
    import org.junit.Test;
    import org.junit.runner.RunWith;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.test.context.ContextConfiguration;
    import org.springframework.test.context.junit4.AbstractTransactionalJUnit4SpringContextTests;
    import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
    import org.springframework.web.context.support.XmlWebApplicationContext;
     
    import com.test.dao.UserDAO;
    import com.test.dao.impl.UserJPADAO;
    import com.test.models.User;
    import com.test.service.IService;
    import com.test.service.impl.UserService;
     
     
    @RunWith(SpringJUnit4ClassRunner.class)
    @ContextConfiguration("classpath:/com/test/tests/application-context.xml")
    public class TestUser extends AbstractTransactionalJUnit4SpringContextTests {
     
    	@Autowired
    	private IService<User> userService;
     
    	@Before
    	public void setUp() throws Exception {
     
    	}
     
    	/**
             * @throws java.lang.Exception
             */
    	@After
    	public void tearDown() throws Exception {
    	}
     
     
     
    	public TestUser() {
    	}
     
    	@Test
    	public void testSave() {
     
    		User user=new User();
    		user.setNom("Test");
    		userService.save(user);
     
    	}
     
     
    }
    Mon erreur est la même.

    Pour info mon interface Iservice est la suivante :

    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
    package com.test.service;
     
     
    public interface IService<R> {
    	/**
             * 
             * @param entity
             * sauvegarde d'un objet en base de données
             */
    	//public void save(R entity);
    	public R save(R entity);
     
    	public R newInstance();
     
    }
    Merci par avance de ta réponse.

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

    Informations forums :
    Inscription : Avril 2008
    Messages : 116
    Par défaut
    Mon JUNIT passe en faite j'avais une autre erreur que j'ai corrigé. Merci de ton aide.
    Mais il n'insère pas en base de donnée alors que j'ai bien cette ligne en log et que j'ai bien la table qui est crée :

    Hibernate: insert into T_User (nom) values (?)

    Quelqu'un aurait-il une idée?

  9. #9
    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
    Par défaut
    Il ne va pas enregistrer ça en permanent, il teste seulement si l'enregistrement passe bien. Si tu n'as pas d'erreur donc c'est bon.
    Sinon, tu peux nous montrer le contenue de la méthode save de UserDao?

    A+.

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

    Informations forums :
    Inscription : Avril 2008
    Messages : 116
    Par défaut
    Je n'ai pas d'erreur, donc on va dire que cela fonctionne (pour le moment) si j'ai un problème je reviendrais.
    Encore merci de ton aide.

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

    Informations forums :
    Inscription : Avril 2008
    Messages : 116
    Par défaut
    Mon test JUNIT passe mais j'essaye de faire le même test dans le main mais cela ne fonctionne pas surement pour des raisons identique à ma première erreur, alors que je fais quelque chose de semblable à mon JUNIT.

    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.test.runtime;
     
     
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.web.context.support.XmlWebApplicationContext;
     
    import com.test.models.User;
    import com.test.service.IService;
    import com.test.service.impl.UserService;
     
     
     
    public class Application {
     
     
    		@Autowired
    		private IService<User> userService;
     
    	/**
             * @param args
             */
    	public static void main(String[] args) {
     
    		XmlWebApplicationContext   context = new XmlWebApplicationContext();		
    		UserService userService = context.getBean(UserService.class);
     
     
     
    		User user= new User(null, "Linus");
    		//user.setNom("Test");
    		userService.save(user);
     
    		context.close();
     
     
    	}
     
    }
    et comme je l'ai dit l'erreur est semblable...

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    Exception in thread "main" java.lang.IllegalStateException: BeanFactory not initialized or already closed - call 'refresh' before accessing beans via the ApplicationContext
    	at org.springframework.context.support.AbstractRefreshableApplicationContext.getBeanFactory(AbstractRefreshableApplicationContext.java:172)
    	at org.springframework.context.support.AbstractApplicationContext.getBean(AbstractApplicationContext.java:1125)
    	at com.test.runtime.Application.main(Application.java:25)
    Est-ce que vous auriez une solution pour m'aidez?
    Merci par avance de votre réponse.

  12. #12
    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
    Par défaut
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    ApplicationContext context= new ClassPathXmlApplicationContext("tonApplicationContext.xml");
    IService userService = context.getBean("UserService");

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

    Informations forums :
    Inscription : Avril 2008
    Messages : 116
    Par défaut
    Bonjour,
    Merci de ta réponse.
    Il a fallut que je fasse un cast pour que sa fonctionne:
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    IService<User> userService = (IService<User>) context.getBean("userService");
    Mais sa marche merci.
    Mais j'ai une question le chargement de contexte se fait si j'ai bien compris par le web.xml, or il faut que je fasse dans mon application :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    		ApplicationContext context= new ClassPathXmlApplicationContext("application-context.xml");
    Est-ce qu'il n'y aurait pas une autre méthode ou est-ce que jesuis obligé de faire comme tu me dit?

    Merci par avance de ta réponse

  14. #14
    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
    Par défaut
    Citation Envoyé par cris83500 Voir le message
    Mais j'ai une question le chargement de contexte se fait si j'ai bien compris par le web.xml, or il faut que je fasse dans mon application :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    		ApplicationContext context= new ClassPathXmlApplicationContext("application-context.xml");
    Dans le cas d'une application web tu n'as plus besoin de ça si tu utilise de framwork MVC avec spring, tu peux utiliser des annotations selon le framwork utilisé; sinon tu dois utiliser XmlWebApplicationContext pour l'implémentation de l'ApplicationContext. Y en a d'autre comme FileSystemXmlApplicationContext, XmlPortletApplicationContext mais tous dépend de ton besoin. Je te conseille de voir la documentation officiel de Spring.

    A+.

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

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

    merci de ta réponse et de tes éclaircissements.

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

Discussions similaires

  1. Problème intégration Spring et Hibernate
    Par mavy77 dans le forum Spring
    Réponses: 4
    Dernier message: 12/11/2013, 10h03
  2. [Data] Problème Integration Spring et Hibernate
    Par Guerr dans le forum Spring
    Réponses: 3
    Dernier message: 06/12/2012, 15h29
  3. Problème j2EE, Spring, MySQL, Hibernate sous Debian
    Par vinou94400 dans le forum Hibernate
    Réponses: 0
    Dernier message: 01/02/2012, 22h10
  4. [Integration] Problème avec Spring 3 + Hibernate + Vaadin
    Par JamesP dans le forum Spring
    Réponses: 10
    Dernier message: 30/11/2011, 02h46
  5. [Data] Problème de lazy avec hibernate et Spring
    Par Invité dans le forum Spring
    Réponses: 3
    Dernier message: 20/02/2008, 20h03

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