NoSuchBeanDefinitionException: No bean named is defined
Bonjour
Je suis débutant sur le framework Spring j'essaie de réaliser un petit TP mais la je me retrouve bloqué, depuis hier je cherche mais je ne trouve pas :oops:
Enfaite j'ai créer mon fichier XML de configuration Spring :
Code:
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
| <?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: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-4.2.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.2.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.2.xsd">
<!--
Création d'une dataSource pour gérer la connexion a la data Base depuis spring par Hibernate
-->
<bean id="dataSource" class="org.apache.commons.dbcp2.BasicDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Drive"/>
<property name="url" value="jdbc:mysql://localhost:3306/stock_magasin"/>
<property name="username" value="root"/>
<property name="password" value="root"/>
</bean>
<!--
Création d'une sessionFactory pour ouvrir une connexion
Définir les classes qui sont gérer depuis spring
Les classes sont gérée par annotation
-->
<bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<property name="packagesToScan">
<list>
<value>Gestion_Stock.src.entites.Journal</value>
<value>Gestion_Stock.src.entites.Produit</value>
<value>Gestion_Stock.src.entites.TypeProduit</value>
</list>
</property>
<!--
Ajouter le dialect depuis hibernate
-->
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQLInnoDBDialect</prop>
</props>
</property>
</bean>
<bean id="transactionManager"
class="org.springframework.orm.hibernate4.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>
<tx:annotation-driven transaction-manager="transactionManager"/>
<context:annotation-config/>
<context:component-scan base-package="Gestion_Stock"></context:component-scan>
</beans> |
Et j'ai créer ma classe produitServiceImpl avec un nouveau service Spring @Service
Code:
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
| package manager;
import java.util.List;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Repository;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import entites.Produit;
@Service("produitService")
@Transactional
public class ProduitServiceImpl implements ProduitService {
@Autowired
private SessionFactory sessionFactory;
@SuppressWarnings("unchecked")
@Override
public List<Produit> getALLProduits() {
return sessionFactory.getCurrentSession().createCriteria("").list();
}
@Override
public Produit getProduitByID(Integer id) {
return (Produit) sessionFactory.getCurrentSession().load(Produit.class,id);
}
@Override
public void setProduit(Produit produit) {
sessionFactory.getCurrentSession().saveOrUpdate(produit);
}
} |
Pour valider cela j'ai créer une classe de test avec JUnit
Code:
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
| package manager;
import static org.junit.Assert.*;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class ProduitServiceTest {
private static ClassPathXmlApplicationContext context;
private static ProduitService produitService;
@BeforeClass
public static void setUpBeforeClass() throws Exception {
context = new ClassPathXmlApplicationContext("gestionStock_Context.xml");
produitService = (ProduitService) context.getBean("produitService");
}
@AfterClass
public static void tearDownAfterClass() throws Exception {
context.close();
}
@Test
public void testGetALLProduits() {
fail("Not yet implemented");
}
@Test
public void testGetProduitByID() {
fail("Not yet implemented");
}
@Test
public void testSetProduit() {
fail("Not yet implemented");
}
} |
Le problème c'est que quand j'exécute ma classe JUnit il me dit qu'il ne trouve pas le bean "produitService" alors que j'ai bien nomé @Service("produitService") :calim2:
pour le résultat j'ai erreur suivante
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
| org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'produitServices' is defined
at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBeanDefinition(DefaultListableBeanFactory.java:698)
at org.springframework.beans.factory.support.AbstractBeanFactory.getMergedLocalBeanDefinition(AbstractBeanFactory.java:1175)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:284)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:197)
at org.springframework.context.support.AbstractApplicationContext.getBean(AbstractApplicationContext.java:1051)
at manager.ProduitServiceTest.setUpBeforeClass(ProduitServiceTest.java:20)
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:47)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:44)
at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:24)
at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:27)
at org.junit.runners.ParentRunner.run(ParentRunner.java:309)
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:459)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:675)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:382)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:192) |
Je suis vraiment bloqué la est ce que vous pouvez m'aider svp ?
Cordialement,