web-dev-qa-db-fra.com

org.springframework.beans.factory.NoSuchBeanDefinitionException: aucun bean nommé 'customerService' n'est défini

J'ai besoin d'aide pour corriger cette erreur que j'obtiens lorsque j'essaie de déployer mon application Web dans Tomcat. Pourquoi le bean customerService n'est-il pas défini? Suis-je en train de manquer quelque chose dans mon web.xml ou dois-je mapper le service client d'une manière ou d'une autre? J'utilise des annotations pour le mappage. Toute aide serait très appréciée. Voici l'entrée du journal des erreurs du journal localhost:

Erreur:

SEVERE: Exception sending context initialized event to listener instance of class org.springframework.web.context.ContextLoaderListener
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'customerController': Injection of resource dependencies failed; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'customerService' is defined
    at org.springframework.context.annotation.CommonAnnotationBeanPostProcessor.postProcessPropertyValues(CommonAnnotationBeanPostProcessor.Java:306)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.Java:1106)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.Java:517)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.Java:456)
    at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.Java:294)
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.Java:225)
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.Java:291)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.Java:193)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.Java:585)
    at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.Java:913)
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.Java:464)
    at org.springframework.web.context.ContextLoader.configureAndRefreshWebApplicationContext(ContextLoader.Java:384)
    at org.springframework.web.context.ContextLoader.initWebApplicationContext(ContextLoader.Java:283)
    at org.springframework.web.context.ContextLoaderListener.contextInitialized(ContextLoaderListener.Java:111)
    at org.Apache.catalina.core.StandardContext.listenerStart(StandardContext.Java:4600)
    at org.Apache.catalina.core.StandardContext$1.call(StandardContext.Java:5097)
    at org.Apache.catalina.core.StandardContext$1.call(StandardContext.Java:5092)
    at Java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.Java:334)
    at Java.util.concurrent.FutureTask.run(FutureTask.Java:166)
    at Java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.Java:1110)
    at Java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.Java:603)
    at Java.lang.Thread.run(Thread.Java:722)
Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'customerService' is defined
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBeanDefinition(DefaultListableBeanFactory.Java:529)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getMergedLocalBeanDefinition(AbstractBeanFactory.Java:1095)
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.Java:277)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.Java:197)
    at org.springframework.context.annotation.CommonAnnotationBeanPostProcessor.autowireResource(CommonAnnotationBeanPostProcessor.Java:442)
    at org.springframework.context.annotation.CommonAnnotationBeanPostProcessor.getResource(CommonAnnotationBeanPostProcessor.Java:416)
    at org.springframework.context.annotation.CommonAnnotationBeanPostProcessor$ResourceElement.getResourceToInject(CommonAnnotationBeanPostProcessor.Java:549)
    at org.springframework.beans.factory.annotation.InjectionMetadata$InjectedElement.inject(InjectionMetadata.Java:150)
    at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.Java:87)
    at org.springframework.context.annotation.CommonAnnotationBeanPostProcessor.postProcessPropertyValues(CommonAnnotationBeanPostProcessor.Java:303)
    ... 21 more

