我正在尝试将JpaRepository添加到我的Spring MVC JPA项目中。
现在我的配置一切都很好。我有一个简单的模型(基于fruzenshtein的项目,https://github.com/Fruzenshtein/spr-data)。
Model: Shop
Service: ShopService
Controller: ShopController
例如在 ShopServiceImpl.java 中我只有一个方法:
@Transactional
public List<Shop> getAll() {
List<Shop> result = em.createQuery("SELECT s FROM Shop s", Shop.class).getResultList();
return result;
}
并在ShopController.java中添加我的@Autowired
@Autowired
private ShopService shopSvc;
此时一切都好。
好吧,我想添加我自己的存储库,如下所示:
@Repository
public interface ShopRepository extends JpaRepository<Shop, Integer> {
}
并在ShopServiceImpl.java中添加@Autowired
@Autowired
private ShopRepository shopRepository;
我收到此错误:
**org.springframework.beans.factory.BeanCreationException:Error creating bean with name 'shopServiceImpl': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private org.jpa.repository.ShopRepository
org.jpa.service.ShopServiceImpl.shopRepository; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'shopRepository': FactoryBean threw exception on object creation; nested exception is java.lang.IllegalArgumentException: [Assertion failed] - this argument is required; it must not be null**
我知道这是以前发布的旧问题,但在两周后尝试修复此错误(我想我已经阅读了stackoverflow中的所有论坛)我专门用我自己的代码发布我的问题/问题等待可能的解决方案
这是我的代码:
型号:
public class Shop {
@Id
@GeneratedValue
private Integer id;
private String name;
@Column(name = "employees_number")
private Integer emplNumber;
// Constructor obligatorio
public Shop() {
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getEmplNumber() {
return emplNumber;
}
public void setEmplNumber(Integer emplNumber) {
this.emplNumber = emplNumber;
}
}
存储库:
@Repository
public interface ShopRepository extends JpaRepository<Shop, Integer> {
List<Shop> findByShopId(Long userId);
}
ShopService:
public interface ShopService {
public List<Shop> findAll();
}
ShopServiceImpl:
@Service
public class ShopServiceImpl implements ShopService {
private static final Log log = LogFactory.getLog(ShopServiceImpl.class);
@Autowired
private ShopRepository shopRepository;
// An EntityManager will be automatically injected from EntityManagerFactory
// setup on spring-context.xml
// @PersistenceContext
// private EntityManager em;
@Transactional
public List<Shop> findAll() {
return shopRepository.findAll();
}
}
ShopController:
@Controller
@RequestMapping(value = "/shop")
public class ShopController {
@Autowired
private ShopServiceImpl shopService;
@RequestMapping(value = "/list", method = RequestMethod.GET)
public ModelAndView shopListPage() {
ModelAndView mav = new ModelAndView("shop-list");
List<Shop> shopList = shopService.findAll();
if (shopList != null) {
mav.addObject("shopList", shopList);
return mav;
} else {
return null;
}
}
}
的web.xml:
<?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">
<display-name>jpa-hibernate</display-name>
<servlet>
<description>
</description>
<display-name>jpa</display-name>
<servlet-name>jpa</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>jpa</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
</welcome-file-list>
</web-app>
JPA-servlet.xml中:
<beans:beans xmlns="http://www.springframework.org/schema/mvc"
xmlns:p="http://www.springframework.org/schema/p" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:jpa="http://www.springframework.org/schema/data/jpa" xmlns:beans="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.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.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd http://www.springframework.org/schema/data/jpa
http://www.springframework.org/schema/data/jpa/spring-jpa.xsd"
xmlns:tx="http://www.springframework.org/schema/tx">
<!-- http://www.journaldev.com/7655/spring-orm-example-with-jpa-hibernate-transactions -->
<!-- http://gordondickens.com/wordpress/2011/08/01/simpler-jpa-with-spring-data-jpa/ -->
<!-- <context:annotation-config /> -->
<!-- <beans:bean class="org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor"
/> -->
<!-- Scans the classpath for annotated components that will be auto-registered
as Spring beans -->
<context:component-scan base-package="org.jpa" />
<context:component-scan base-package="org.jpa.repository" />
<context:component-scan base-package="org.jpa.service" />
<context:component-scan base-package="org.jpa.controller" />
<context:component-scan base-package="org.jpa.model" />
<!-- Resolves views selected for rendering by @Controllers to .jsp resources
in the /WEB-INF/views directory -->
<beans:bean
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<beans:property name="prefix" value="/WEB-INF/views/" />
<beans:property name="suffix" value=".jsp" />
</beans:bean>
<!-- DataSource para Microsoft Sql Server 2005 -->
<beans:bean id="dataSource" name="dataSource"
class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<beans:property name="driverClassName"
value="com.microsoft.sqlserver.jdbc.SQLServerDriver" />
<beans:property name="url"
value="" />
<beans:property name="username" value="" />
<beans:property name="password" value="" />
</beans:bean>
<beans:bean id="entityManagerFactory"
class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean"
p:packagesToScan="org.jpa" p:dataSource-ref="dataSource">
<beans:property name="jpaVendorAdapter">
<beans:bean
class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
<beans:property name="generateDdl" value="true" />
<beans:property name="showSql" value="true" />
</beans:bean>
</beans:property>
</beans:bean>
<!-- Transactions -->
<beans:bean id="transactionManager"
class="org.springframework.orm.jpa.JpaTransactionManager">
<beans:property name="entityManagerFactory" ref="entityManagerFactory" />
</beans:bean>
<!-- enable the configuration of transactional behavior based on annotations -->
<tx:annotation-driven />
<!-- <tx:annotation-driven transaction-manager="transactionManager" /> -->
<!-- JPA -->
<jpa:repositories base-package="org.jpa.repository" />
<!-- <jpa:repositories base-package="org.jpa.repository" entity-manager-factory-ref="entityManagerFactory"
transaction-manager-ref="transactionManager" /> -->
<!-- Activates various annotations to be detected in bean classes e.g: @Autowired -->
</beans:beans>
的pom.xml:
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>jpa-repository</groupId>
<artifactId>jpa-repository</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>
<properties>
<spring.version>3.1.4.RELEASE</spring.version>
<hibernate.version>3.6.0.Final</hibernate.version>
<org.slf4j-version>1.6.6</org.slf4j-version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-orm</artifactId>
<version>${spring.version}</version>
</dependency>
<!-- Hibernate -->
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>${hibernate.version}</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-entitymanager</artifactId>
<version>${hibernate.version}</version>
</dependency>
<dependency>
<groupId>org.hibernate.javax.persistence</groupId>
<artifactId>hibernate-jpa-2.0-api</artifactId>
<version>1.0.1.Final</version>
</dependency>
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-jpa</artifactId>
<version>1.0.0.RELEASE</version>
</dependency>
<dependency>
<groupId>javax.annotation</groupId>
<artifactId>jsr250-api</artifactId>
<version>1.0</version>
</dependency>
<dependency>
<groupId>commons-dbcp</groupId>
<artifactId>commons-dbcp</artifactId>
<version>1.4</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
<version>1.1.2</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.0.1</version>
</dependency>
<dependency>
<groupId>taglibs</groupId>
<artifactId>standard</artifactId>
<version>1.1.2</version>
</dependency>
<dependency>
<groupId>xalan</groupId>
<artifactId>xalan</artifactId>
<version>2.7.1</version>
</dependency>
<!-- Logging -->
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>${org.slf4j-version}</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>jcl-over-slf4j</artifactId>
<version>${org.slf4j-version}</version>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>${org.slf4j-version}</version>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.15</version>
<exclusions>
<exclusion>
<groupId>javax.mail</groupId>
<artifactId>mail</artifactId>
</exclusion>
<exclusion>
<groupId>javax.jms</groupId>
<artifactId>jms</artifactId>
</exclusion>
<exclusion>
<groupId>com.sun.jdmk</groupId>
<artifactId>jmxtools</artifactId>
</exclusion>
<exclusion>
<groupId>com.sun.jmx</groupId>
<artifactId>jmxri</artifactId>
</exclusion>
</exclusions>
<scope>runtime</scope>
</dependency>
</dependencies>
<build>
<sourceDirectory>src</sourceDirectory>
<plugins>
<plugin>
<artifactId>maven-war-plugin</artifactId>
<version>2.4</version>
<configuration>
<warSourceDirectory>WebContent</warSourceDirectory>
</configuration>
</plugin>
</plugins>
</build>
</project>
的persistence.xml:
<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.0"
xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/persistence
http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd">
<persistence-unit name="persistenceUnit"
transaction-type="RESOURCE_LOCAL">
<!-- <persistence-unit name="persistenceUnit" transaction-type="JTA"> -->
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<properties>
<property name="hibernate.dialect" value="org.hibernate.dialect.SQLServerDialect" />
<!-- Show and print nice SQL on stdout -->
<property name="hibernate.show_sql" value="true" />
<property name="hibernate.format_sql" value="true" />
<property name="hibernate.hbm2ddl.auto" value="validate" />
<!-- <property name="hibernate.ejb.naming_strategy" value="org.hibernate.cfg.ImprovedNamingStrategy"
/> -->
<property name="hibernate.connection.charSet" value="UTF-8" />
</properties>
</persistence-unit>
</persistence>
添加了整个堆栈跟踪日志:
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'shopController': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private org.jpa.service.ShopServiceImpl org.jpa.controller.ShopController.shopService; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'shopServiceImpl': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private org.jpa.repository.ShopRepository org.jpa.service.ShopServiceImpl.shopRepository; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'shopRepository': FactoryBean threw exception on object creation; nested exception is java.lang.IllegalArgumentException: [Assertion failed] - this argument is required; it must not be null
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:287)
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:607)
at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:925)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:472)
at org.springframework.web.servlet.FrameworkServlet.configureAndRefreshWebApplicationContext(FrameworkServlet.java:631)
at org.springframework.web.servlet.FrameworkServlet.createWebApplicationContext(FrameworkServlet.java:588)
at org.springframework.web.servlet.FrameworkServlet.createWebApplicationContext(FrameworkServlet.java:645)
at org.springframework.web.servlet.FrameworkServlet.initWebApplicationContext(FrameworkServlet.java:508)
at org.springframework.web.servlet.FrameworkServlet.initServletBean(FrameworkServlet.java:449)
at org.springframework.web.servlet.HttpServletBean.init(HttpServletBean.java:133)
at javax.servlet.GenericServlet.init(GenericServlet.java:211)
at org.apache.catalina.core.StandardWrapper.loadServlet(StandardWrapper.java:1029)
at org.apache.catalina.core.StandardWrapper.load(StandardWrapper.java:862)
at org.apache.catalina.core.StandardContext.loadOnStartup(StandardContext.java:4013)
at org.apache.catalina.core.StandardContext.start(StandardContext.java:4357)
at org.apache.catalina.core.ContainerBase.start(ContainerBase.java:1083)
at org.apache.catalina.core.StandardHost.start(StandardHost.java:789)
at org.apache.catalina.core.ContainerBase.start(ContainerBase.java:1083)
at org.apache.catalina.core.StandardEngine.start(StandardEngine.java:478)
at org.apache.catalina.core.StandardService.start(StandardService.java:480)
at org.apache.catalina.core.StandardServer.start(StandardServer.java:2313)
at org.apache.catalina.startup.Catalina.start(Catalina.java:556)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at org.apache.catalina.startup.Bootstrap.start(Bootstrap.java:287)
at org.apache.catalina.startup.Bootstrap.main(Bootstrap.java:425)
Caused by: org.springframework.beans.factory.BeanCreationException: Could not autowire field: private org.jpa.service.ShopServiceImpl org.jpa.controller.ShopController.shopService; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'shopServiceImpl': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private org.jpa.repository.ShopRepository org.jpa.service.ShopServiceImpl.shopRepository; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'shopRepository': FactoryBean threw exception on object creation; nested exception is java.lang.IllegalArgumentException: [Assertion failed] - this argument is required; it must not be null
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:513)
at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:92)
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:284)
... 34 more
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'shopServiceImpl': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private org.jpa.repository.ShopRepository org.jpa.service.ShopServiceImpl.shopRepository; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'shopRepository': FactoryBean threw exception on object creation; nested exception is java.lang.IllegalArgumentException: [Assertion failed] - this argument is required; it must not be null
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:287)
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.findAutowireCandidates(DefaultListableBeanFactory.java:871)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:813)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:730)
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:485)
... 36 more
Caused by: org.springframework.beans.factory.BeanCreationException: Could not autowire field: private org.jpa.repository.ShopRepository org.jpa.service.ShopServiceImpl.shopRepository; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'shopRepository': FactoryBean threw exception on object creation; nested exception is java.lang.IllegalArgumentException: [Assertion failed] - this argument is required; it must not be null
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:513)
at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:92)
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:284)
... 47 more
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'shopRepository': FactoryBean threw exception on object creation; nested exception is java.lang.IllegalArgumentException: [Assertion failed] - this argument is required; it must not be null
at org.springframework.beans.factory.support.FactoryBeanRegistrySupport.doGetObjectFromFactoryBean(FactoryBeanRegistrySupport.java:149)
at org.springframework.beans.factory.support.FactoryBeanRegistrySupport.getObjectFromFactoryBean(FactoryBeanRegistrySupport.java:102)
at org.springframework.beans.factory.support.AbstractBeanFactory.getObjectForBeanInstance(AbstractBeanFactory.java:1442)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:305)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:193)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.findAutowireCandidates(DefaultListableBeanFactory.java:871)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:813)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:730)
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:485)
... 49 more
Caused by: java.lang.IllegalArgumentException: [Assertion failed] - this argument is required; it must not be null
at org.springframework.util.Assert.notNull(Assert.java:112)
at org.springframework.util.Assert.notNull(Assert.java:123)
at org.springframework.data.jpa.repository.support.SimpleJpaRepository.<init>(SimpleJpaRepository.java:74)
at org.springframework.data.jpa.repository.support.JpaRepositoryFactory.getTargetRepository(JpaRepositoryFactory.java:94)
at org.springframework.data.jpa.repository.support.JpaRepositoryFactory.getTargetRepository(JpaRepositoryFactory.java:69)
at org.springframework.data.repository.core.support.RepositoryFactorySupport.getRepository(RepositoryFactorySupport.java:146)
at org.springframework.data.repository.core.support.RepositoryFactoryBeanSupport.getObject(RepositoryFactoryBeanSupport.java:120)
at org.springframework.data.repository.core.support.RepositoryFactoryBeanSupport.getObject(RepositoryFactoryBeanSupport.java:39)
at org.springframework.beans.factory.support.FactoryBeanRegistrySupport.doGetObjectFromFactoryBean(FactoryBeanRegistrySupport.java:142)
... 57 more
2016-02-15 12:07:39 StandardContext[/jpa-hibernate]El Servlet /jpa-hibernate lanzó excepción de load()
javax.servlet.ServletException: Servlet.init() para servlet jpa lanzó excepción
at org.apache.catalina.core.StandardWrapper.loadServlet(StandardWrapper.java:1071)
at org.apache.catalina.core.StandardWrapper.load(StandardWrapper.java:862)
at org.apache.catalina.core.StandardContext.loadOnStartup(StandardContext.java:4013)
at org.apache.catalina.core.StandardContext.start(StandardContext.java:4357)
at org.apache.catalina.core.ContainerBase.start(ContainerBase.java:1083)
at org.apache.catalina.core.StandardHost.start(StandardHost.java:789)
at org.apache.catalina.core.ContainerBase.start(ContainerBase.java:1083)
at org.apache.catalina.core.StandardEngine.start(StandardEngine.java:478)
at org.apache.catalina.core.StandardService.start(StandardService.java:480)
at org.apache.catalina.core.StandardServer.start(StandardServer.java:2313)
at org.apache.catalina.startup.Catalina.start(Catalina.java:556)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
----- Root Cause -----
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'shopController': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private org.jpa.service.ShopServiceImpl org.jpa.controller.ShopController.shopService; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'shopServiceImpl': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private org.jpa.repository.ShopRepository org.jpa.service.ShopServiceImpl.shopRepository; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'shopRepository': FactoryBean threw exception on object creation; nested exception is java.lang.IllegalArgumentException: [Assertion failed] - this argument is required; it must not be null
为github添加了代码: https://github.com/elecdesa/jpa-repository/
由于
答案 0 :(得分:1)
在您的存储库中,您列出了方法findByShopId
,但您的类没有shopId
属性。删除声明,然后使用find
。
(另外,对于任何新的Spring项目,使用Boot;它会大大减少样板配置。另外,尽可能使用构造函数注入而不是字段注入。)
答案 1 :(得分:0)
答案 2 :(得分:-1)
您是否尝试将@Repository注释从ShopRepository接口移动到实现它的实际类?