gpt4 book ai didi

java - 无法在 Spring Autowiring 字段。为什么?

转载 作者:IT老高 更新时间:2023-10-28 21:14:01 26 4
gpt4 key购买 nike

我不断收到此错误,但不知道为什么.. 是的,我知道很多人有类似的问题,但阅读他们得到的答案并不能解决我的问题。

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'contactController': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private net.service.ContactService net.controller.ContactController.contactService; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No matching bean of type [net.service.ContactService] 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)}

这里是 Controller :

@Controller
@SessionAttributes
public class ContactController {

@Autowired
private ContactService contactService;
//methods...


}

ContactServiceImpl

@Service("contactService")
@Transactional(propagation = Propagation.SUPPORTS, readOnly = true)
public class ContactServiceImpl implements ContactService {

@Autowired
private ContactDao contactDao;

public ContactServiceImpl() {
}

@Override
@Transactional(propagation = Propagation.REQUIRED, readOnly = false)
public void addContact(Contact contact) {
contactDao.saveContact(contact);
}

@Override
public List<Contact> getContacts() {
return contactDao.getAllContacts();
}

}

ContactDaoImpl

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

@Autowired
private SessionFactory sessionFactory;

@Override
public void saveContact(Contact contact) {
sessionFactory.getCurrentSession().saveOrUpdate(contact);
}

@Override
@SuppressWarnings("unchecked")
public List<Contact> getAllContacts() {
return (List<Contact>) sessionFactory.getCurrentSession().createQuery("from contact c").list();
}

}

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

<context:property-placeholder location="classpath:jdbc.properties" />
<context:component-scan base-package="net.controller" />


<tx:annotation-driven transaction-manager="hibernateTransactionManager" />

<bean id="viewResolver"
class="org.springframework.web.servlet.view.UrlBasedViewResolver">
<property name="viewClass"
value="org.springframework.web.servlet.view.JstlView" />
<property name="prefix" value="/WEB-INF/jsp/" />
<property name="suffix" value=".jsp" />
</bean>

<bean id="dataSource"
class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="${database.driver}" />
<property name="url" value="${database.url}" />
<property name="username" value="${database.user}" />
<property name="password" value="${database.password}" />
</bean>

<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="annotatedClasses">
<list>
<value>net.form.Contact</value>
</list>
</property>


<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">${hibernate.dialect}</prop>
<prop key="hibernate.show_sql">${hibernate.show_sql}</prop>
</props>
</property>
</bean>

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

最佳答案

在 spring servlet .xml 中:

<context:component-scan base-package="net.controller" />

(我假设服务实现与服务接口(interface)“net.service”在同一个包中)

我认为您必须将包 net.service(或所有网络)添加到组件扫描中。目前 spring 仅在 net.controller 中搜索组件,并且由于您的服务 impl 在 net.service 中,因此它不会被 spring 实例化。

关于java - 无法在 Spring Autowiring 字段。为什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11954461/

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