我在autowire
课程JpaRepository
时遇到此错误。
我尝试保存数据。然后我得到了以下错误。
这是我的错误.....
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'cutomerController': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: public com.nought.service.MyDBService com.nought.controller.CutomerController.myDBService; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [com.nought.service.MyDBService] 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)}
这是我的CutomerController
班级......
@Controller
public class CutomerController {
@Autowired
public MyDBService myDBService;
@RequestMapping(value = "/addCutomer", method = RequestMethod.GET)
public String addCustomer() {
return "add-customer";
}
@ModelAttribute("addCustomer")
public Customer construct() {
return new Customer();
}
@RequestMapping(value = "/addCustomer", method = RequestMethod.POST)
public String doRegister(@ModelAttribute("addCustomer") Customer customer) {
myDBService.saveCustomer(customer);
return "success";
}
}
这是我的Repository类....
public interface CustomerRepository extends JpaRepository<Customer,Integer> {
}
这是我的服务类.....
@Service
public class MyDBService {
@Autowired
private CustomerRepository customerRepository;
public void saveCustomer(Customer customer) {
customerRepository.save(customer);
}
}
这是我的applicationContext.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:jpa="http://www.springframework.org/schema/data/jpa" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.1.xsd
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.1.xsd
http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa-1.3.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.1.xsd">
<context:component-scan base-package="com.nought">
<context:exclude-filter type="annotation"
expression="org.springframework.stereotype.Controller" />
</context:component-scan>
<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
<property name="dataSource" ref="dataSource" />
</bean>
<bean
class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean"
id="emf">
<property name="packagesToScan" value="com.nought.entity" />
<property name="dataSource" ref="dataSource" />
<property name="jpaProperties">
<props>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.hbm2ddl.auto">create</prop>
<prop key="hibernate.connection.driver_class">com.mysql.jdbc.Driver</prop>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
</props>
</property>
<property name="persistenceProvider">
<bean class="org.hibernate.jpa.HibernatePersistenceProvider" />
</property>
</bean>
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
<property name="url" value="jdbc:mysql://localhost:3306/hibernate" />
<property name="username" value="root" />
<property name="password" value="" />
</bean>
<import resource="security.xml"/>
<tx:annotation-driven transaction-manager="transactionManager" />
<mvc:annotation-driven />
<jpa:repositories base-package="com.nought.repository"
entity-manager-factory-ref="emf" />
</beans>