Spring应用程序中的NoSuchBeanDefinitionException

时间:2014-09-11 21:06:34

标签: java spring rest spring-mvc

我正在创建一个使用REST的应用程序。目前我的Controller正在使用ServiceMockImplementation实现,但我打算使用数据库(而不是硬编码我的Contacts的值)。当模拟实现使用时,restful客户端工作正常(并在服务器上运行)但是当我添加我的DAO实现时,它崩溃并且给了我NoSuchBeanDefinitionException,即使bean“contactDao”被注释为{{ 1}}在我的@Repository("contactDao")课程中。

这是我的DAOImplementation

RestfulClientMain

这是我的public class RestfulClientMain { private static final String URL_GET_ALL_CONTACTS = "http://localhost:8080/Contact/contacts"; private static final String URL_GET_CONTACT_BY_ID = "http://localhost:8080/Contact/contacts/{id}"; private static final String URL_CREATE_CONTACT = "http://localhost:8080/Contact/contacts/"; private static final String URL_UPDATE_CONTACT = "http://localhost:8080/Contact/contacts/{id}"; private static final String URL_DELETE_CONTACT = "http://localhost:8080/Contact/contacts/{id}"; public static void main(String[] args) { GenericXmlApplicationContext ctx = new GenericXmlApplicationContext(); ctx.load("classpath:restful-client-app-context.xml"); ctx.refresh(); Contacts contacts; Contact contact; RestTemplate restTemplate = ctx.getBean("restTemplate", RestTemplate.class); // Test retrieve all contacts System.out.println("Testing retrieve all contacts:"); contacts = restTemplate.getForObject(URL_GET_ALL_CONTACTS, Contacts.class); listContacts(contacts); // Test retrieve contact by id System.out.println("Testing retrieve a contact by id :"); contact = restTemplate.getForObject(URL_GET_CONTACT_BY_ID, Contact.class, 1); System.out.println(contact); System.out.println(""); // Test update contact contact = restTemplate.getForObject(URL_UPDATE_CONTACT, Contact.class, 1); contact.setFirstName("Kim Fung"); System.out.println("Testing update contact by id :"); restTemplate.put(URL_UPDATE_CONTACT, contact, 1); System.out.println("Contact update successfully: " + contact); System.out.println(""); // Testing delete contact restTemplate.delete(URL_DELETE_CONTACT, 1); System.out.println("Testing delete contact by id :"); contacts = restTemplate.getForObject(URL_GET_ALL_CONTACTS, Contacts.class); listContacts(contacts); // Testing create contact System.out.println("Testing create contact :"); Contact contactNew = new Contact(); contactNew.setFirstName("JJ"); contactNew.setLastName("Gosling"); contactNew.setBirthDate(new Date()); contactNew = restTemplate.postForObject(URL_CREATE_CONTACT, contactNew, Contact.class); System.out.println("Contact created successfully: " + contactNew); } private static void listContacts(Contacts contacts) { for (Contact contact: contacts.getContacts()) { System.out.println(contact); } System.out.println(""); } }

ContactController

这是我的ContactMockImplementation。目前,您可以看到它包含硬编码值,但作为注释,我已经包含了解决此错误后代码应该实际执行的操作。这是与DAO交互(从数据库中检索联系人)。

import javax.servlet.http.HttpServletResponse;
import org.slf4j.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.context.request.WebRequest;
import com.aucklanduni.spring.webservices.domain.*;
import com.aucklanduni.spring.webservices.service.ContactService;


@Controller
@RequestMapping(value="/contacts")
public class ContactController {

    final Logger logger = LoggerFactory.getLogger(ContactController.class);


    @Autowired
    private ContactService contactService;

    @RequestMapping(method = RequestMethod.GET)
    @ResponseBody
    public Contacts listData(WebRequest webRequest) {
        return new Contacts(contactService.findAll());
    }   

    @RequestMapping(value="/{id}", method=RequestMethod.GET)
    @ResponseBody
    public Contact findContactById(@PathVariable Long id) {     
        return contactService.findById(id);
    }

    @RequestMapping(value="/", method=RequestMethod.POST)
    @ResponseBody
    public Contact create(@RequestBody Contact contact, HttpServletResponse response) {
        logger.info("Creating contact: " + contact);
        contactService.save(contact);
        logger.info("Contact created successfully with info: " + contact);
        response.setHeader("Location",  "/contacts/" + contact.getId());
        return contact;
    }

    @RequestMapping(value="/{id}", method=RequestMethod.PUT)
    @ResponseBody
    public void update(@RequestBody Contact contact, @PathVariable Long id) {
        logger.info("Updating contact: " + contact);
        contactService.save(contact);
        logger.info("Contact updated successfully with info: " + contact);
        //return contact;
    }   

    @RequestMapping(value="/{id}", method=RequestMethod.DELETE)
    @ResponseBody
    public void delete(@PathVariable Long id) {
        logger.info("Deleting contact with id: " + id);
        Contact contact = contactService.findById(id);
        contactService.delete(contact);
        logger.info("Contact deleted successfully");
    }   

}

这是我的import java.util.*; import org.joda.time.DateTime; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.*; import com.aucklanduni.spring.webservices.domain.*; @Service("contactService") @Component public class ContactServiceMockImplementation implements ContactService { private Contacts _contacts; //private ContactDao contactDao; public ContactServiceMockImplementation() { Contact contact1 = new Contact(); contact1.setId(1L); contact1.setVersion(1); contact1.setFirstName("Clint"); contact1.setLastName("Eastwood"); contact1.setBirthDate(new Date()); Contact contact2 = new Contact(); contact2.setId(1L); contact2.setVersion(1); contact2.setFirstName("Robert"); contact2.setLastName("Redford"); contact2.setBirthDate(new Date()); Contact contact3 = new Contact(); contact3.setId(1L); contact3.setVersion(1); contact3.setFirstName("Michael"); contact3.setLastName("Caine"); contact3.setBirthDate(new Date()); List<Contact> contactList = new ArrayList<Contact>(); contactList.add(contact1); contactList.add(contact2); contactList.add(contact3); _contacts = new Contacts(contactList); } @Override public List<Contact> findAll() { return _contacts.getContacts(); //return contactDao.findAll(); } // @Override // public List<Contact> findByFirstName(String firstName) { // List<Contact> results = new ArrayList<Contact>(); // // for(Contact contact : _contacts.getContacts()) { // if(contact.getFirstName().equals(firstName)) { // results.add(contact); // } // } // return results; // } @Override public Contact findById(Long id) { Contact result = null; for(Contact contact : _contacts.getContacts()) { if(contact.getId() == id) { result = contact; break; } } return result; //return contactDao.findById(id); } @Override public Contact save(Contact contact) { return contact; //return contactDao.save(contact); } @Override public void delete(Contact contact) { // TODO Auto-generated method stub //contactDao.delete(contact); } } - 这就是给我一个错误。

ContactDao

启动服务器时,这是我得到的错误:

import java.util.List;
import javax.annotation.Resource;
import org.hibernate.SessionFactory;
import org.slf4j.*;
import org.springframework.stereotype.*;
import org.springframework.transaction.annotation.Transactional;
import com.aucklanduni.spring.webservices.domain.Contact;

@Repository("contactDao")
@Component
@Transactional
public class ContactDaoImpl implements ContactDao {

    private Logger _logger = LoggerFactory.getLogger(ContactDaoImpl.class);

    private SessionFactory _sessionFactory; 

    public SessionFactory getSessionFactory() {
        return _sessionFactory;
    }

    @Resource
    public void setSessionFactory(SessionFactory sessionFactory) {
        this._sessionFactory = sessionFactory;
        _logger.debug("SessionFactory class: " + sessionFactory.getClass().getName());
    }

    @Transactional(readOnly=true)
    public List<Contact> findAll() {
        List<Contact> result = _sessionFactory.getCurrentSession().createQuery("from Contact c").list();
        return result;
    }

    public List<Contact> findAllWithDetail() {
        return _sessionFactory.getCurrentSession().getNamedQuery("Contact.findAllWithDetail").list();
    }

    public Contact findById(Long id) {
        return (Contact) _sessionFactory.getCurrentSession().
                getNamedQuery("Contact.findById").setParameter("id", id).uniqueResult();
    }

    public Contact save(Contact contact) {
        _sessionFactory.getCurrentSession().saveOrUpdate(contact);
        _logger.info("Contact saved with id: " + contact.getId());
        return contact;
    }

    public void delete(Contact contact) {
        _sessionFactory.getCurrentSession().delete(contact);
        _logger.info("Contact deleted with id: " + contact.getId());
    }

}

我目前陷入困境,因为我不太确定如何将我的ContactService与我的ContactDao链接(而不是使用MockImplementation方法)。任何帮助都感激不尽。谢谢!

这是我的restful-client-app-context.xml:

ERROR: org.springframework.web.servlet.DispatcherServlet - Context initialization failed
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'contactDao': Injection of resource dependencies failed; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [org.hibernate.SessionFactory] found for dependency: expected at least 1 bean which qualifies as 

最新日志错误:

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

    <jdbc:embedded-database id="dataSource" type="H2">
        <jdbc:script location="classpath:schema.sql"/>
        <jdbc:script location="classpath:test-data.sql"/>
    </jdbc:embedded-database>

    <bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
        <property name="sessionFactory" ref="springSessionFactory"/>
    </bean>  

    <!-- <bean id="contactDao" class="com.aucklanduni.spring.webservices.service.ContactDao"></bean> -->

    <tx:annotation-driven/>

    <context:component-scan base-package="com.aucklanduni.spring.webservices" />

    <bean class="org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor" />

    <bean id="springSessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
        <property name="dataSource" ref="dataSource"/>
        <property name="packagesToScan" value="com.aucklanduni.spring.webservices.domain"/>
        <property name="hibernateProperties">
            <props>
                <prop key="hibernate.dialect">org.hibernate.dialect.H2Dialect</prop>
                <prop key="hibernate.max_fetch_depth">3</prop>
                <prop key="hibernate.jdbc.fetch_size">50</prop>
                <prop key="hibernate.jdbc.batch_size">10</prop>
                <prop key="hibernate.show_sql">true</prop>
            </props>
        </property>        
    </bean>
    <bean id="restTemplate" class="org.springframework.web.client.RestTemplate">
        <!--  <constructor-arg ref="httpRequestFactory"/>-->
        <property name="messageConverters">
            <list>
                <bean class="org.springframework.http.converter.xml.MarshallingHttpMessageConverter">
                    <property name="marshaller" ref="castorMarshaller"/>
                    <property name="unmarshaller" ref="castorMarshaller"/>
                    <property name="supportedMediaTypes">
                        <list>
                            <bean class="org.springframework.http.MediaType">
                                <constructor-arg index="0" value="application"/>
                                <constructor-arg index="1" value="xml"/>
                            </bean>
                        </list>
                    </property>
                </bean>                
            </list>
        </property>    
    </bean>

    <bean id="castorMarshaller" class="org.springframework.oxm.castor.CastorMarshaller">
        <property name="mappingLocation" value="classpath:oxm-mapping.xml"/>
    </bean>

这是我的web.xml:

[mvcContentNegotiationManager,org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping#0,org.springframework.format.support.FormattingConversionServiceFactoryBean#0,org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter#0,org.springframework.web.servlet.handler.MappedInterceptor#0,org.springframework.web.servlet.mvc.method.annotation.ExceptionHandlerExceptionResolver#0,org.springframework.web.servlet.mvc.annotation.ResponseStatusExceptionResolver#0,org.springframework.web.servlet.mvc.support.DefaultHandlerExceptionResolver#0,org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping,org.springframework.web.servlet.mvc.HttpRequestHandlerAdapter,org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter,contactController,contactDao,contactService,org.springframework.context.annotation.internalConfigurationAnnotationProcessor,org.springframework.context.annotation.internalAutowiredAnnotationProcessor,org.springframework.context.annotation.internalRequiredAnnotationProcessor,org.springframework.context.annotation.internalCommonAnnotationProcessor,org.springframework.context.annotation.internalPersistenceAnnotationProcessor,castorMarshaller,org.springframework.context.annotation.ConfigurationClassPostProcessor.importAwareProcessor]; parent: org.springframework.beans.factory.support.DefaultListableBeanFactory@50086ff0
INFO : org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping - Mapped "{[/contacts/{id}],methods=[DELETE],params=[],headers=[],consumes=[],produces=[],custom=[]}" onto public void com.aucklanduni.spring.webservices.restful.controller.ContactController.delete(java.lang.Long)
INFO : org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping - Mapped "{[/contacts/],methods=[POST],params=[],headers=[],consumes=[],produces=[],custom=[]}" onto public com.aucklanduni.spring.webservices.domain.Contact com.aucklanduni.spring.webservices.restful.controller.ContactController.create(com.aucklanduni.spring.webservices.domain.Contact,javax.servlet.http.HttpServletResponse)
INFO : org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping - Mapped "{[/contacts/{id}],methods=[PUT],params=[],headers=[],consumes=[],produces=[],custom=[]}" onto public void com.aucklanduni.spring.webservices.restful.controller.ContactController.update(com.aucklanduni.spring.webservices.domain.Contact,java.lang.Long)
INFO : org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping - Mapped "{[/contacts],methods=[GET],params=[],headers=[],consumes=[],produces=[],custom=[]}" onto public com.aucklanduni.spring.webservices.domain.Contacts com.aucklanduni.spring.webservices.restful.controller.ContactController.listData(org.springframework.web.context.request.WebRequest)
INFO : org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping - Mapped "{[/contacts/{id}],methods=[GET],params=[],headers=[],consumes=[],produces=[],custom=[]}" onto public com.aucklanduni.spring.webservices.domain.Contact com.aucklanduni.spring.webservices.restful.controller.ContactController.findContactById(java.lang.Long)
INFO : org.springframework.beans.factory.support.DefaultListableBeanFactory - Destroying singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@1201f5bc: defining beans [mvcContentNegotiationManager,org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping#0,org.springframework.format.support.FormattingConversionServiceFactoryBean#0,org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter#0,org.springframework.web.servlet.handler.MappedInterceptor#0,org.springframework.web.servlet.mvc.method.annotation.ExceptionHandlerExceptionResolver#0,org.springframework.web.servlet.mvc.annotation.ResponseStatusExceptionResolver#0,org.springframework.web.servlet.mvc.support.DefaultHandlerExceptionResolver#0,org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping,org.springframework.web.servlet.mvc.HttpRequestHandlerAdapter,org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter,contactController,contactDao,contactService,org.springframework.context.annotation.internalConfigurationAnnotationProcessor,org.springframework.context.annotation.internalAutowiredAnnotationProcessor,org.springframework.context.annotation.internalRequiredAnnotationProcessor,org.springframework.context.annotation.internalCommonAnnotationProcessor,org.springframework.context.annotation.internalPersistenceAnnotationProcessor,castorMarshaller,org.springframework.context.annotation.ConfigurationClassPostProcessor.importAwareProcessor]; parent: org.springframework.beans.factory.support.DefaultListableBeanFactory@50086ff0
ERROR: org.springframework.web.servlet.DispatcherServlet - Context initialization failed
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'contactDao': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire method: public void com.aucklanduni.spring.webservices.service.ContactDaoImpl.setSessionFactory(org.hibernate.SessionFactory); nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [org.hibernate.SessionFactory] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {}
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:289)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1146)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:519)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:458)
    at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:296)
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:223)
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:293)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:194)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:633)
    at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:932)
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:479)
    at org.springframework.web.servlet.FrameworkServlet.configureAndRefreshWebApplicationContext(FrameworkServlet.java:651)
    at org.springframework.web.servlet.FrameworkServlet.createWebApplicationContext(FrameworkServlet.java:602)
    at org.springframework.web.servlet.FrameworkServlet.createWebApplicationContext(FrameworkServlet.java:665)
    at org.springframework.web.servlet.FrameworkServlet.initWebApplicationContext(FrameworkServlet.java:521)
    at org.springframework.web.servlet.FrameworkServlet.initServletBean(FrameworkServlet.java:462)
    at org.springframework.web.servlet.HttpServletBean.init(HttpServletBean.java:136)
    at javax.servlet.GenericServlet.init(GenericServlet.java:158)
    at org.apache.catalina.core.StandardWrapper.initServlet(StandardWrapper.java:1284)
    at org.apache.catalina.core.StandardWrapper.loadServlet(StandardWrapper.java:1197)
    at org.apache.catalina.core.StandardWrapper.load(StandardWrapper.java:1087)
    at org.apache.catalina.core.StandardContext.loadOnStartup(StandardContext.java:5210)
    at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5493)
    at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
    at org.apache.catalina.core.ContainerBase.addChildInternal(ContainerBase.java:901)
    at org.apache.catalina.core.ContainerBase.addChild(ContainerBase.java:877)
    at org.apache.catalina.core.StandardHost.addChild(StandardHost.java:632)
    at org.apache.catalina.startup.HostConfig.deployDescriptor(HostConfig.java:670)
    at org.apache.catalina.startup.HostConfig$DeployDescriptor.run(HostConfig.java:1839)
    at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:471)
    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)

