gpt4 book ai didi

java - 无法 Autowiring 存储库服务 Spring JPA

转载 作者:塔克拉玛干 更新时间:2023-11-02 07:47:33 26 4
gpt4 key购买 nike

我正尝试在我的代码中使用用户存储库,但 intellij 给我错误。

interface is not allowed for non abstract beans

我有以下设置。

@Entity
public class User {

@Id
private long id;
private String firstName;
private String lastName;
private String profileUrl;
private String email;
private String location;

protected User(){};

public User(String first, String last, String profileUrl, String email, String location){
this.firstName = first;
this.lastName = last;
this.profileUrl = profileUrl;
this.email = email;
this.location = location;

}

public User(Person person){
this.firstName = person.getName().getGivenName();
this.lastName = person.getName().getFamilyName();
this.profileUrl = person.getImage().getUrl();
this.email = person.getEmails().get(0).getValue();
this.location = person.getCurrentLocation();
}
}


public interface UserRepository extends CrudRepository<User, Long> {

List<User> findByProfileId(long id);
}

我尝试用 @Service@Repository 对此进行注释,但无济于事。

@Component
public class UserRepositoryImpl {

@Autowired
private UserRepository userRepository;


public void doSomething() {
List<User> byProfileId = userRepository.findByProfileId(100);
}
}

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:tx="http://www.springframework.org/schema/tx"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:rep="http://www.springframework.org/schema/data/jpa"
xsi:schemaLocation="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-3.0.xsd
http://www.springframework.org/schema/data/jpa
http://www.springframework.org/schema/data/jpa/spring-jpa.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">

<rep:repositories base-package="com.planit.persistence.*"/>
<bean id="userRepository" class="com.planit.persistence.registration.UserRepository"/>

<!-- DB CONNECTION-->
<bean class="java.net.URI" id="dbUrl">
<constructor-arg value="#{T(com.ProjectUtils).getDBUrl()}"/>
</bean>

<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="org.postgresql.Driver"/>
<property name="url"
value="#{ 'jdbc:postgresql://' + @dbUrl.getHost() + ':' + @dbUrl.getPort() + @dbUrl.getPath() }"/>
<property name="username" value="#{ @dbUrl.getUserInfo().split(':')[0] }"/>
<property name="password" value="#{ @dbUrl.getUserInfo().split(':')[1] }"/>
</bean>


<bean id="myEmf"
class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="dataSource" ref="dataSource"/>
<property name="packagesToScan" value="com.planit.persistence"/>
<property name="jpaVendorAdapter">
<bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
<property name="showSql" value="true"/>
<property name="generateDdl" value="true"/>
<property name="database" value="POSTGRESQL"/>
</bean>
</property>
</bean>

<bean id="txManager" class="org.springframework.orm.jpa.JpaTransactionManager">
<property name="entityManagerFactory" ref="myEmf"/>
</bean>

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

错误发生在我定义 userRepository bean 的 spring.xml 行中。

提前致谢。

最佳答案

@Repository 丢失,尝试在 repo 接口(interface)上方添加并删除 Spring 中不允许的 bean 声明。

关于java - 无法 Autowiring 存储库服务 Spring JPA,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21923058/

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