gpt4 book ai didi

java - Spring应用程序中的NoSuchBeanDefinitionException

转载 作者:太空宇宙 更新时间:2023-11-04 14:34:11 24 4
gpt4 key购买 nike

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

这是我的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:

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");
}

}

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

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());
}

}

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

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

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

这是我的restful-client-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: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>

最新日志错误:

[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)

这是我的 web.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>

restful-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>

根上下文.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"
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 -->

</beans>

最佳答案

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

Documentation reference for Spring 4.0.6

关于java - Spring应用程序中的NoSuchBeanDefinitionException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25796945/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com