宁静-context.xml中:

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" 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-app_2_5.xsd">

    <!-- The definition of the Root Spring Container shared by all Servlets and Filters -->
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/app/root-context.xml</param-value>
    </context-param>

    <!-- Creates the Spring Container shared by all Servlets and Filters -->
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>

    <!-- Configure the Web container to use Spring's DispatcherServlet. DispatcherServlet will 
         forward incoming requests and direct them to application controllers. The Spring container 
         that is used by the The DisplatcherServlet is initialised by the XML file specified by
         the "contextConfiguration" initialisation parameter (i.e. "restful-context.xml"). -->
    <servlet>
        <servlet-name>contactsService</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>/WEB-INF/app/restful-context.xml</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>

    <!-- Ensure that the servlet executes incoming requests that match the pattern "/*". This means
         that if the application is hosted within a Web container at path "Contact", and if the 
         Web container's domain name is "http://localhost:808", then the servlet will process any
         requests of the form: http://localhost:8080/Contact/, 
         e.g http://localhost:8080/Contact/contacts.   -->
    <servlet-mapping>
        <servlet-name>contactsService</servlet-name>
        <url-pattern>/*</url-pattern>
    </servlet-mapping>

</web-app>

根context.xml中:

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

    <mvc:annotation-driven>
        <mvc:message-converters>
            <bean class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter"/>
            <bean class="org.springframework.http.converter.xml.MarshallingHttpMessageConverter">
                <property name="marshaller" ref="castorMarshaller"/>
                <property name="unmarshaller" ref="castorMarshaller"/>
            </bean>
        </mvc:message-converters>
    </mvc:annotation-driven>

    <context:component-scan base-package="com.aucklanduni.spring.webservices.restful.controller,
        com.aucklanduni.spring.webservices.service" />

    <bean id="castorMarshaller" class="org.springframework.oxm.castor.CastorMarshaller">
        <property name="mappingLocation" value="classpath:oxm-mapping.xml"/>
    </bean>     

</beans>

2 个答案:

答案 0 :(得分:0)

@Resource似乎是在按名称布线的基础上工作,而不是允许通过类型的布线。您可以使用@Autowired代替,或者如果您真的想要坚持使用符合JSR-250的注释,请使用@Resource(name="springSessionFactory")。最后一种方法是将bean重命名为sessionFactory,这应该就地修复所有内容。

Documentation reference for Spring 4.0.6

答案 1 :(得分:0)

目前我添加了root-context.xml的导入行,它似乎有效:

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

    <!-- Root Context: defines shared resources visible to all other web components -->
<import resource= "classpath:restful-client-app-context.xml"/> 
</beans>
相关问题