Apr 30, 2012 11:17:09 AM org.Apache.catalina.core.ApplicationContext log
INFO: Closing Spring root WebApplicationContext
Apr 30, 2012 11:23:19 AM org.Apache.catalina.core.ApplicationContext log
INFO: SessionListener: contextDestroyed()
Apr 30, 2012 11:23:19 AM org.Apache.catalina.core.ApplicationContext log
INFO: ContextListener: contextDestroyed()
Apr 30, 2012 11:23:20 AM org.Apache.catalina.core.ApplicationContext log
INFO: Destroying Spring FrameworkServlet 'spring'
Apr 30, 2012 11:23:20 AM org.Apache.catalina.core.ApplicationContext log
INFO: Closing Spring root WebApplicationContext
Apr 30, 2012 11:23:27 AM org.Apache.catalina.core.ApplicationContext log
INFO: Initializing Spring root WebApplicationContext
Apr 30, 2012 11:23:33 AM org.Apache.catalina.core.ApplicationContext log
INFO: Initializing Spring FrameworkServlet 'spring'
Apr 30, 2012 11:23:35 AM org.Apache.catalina.core.ApplicationContext log
INFO: ContextListener: contextInitialized()
Apr 30, 2012 11:23:35 AM org.Apache.catalina.core.ApplicationContext log
INFO: SessionListener: contextInitialized()
Apr 30, 2012 11:23:57 AM org.Apache.catalina.core.ApplicationContext log
INFO: Initializing Spring root WebApplicationContext
Apr 30, 2012 11:23:58 AM org.Apache.catalina.core.StandardContext listenerStart
SEVERE: Exception sending context initialized event to listener instance of class org.springframework.web.context.ContextLoaderListener
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'customerController': Injection of resource dependencies failed; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'customerService' is defined
    at org.springframework.context.annotation.CommonAnnotationBeanPostProcessor.postProcessPropertyValues(CommonAnnotationBeanPostProcessor.Java:306)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.Java:1106)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.Java:517)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.Java:456)
    at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.Java:294)
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.Java:225)
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.Java:291)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.Java:193)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.Java:585)
    at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.Java:913)
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.Java:464)
    at org.springframework.web.context.ContextLoader.configureAndRefreshWebApplicationContext(ContextLoader.Java:384)
    at org.springframework.web.context.ContextLoader.initWebApplicationContext(ContextLoader.Java:283)
    at org.springframework.web.context.ContextLoaderListener.contextInitialized(ContextLoaderListener.Java:111)
    at org.Apache.catalina.core.StandardContext.listenerStart(StandardContext.Java:4600)
    at org.Apache.catalina.core.StandardContext$1.call(StandardContext.Java:5097)
    at org.Apache.catalina.core.StandardContext$1.call(StandardContext.Java:5092)
    at Java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.Java:334)
    at Java.util.concurrent.FutureTask.run(FutureTask.Java:166)
    at Java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.Java:1110)
    at Java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.Java:603)
    at Java.lang.Thread.run(Thread.Java:722)
Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'customerService' is defined
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBeanDefinition(DefaultListableBeanFactory.Java:529)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getMergedLocalBeanDefinition(AbstractBeanFactory.Java:1095)
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.Java:277)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.Java:197)
    at org.springframework.context.annotation.CommonAnnotationBeanPostProcessor.autowireResource(CommonAnnotationBeanPostProcessor.Java:442)
    at org.springframework.context.annotation.CommonAnnotationBeanPostProcessor.getResource(CommonAnnotationBeanPostProcessor.Java:416)
    at org.springframework.context.annotation.CommonAnnotationBeanPostProcessor$ResourceElement.getResourceToInject(CommonAnnotationBeanPostProcessor.Java:549)
    at org.springframework.beans.factory.annotation.InjectionMetadata$InjectedElement.inject(InjectionMetadata.Java:150)
    at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.Java:87)
    at org.springframework.context.annotation.CommonAnnotationBeanPostProcessor.postProcessPropertyValues(CommonAnnotationBeanPostProcessor.Java:303)
    ... 21 more

web.xml (mistest/WebContent/WEB-INF) * Mis à jour *

