3

I have two interface with same name but in different packages. As I autowired them I have to give them name. My interfaces are com.example.mysql.UserRepository and com.example.mongo.UserRepository.

So I have declared them like this:

@Repository(value = "mysqlrepo")
public interface UserRepository extends JpaRepository<User,Long> ...

and

@Repository(value = "mongorepo")
public interface UserRepository extends MongoRepository<User,String> ...

and used them like this:

@Autowired
ee3.demo.repositories.mysql.UserRepository userRepository;

@Qualifier("mongorepo")
@Autowired
UserRepository userRepository1;

Now I wonder how can I do this with beans configure file. I tried <bean id="mysqlService" lazy-init="true" class="ee3.demo.repositories.mysql.UserRepository"/> but I am getting error interface not allowed for non abstract beans. I wondering what is the correct way of doing this, this question if different with this question as this question have just one interface and don't need to use qualifier to specify which interface he need What I have tried so far is HERE

joe gates
  • 459
  • 1
  • 5
  • 15
  • 1
    I think you should use [`MongoRepository`](https://docs.spring.io/spring-data/mongodb/docs/current/api/org/springframework/data/mongodb/repository/MongoRepository.html) interface instead of `JpaRepository` in the second case. – naXa stands with Ukraine Oct 06 '17 at 10:23
  • yes,Thanks, I fixed it – joe gates Oct 06 '17 at 10:25
  • You say that your question is different from the linked one but you get the same error. If you try proposed solutions (remove bad `` declaration from your config) you'll resolve your error. – naXa stands with Ukraine Oct 06 '17 at 10:59

2 Answers2

1

You must desclare alias for these beans

For example

<bean id="mysqlService" lazy-init="true" alias="mysqlrepoAlias"
 class="ee3.demo.repositories.mysql.UserRepository"/>

<bean id="mongorepoService" lazy-init="true"  alias="mongorepoAlias"class="ee3.demo.repositories.mysql.UserRepository"/>
java.nazif
  • 713
  • 1
  • 9
  • 18
  • are you sure I can use alias like this ? and please give some more detail about `alias` jobs – joe gates Oct 06 '17 at 09:37
  • @Joe you can look https://docs.spring.io/spring/docs/3.2.6.RELEASE/spring-framework-reference/html/beans.html#beans-beanname-alias – java.nazif Oct 06 '17 at 09:44
  • Well, This is not what I am looking for, my problem is not use alias for class name, my problem is `interface` that can't used in beans and I need to name this repository – joe gates Oct 06 '17 at 10:17
  • I was looking for you ..I find this example .I think it will fix your issue http://www.mytechnotes.biz/2013/03/spring-jparepository-example-in-memory.html – java.nazif Oct 06 '17 at 10:44
  • Not really, this is not what I am looking for, I am configuring my application use xml files – joe gates Oct 06 '17 at 11:16
  • @java.nazif the domain name has changed https://tshikatshikaaa.blogspot.com/2013/03/spring-jparepository-example-in-memory.html – Jérôme Verstrynge Feb 09 '20 at 19:28
1

If you want to configure Spring Data using XML config you don't need to declare beans for all your repositories. See Spring Data docs here. It's easier to specify a base package for scanning:

<?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:jpa="http://www.springframework.org/schema/data/jpa"
  xmlns:mongo="http://www.springframework.org/schema/data/mongo"
  xsi:schemaLocation="http://www.springframework.org/schema/beans 
    http://www.springframework.org/schema/beans/spring-beans.xsd
    http://www.springframework.org/schema/data/jpa 
    http://www.springframework.org/schema/data/jpa/spring-jpa-1.0.xsd
    http://www.springframework.org/schema/data/mongo
    http://www.springframework.org/schema/data/mongo/spring-mongo-1.0.xsd">

  <!-- 
    Configure Spring Data JPA / MongoDB and set the base package of the 
    repository interfaces 
  -->
  <jpa:repositories base-package="com.example.mysql"/>
  <mongo:repositories base-package="com.example.mongo"/>

 ...

Read more in this post.


EDIT

You need to give different names to your repositories (use value attribute on annotation). I suspect that if you have multiple interfaces with the same name UserRepository, all of them would be overridden by the latest declared bean. This is a modified code from GitHub

DemoApplication.java

@SpringBootApplication
//@ImportResource("classpath:beans.xml")
public class DemoApplication {

    public static void main(String[] args) {
        SpringApplication.run(DemoApplication.class, args);
    }
}

beans.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"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
</beans>

mysql.UserRepository.java

@Repository(value = "userRepositoryJpa")
public interface UserRepository extends JpaRepository<User,Long> {
    User findByName(String name);
}

mongodb.UserRepository.java

@Repository(value = "userRepositoryMongo")
public interface UserRepository extends MongoRepository<UserMongo, String> {
    User findByName(String name);
}

EDIT 2

Another option is to write your own implementations for these interfaces and reference them from XML. You need to remove @Repository, @EnableJpaRepositories, @EnableMongoRepositories annotations also. You need to implement all methods by yourself. But you lose the benefits of Spring Data... Look for Custom implementations for Spring Data repositories in docs.

public interface UserRepositoryImpl implements UserRepository {
    public User findByName(String name) {
        // your custom implementation
    }
}

XML Configuration example

<jpa:repositories base-package="com.example.repositories.mysql" repository-impl-postfix="Impl" />  
<mongo:repositories base-package="com.example.repositories.mongodb" repository-impl-postfix="Impl" />  

<beans:bean id="jpaUserRepositoryImpl" class="com.example.repositories.mysql.UserRepositoryImpl">
</beans:bean>

<beans:bean id="mongoUserRepositoryImpl" class="com.example.repositories.mongodb.UserRepositoryImpl">
</beans:bean>
naXa stands with Ukraine
  • 35,493
  • 19
  • 190
  • 259