无法自动装配存储库服务Spring JPA [英] Cannot Autowire repository service Spring JPA

查看:303
本文介绍了无法自动装配存储库服务Spring JPA的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am trying to use a User repository in my code, but intellij is giving me the error.

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 <注释这个/ code>和 @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>

在spring.xml的行中出错我在哪里定义 userRepository bean。

Error occurs in the line of my spring.xml where I am defining my userRepository bean.

提前致谢。

推荐答案

缺少@Repository ,尝试在repo的接口上面添加并删除Spring中不允许的bean声明。

@Repository is missing, try adding above the interface of the repo and remove bean declaration that is not allowed in Spring.

这篇关于无法自动装配存储库服务Spring JPA的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