<?xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp_ID" version="2.4" 
         xmlns="http://Java.Sun.com/xml/ns/j2ee" 
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
         xsi:schemaLocation="http://Java.Sun.com/xml/ns/j2ee 
         http://Java.Sun.com/xml/ns/j2ee/web-app_2_4.xsd">

   <servlet>
    <servlet-name>spring</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <load-on-startup>1</load-on-startup>
  </servlet>

    <servlet-mapping>
    <servlet-name>spring</servlet-name>
    <url-pattern>/mistest/*</url-pattern>
    </servlet-mapping>

    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/applicationContext.xml</param-value>
    </context-param>



    <listener>
            <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>



 </web-app>

service Clients

package testapp.mis.service;

import Java.util.List;

import testapp.mis.domain.Customer;

public interface CustomerService {


    public List<Customer> retrieveAllCustomerNames();
    public List<Customer> retrieveAllCustomerCountries();
    public void createCustomer(Customer customer);
    public List<Customer> retrieveAllCustomers();

}

merci pour l'aide, faites-moi savoir si j'ai besoin de mettre en place d'autres fichiers.

Edit: Voici les fichiers applicationContext.xml et customerService Implementation:

applicationContext.xml (mistest/WebContent/WEB-INF) * Mis à jour *

<?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: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:mvc="http://www.springframework.org/schema/mvc"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.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/context
      http://www.springframework.org/schema/context/spring-context-3.0.xsd
   http://www.springframework.org/schema/mvc
   http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">

    <context:annotation-config/>
    <context:component-scan base-package="testapp.mis"/>
    <!-- Configures the @Controller programming model -->
    <mvc:annotation-driven />

    <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver" 
            p:prefix="/WEB-INF/jsp/" p:suffix=".jsp" p:order="0"/>

    <bean id="messageSource"
        class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
        <property name="basename" value="classpath:messages"/>
        <property name="defaultEncoding" value="UTF-8"/>
    </bean>
</beans>

customerServiceImpl:

package testapp.mis.service;

import Java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import testapp.mis.dao.CustomerDao;
import testapp.mis.domain.Customer;

@Service
public class CustomerServiceImpl implements CustomerService {

    @Autowired(required=true)
    private CustomerDao customerDao;

    @Transactional
    public List<Customer> retrieveAllCustomerNames() {
        return this.customerDao.getAllCustomerNames();
    }

    @Transactional
    public List<Customer> retrieveAllCustomerCountries() {
        return this.customerDao.getAllCustomerCountries();
    }

    @Transactional
    public void createCustomer(Customer customer){
        this.customerDao.saveCustomer(customer);
    }

    @Transactional
    public List<Customer> retrieveAllCustomers(){
        return this.customerDao.getAllCustomers();
    }

}

Edit2:

Voici le CustomerController. J'ai ajouté le @Autowired comme suggéré.

package testapp.mis.controller;

//import javax.annotation.Resource;
import Java.util.List;

import testapp.mis.service.CustomerService;
import testapp.mis.domain.Customer;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.beans.factory.annotation.Autowired;


@Controller
@RequestMapping("/customer")
public class CustomerController {



    /*@Resource(name="customerService")
    private CustomerService customerService;
    */
    @Autowired
    private CustomerService customerService;


    @RequestMapping(value="/list", method=RequestMethod.GET)
    public String getCustomerList(Model model) {
        List<Customer> customers = customerService.retrieveAllCustomers();
        System.out.println("test");
        model.addAttribute("customerList", customers);

        return "customer";
    }

    @RequestMapping(value="/add", method=RequestMethod.GET)
    public String getCustomer(Model model) {

        model.addAttribute("customerAttribute", new Customer());

        return "new-customer";
    }

    @RequestMapping(value="/add", method=RequestMethod.POST)
    public String postCustomer(@ModelAttribute("customerAttribute") Customer customer) {
        customerService.createCustomer(customer);

        return "redirect:/mis/customer/list";
    }
} 

spring-servlet.xml (mistest/WebContent/WEB-INF)

<?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:p="http://www.springframework.org/schema/p" 
    xsi:schemaLocation="http://www.springframework.org/schema/beans 
            http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">

    <!-- Declare a view resolver -->
    <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver" 
            p:prefix="/WEB-INF/jsp/" p:suffix=".jsp" />

</beans>

hibernate-context.xml (mistest/WebContent/WEB-INF)

<?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:p="http://www.springframework.org/schema/p"
        xmlns:tx="http://www.springframework.org/schema/tx"
        xmlns:context="http://www.springframework.org/schema/context"
        xsi:schemaLocation="
           http://www.springframework.org/schema/beans
           http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
           http://www.springframework.org/schema/tx
           http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
           http://www.springframework.org/schema/context
           http://www.springframework.org/schema/context/spring-context-3.0.xsd
      ">

 <context:property-placeholder location="/WEB-INF/spring.properties" />

    <!-- Enables annotations for transaction management -->
    <tx:annotation-driven transaction-manager="transactionManager" />

    <!-- Declare the Hibernate SessionFactory for retrieving Hibernate sessions -->
    <bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean"
        p:dataSource-ref="dataSource"
        p:configLocation="${hibernate.config}"
        p:packagesToScan="testapp.mis"/>

 <!-- Declare a datasource that has pooling capabilities--> 
 <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"
    destroy-method="close"
    p:driverClass="${app.jdbc.driverClassName}"
    p:jdbcUrl="${app.jdbc.url}"
    p:user="${app.jdbc.username}"
    p:password="${app.jdbc.password}"
    p:acquireIncrement="5"
    p:idleConnectionTestPeriod="60"
    p:maxPoolSize="100"
    p:maxStatements="50"
    p:minPoolSize="10" />

 <!-- Declare a transaction manager-->
 <bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager"
          p:sessionFactory-ref="sessionFactory" />

