3

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

I have the following setup.

@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);
}

I have tried to annotate this with @Service and @Repository but to no avail.

@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>

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

Thanks in advance.

naXa stands with Ukraine
  • 35,493
  • 19
  • 190
  • 259
user1089599
  • 303
  • 6
  • 20

2 Answers2

4

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

naXa stands with Ukraine
  • 35,493
  • 19
  • 190
  • 259
Koitoer
  • 18,778
  • 7
  • 63
  • 86
1

I believe you just need to remove this line:

<bean id="userRepository" class="com.planit.persistence.registration.UserRepository"/>

See the SpringData docs here.

mdatwood
  • 98
  • 1
  • 5