我试图在没有任何实现的情况下使用Spring Data JPA-Crudepository use(所有默认选项)。
我的代码有这个例外:
线程“main”组织中出现异常。springframework。豆。工厂BeanCreationException:创建名为“springJpaContactService”的bean时出错:自动连线依赖项的注入失败;嵌套的异常是org。springframework。豆。工厂BeanCreationException:无法自动连接字段:private com。样品冬眠联系我们。样品。冬眠。RepositoryContactServiceImpl。联系人存储库;嵌套的异常是org。springframework。豆。工厂BeanCreationException:创建名为“contactRepository”的bean时出错:调用init方法失败;嵌套的例外是java。lang.AbstractMethodError:org。springframework。数据存储库。果心支持RepositoryFactorySupport。GetTargetPository(Lorg/springframework/data/repository/core/RepositoryInformation;)Ljava/lang/Object;在org。springframework。豆。工厂注释。AutoWiredNotationBeanPostProcessor。org上的PostProcessPropertyValue(AutowiredNotationBeanpostProcessor.java:334)。springframework。豆。工厂支持AbstractAutoWireAbleBeanFactory。populateBean(AbstractAutowireCapableBeanFactory.java:1214)位于org。springframework。豆。工厂支持AbstractAutoWireAbleBeanFactory。org上的doCreateBean(AbstractAutowireCapableBeanFactory.java:543)。springframework。豆。工厂支持AbstractAutoWireAbleBeanFactory。在org上创建Bean(AbstractAutowireCapableBeanFactory.java:482)。springframework。豆。工厂支持AbstractBeanFactory 1美元。org上的getObject(AbstractBeanFactory.java:305)。springframework。豆。工厂支持DefaultSingletonBeanRegistry。getSingleton(DefaultSingletonBeanRegistry.java:230)位于org。springframework。豆。工厂支持抽象工厂。org上的doGetBean(AbstractBeanFactory.java:301)。springframework。豆。工厂支持抽象工厂。getBean(AbstractBeanFactory.java:196)位于org。springframework。豆。工厂支持DefaultListableBeanFactory。org上的预实例化单例(DefaultListableBeanFactory.java:772)。springframework。上下文支持。AbstractApplicationContext。在org上完成BeanFactoryInitialization(AbstractApplicationContext.java:834)。springframework。上下文支持AbstractApplicationContext。在com上刷新(AbstractApplicationContext.java:537)。样品冬眠HibernateJava应用程序。main(HibernateJpaApplication.java:15)
我的课程负责Crudepository:
联系人存储库:
package com.sample.hibernate;
import java.util.List;
import org.springframework.data.repository.CrudRepository;
public interface ContactRepository extends CrudRepository<Contact, Long> {
List<Contact> findByFirstName(String firstName);
List<Contact> findByFirstNameAndLastName(String firstName, String lastName);
}
RepositoryContactService:
package com.sample.hibernate;
import java.util.List;
public interface RepositoryContactService {
List<Contact> findAll();
List<Contact> findByFirstName(String firstName);
List<Contact> findByFirstNameAndLastName(String firstName, String lastName);
}
RepositoryContactServiceImpl:
package com.sample.hibernate;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.google.common.collect.Lists;
@Service("springJpaContactService")
@Repository
@Transactional
public class RepositoryContactServiceImpl implements RepositoryContactService {
@Autowired
private ContactRepository contactRepository;
@Transactional(readOnly = true)
public List<Contact> findAll() {
return Lists.newArrayList(contactRepository.findAll());
}
@Transactional(readOnly = true)
public List<Contact> findByFirstName(String firstName) {
return contactRepository.findByFirstName(firstName);
}
@Transactional(readOnly = true)
public List<Contact> findByFirstNameAndLastName(String firstName,
String lastName) {
return contactRepository
.findByFirstNameAndLastName(firstName, lastName);
}
}
联系人:
package com.sample.hibernate;
import java.io.Serializable;
import java.util.Date;
import java.util.HashSet;
import java.util.Set;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.EntityResult;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.OneToMany;
import javax.persistence.SqlResultSetMapping;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
import javax.persistence.Version;
@Entity
@Table(name = "contact")
@NamedQueries({
@NamedQuery(name="Contact.findAll", query="select c from Contact c"),
@NamedQuery(name = "Contact.findAllWithDetail", query = "select distinct c from Contact c left join fetch c.contactTelDetails t left join fetch c.hobbies h"),
@NamedQuery(name = "Contact.findById", query = "select distinct c from Contact c left join fetch c.contactTelDetails t left join fetch c.hobbies h where c.id = :id") })
@SqlResultSetMapping(name="contactResult", entities=@EntityResult(entityClass=Contact.class)
)
public class Contact implements Serializable {
private static final long serialVersionUID = -8008307767408320097L;
private Long id;
private int version;
private String firstName;
private String lastName;
private Date birthDate;
private Set<ContactTelDetail> contactTelDetails = new HashSet<ContactTelDetail>();
private Set<Hobby> hobbies = new HashSet<Hobby>();
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "ID")
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
@Version
@Column(name = "VERSION")
public int getVersion() {
return version;
}
public void setVersion(int version) {
this.version = version;
}
@Column(name = "FIRST_NAME")
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
@Column(name = "LAST_NAME")
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
@Temporal(TemporalType.DATE)
@Column(name = "BIRTH_DATE")
public Date getBirthDate() {
return birthDate;
}
public void setBirthDate(Date birthDate) {
this.birthDate = birthDate;
}
@OneToMany(mappedBy = "contact", cascade = CascadeType.ALL, orphanRemoval = true)
public Set<ContactTelDetail> getContactTelDetails() {
return contactTelDetails;
}
public void setContactTelDetails(Set<ContactTelDetail> contactTelDetails) {
this.contactTelDetails = contactTelDetails;
}
public void addContactTelDetail(ContactTelDetail contactTelDetail) {
contactTelDetail.setContact(this);
getContactTelDetails().add(contactTelDetail);
}
public void removeContactTelDetail(ContactTelDetail contactTelDetail) {
getContactTelDetails().remove(contactTelDetail);
}
@ManyToMany
@JoinTable(name = "contact_hobby_detail", joinColumns = @JoinColumn(name = "CONTACT_ID"), inverseJoinColumns = @JoinColumn(name = "HOBBY_ID"))
public Set<Hobby> getHobbies() {
return hobbies;
}
public void setHobbies(Set<Hobby> hobbies) {
this.hobbies = hobbies;
}
@Override
public String toString() {
return "Contact [id=" + id + ", version=" + version + ", firstName="
+ firstName + ", lastName=" + lastName + ", birthDate="
+ birthDate + "]";
}
}
app-config.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:p="http://www.springframework.org/schema/p"
xmlns:jpa="http://www.springframework.org/schema/data/jpa" xmlns:jdbc="http://www.springframework.org/schema/jdbc"
xmlns:util="http://www.springframework.org/schema/util"
xsi:schemaLocation="
http://www.springframework.org/schema/jdbc
http://www.springframework.org/schema/jdbc/spring-jdbc.xsd
http://www.springframework.org/schema/data/jpa
http://www.springframework.org/schema/data/jpa/spring-jpa.xsd
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/util
http://www.springframework.org/schema/util/spring-util.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
<jdbc:embedded-database id="dataSource" type="H2">
<jdbc:script location="classpath:sql/schema.sql" />
<jdbc:script location="classpath:sql/test-data.sql" />
</jdbc:embedded-database>
<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
<property name="entityManagerFactory" ref="emf" />
</bean>
<tx:annotation-driven transaction-manager="transactionManager" />
<bean id="emf"
class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="jpaVendorAdapter">
<bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter" />
</property>
<property name="packagesToScan" value="com.sample.hibernate" />
<property name="jpaProperties">
<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">false</prop>
</props>
</property>
</bean>
<context:component-scan base-package="com.sample.hibernate" />
<jpa:repositories base-package="com.sample.hibernate"
entity-manager-factory-ref="emf" transaction-manager-ref="transactionManager" />
</beans>
我很乐意得到任何线索。。这个例子来自一本书,我已经用它在线教程检查了3次,我不知道为什么它不能按预期工作。。。
您的@Repository
注释放错了位置。您的ContactRepository
类应该是带有@Repository
注释的类。
Spring无法自动连接您的联系人存储库
,因为它不是由Spring管理的,因为您没有使用@Repository
对它进行注释。
另外,您的RepositoryContaltServiceImpl上有@Transactional
,这将使该类中的所有公共方法都是事务性的,因此将@Transactional
放在该类中的每个方法上是多余的。如果您需要每个方法具有不同的事务性行为,您可以在每个方法上保留@Transactional
注释,并对每个方法进行修改以满足您的需求。然后您可以从类中删除@Transactional
。
但是,如果每个方法都需要相同类型的事务,如所提供的示例中所示,则可以从每个方法中删除@Transactional
注释,并将@Transactional
保留在类级别,并在其中指定只读
属性。
有注释的一切都很好——不需要将@仓库移动到联系仓库。这是因为联系仓库扩展了CrudRepository,它告诉Spring,他可以在RepositoryContictServiceImpl中引用自动连接。问题是在maven配置中(因为你的想法是正确的,谢谢你)。我改变了:
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.3.0.M2</version>
<relativePath /> <!-- lookup parent from repository -->
</parent>
到
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.0.2.RELEASE</version>
<relativePath /> <!-- lookup parent from repository -->
</parent>
现在,存储库按预期工作。
问题内容: 我遵循了此处发布的教程,以获取与Spring Data JPA一起使用的基础应用程序。现在,我如何理解,使用配置 应该会导致Spring Data JPA对该软件包进行扫描,以获取扩展的接口并为其创建一个concreate bean,以便可以使用简单的Spring在我的服务类中的任何位置使用它。但是它失败了,说它找不到具有className的bean(这是创建时仅使用去大写字母的Cla
我从spring-boot开始,我有一些配置问题。我无法自动操作某些服务。我得到一个BeanCreationException。 Hier是我自动执行服务的控制器: 控制台的错误: 当我从compenentscan中删除两个控制器时,我不会出现这个错误,但我需要扫描这些控制器来访问endpoint。
我正在努力学习一本书名为《SpringMVC初学者指南》的书,我一直在努力创建存储库对象。我不断地得到一个BeanCreationException。不知道我还错过了什么。我想知道是否有人能帮我解决这个问题。 请在下面找到我的代码。谢谢 BeanCreationException XML文件: ProductCrontroller: 产品存储库: InMemoryProductRepository
我在我的服务器Tomcat上部署project后遇到了这个错误。我发现了许多类似的问题,但我没有找到我的问题的答案。 ApplicationContext.xml CategoryDaoDbImpl类 Category类
这是我的FileStorageProperties类: 这让我想到:未通过@enableconfigurationproperties注册或标记为spring组件。 这是我的文件存储服务: 这给了我一个错误:无法自动关联未找到类型的bean。 这是我的项目结构: 当我试图运行它时,它给了我: 申请无法启动 描述: com中构造函数的参数0。穆阿。cse616.服务。FileStorageServi
我正在使用java配置而不是XML迁移到spring Security4.0.1。当我自动操作时,它会给出以下错误: HTTP状态500-org.springframework.beans.factory.beanCreationException:创建名为“ScopedTarget.UsersComponent”的bean时出错:注入autowired依赖项失败;嵌套异常为org.springf