</beans>

hibernate.cfg.xml (mistest/WebContent/WEB-INF)

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
  "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
  "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">

<hibernate-configuration>
  <session-factory>

   <!-- We're using MySQL database so the dialect needs to MySQL as well-->
    <property name="hibernate.dialect">org.hibernate.dialect.MySQL5InnoDBDialect</property>
    <!-- Enable this to see the SQL statements in the logs-->
    <property name="show_sql">false</property>

    <!-- Remove after testing -->

    <!-- This will drop our existing database and re-create a new one.
      Existing data will be deleted! -->
    <property name="hbm2ddl.auto">create</property>
  </session-factory>
</hibernate-configuration>

/RÉ

13
dlinx90

En lisant votre exception, il est certain que vous avez oublié de câbler automatiquement le service client

Vous devez câbler automatiquement votre service client.

apporter les modifications suivantes dans votre classe de contrôleur

@Controller
public class CustomerController{

    @Autowired
    private Customerservice customerservice;
......other code......

}

Encore une fois, votre classe d'implémentation de service

écrire

@Service
public class CustomerServiceImpl implements CustomerService {
    @Autowired
    private CustomerDAO customerDAO;
......other code......

.....add transactional methods
}

Si vous utilisez hibernate, apportez les modifications nécessaires dans votre fichier xml d'applicationcontext (la configuration de la fabrique de sessions est nécessaire).

vous devez autowire la méthode sessionFactory set dans votre mise en œuvre DAO

veuillez trouver le contexte d'application samle:

<?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: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:mvc="http://www.springframework.org/schema/mvc"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.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/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd">

    <context:annotation-config />
    <context:component-scan base-package="com.sparkle" />
    <!-- Configures the @Controller programming model -->
    <mvc:annotation-driven />

    <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver" 
            p:prefix="/WEB-INF/jsp/" p:suffix=".jsp" p:order="0" />

    <bean id="messageSource"
        class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
        <property name="basename" value="classpath:messages" />
        <property name="defaultEncoding" value="UTF-8" />
    </bean>
    <!-- <bean id="propertyConfigurer"
        class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"
        p:location="/WEB-INF/jdbc.properties" /> -->

     <bean id="propertyConfigurer"
        class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
    <property name="locations">         
        <list>
            <value>/WEB-INF/jdbc.properties</value>          

        </list>    
    </property>     
    </bean>




    <bean id="dataSource"
        class="org.springframework.jdbc.datasource.DriverManagerDataSource"
        p:driverClassName="${jdbc.driverClassName}"
        p:url="${jdbc.databaseurl}" p:username="${jdbc.username}"
        p:password="${jdbc.password}" />


    <bean id="sessionFactory"
        class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
        <property name="dataSource" ref="dataSource" />
        <property name="configLocation">
            <value>classpath:hibernate.cfg.xml</value>
        </property>
        <property name="configurationClass">
            <value>org.hibernate.cfg.AnnotationConfiguration</value>
        </property>
        <property name="hibernateProperties">
            <props>
                <prop key="hibernate.dialect">${jdbc.dialect}</prop>
                <prop key="hibernate.show_sql">true</prop>
            </props>
        </property>
    </bean>

    <tx:annotation-driven />


   <bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager"
          p:sessionFactory-ref="sessionFactory"/>


</beans>

notez que j'utilise le fichier jdbc.properties pour l'url jdbc et la spécification du pilote

9
JOHND

Vous devrez annoter votre service avec @Service puisque vous avez dit I am using annotations for mapping

4
mprabhat

Veuillez vous assurer que votre fichier applicationContext.xml est chargé en le spécifiant dans votre fichier web.xml:

    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/applicationContext.xml</param-value>
    </context-param>
3
ndeverge

Juste une autre possibilité: Spring initialise le bean par type et non par nom si vous ne définissez pas le bean avec un nom, ce qui est correct si vous l'utilisez par son type:

Producteur:

@Service
public void FooServiceImpl implements FooService{}

Consommateur:

@Autowired
private FooService fooService;

ou

@Autowired
private void setFooService(FooService fooService) {}

mais pas ok si vous l'utilisez par son nom:

ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");
ctx.getBean("fooService");

Il se plaindrait: org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'fooService' is defined Dans ce cas, attribuer un nom à @Service("fooService") le ferait fonctionner.

2
Tiina