无法自动装配存储库服务Spring JPA


问题内容

我正在尝试在代码中使用用户存储库,但是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>

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

提前致谢。


问题答案:

@Repository 丢失了,请尝试在仓库的接口上方添加并删除Spring不允许的bean声明。