5

I have a service class which depends on a Repository Bean

@Service
public class SomeService{
   private Repo repoClass;
   @Autowired
   public SomeService(Repo repoClass){
      this.repoClass = repoClass;
   }
   //Methods
}

However I have two kinds of Repo

public class JdbcRepo implements Repo{
}

public class HibernateRepo implements Repo {
}

How do I make two beans of SomeService which one is injected with JdbcRepo and another is injected with HibernateRepo ?

IllSc
  • 1,419
  • 3
  • 17
  • 24

3 Answers3

3

I have a simple solution here, please look into @Primary

I am assuming that you are using the annotation driven approach:

@Primary
@Repository(value = "jdbcRepo")
public class JdbcRepo implements Repo{
}

@Primary indicates that a bean should be given preference when multiple candidates are qualified to autowire a single-valued dependency. If exactly one 'primary' bean exists among the candidates, it will be the autowired value.

@Repository(value = "hibernateRepo")
public class HibernateRepo implements Repo {
}

To Inject the dependency, you can use @Autowired with @Qualifier or only @Resource.

Now to inject JdbcRepo, you can just use @Autowired, because of the @Primary:

@Service
public class SomeService{
   @Autowired
   private Repo repoClass;
}  

To inject HibernateRepo, you have to use the @Qualifier

 @Service
    public class RandomService{
       @Autowired
       @Qualifier("hibernateRepo")
       private Repo repoClass;
    }  

For your concern two beans of SomeService which one is injected with JdbcRepo and another is injected with HibernateRepo, You can follow the same pattern for Service class that is followed for your Repository.

public interface SomeService{
}

@Primary
@Service(value = "jdbcService")
public class  JdbcService extends SomeService{
   @Autowired
   private Repo repo;
}

@Service(value = "hibernateService")
public class  HibernateService extends SomeService{
   @Autowired
   @Qualifier("hibernateRepo")
   private Repo repo;
}

To Inject SomeService with jdbcRepo:

@Autowired
private SomeService someService;

To Inject SomeService with HibernateRepo

@Autowired
@Qualifier("hibernateService")
private SomeService someService;

Please take into these Stackoverflow threads for further reference:

I hope this helps you, feel free to comment!

Community
  • 1
  • 1
SyntaX
  • 2,090
  • 17
  • 30
  • So In the end I have to have two class of `SomeService` ? – IllSc Nov 27 '15 at 07:48
  • @IIISc That is just an example. Important here is the way `Repo` is injected, not the service class. It could be any spring managed bean, I mean wherever you want to inject. Anyways, I have rename the Service class to avoid confusion. Let me know if you still hv doubts. – SyntaX Nov 27 '15 at 09:10
  • My main concern is to have two beans type, but each bean has different bean to be injected – IllSc Nov 27 '15 at 09:33
1

Define two someService bean in xml like this

<bean id="someservice" class="">
     <constructor-arg>
         <value>JdbcRepo</value>
     </constructor-arg>
</bean>

<bean id="someservice2" class="">
     <constructor-arg>
         <value>HibernateRepo</value>
     </constructor-arg>
</bean>
Bilal Shah
  • 1,135
  • 7
  • 17
  • 42
0

If your are using annotation driven,simply use this

@Autowired private JdbcRepo jdbcRepo; 
@Autowired private HibernateRepo hibernateRepo ;