-1

I have two packages and beans as below in spring

com.myapp.test1

  • myService

com.myapp.test2

  • myService

part 1:The solution for this situation is giving name for beans like this:

@Service(name="myService1")
myService

and

 @Service(name="myService2")
 myService

and Inject like

@Autowire
@Qualifier("myService1")

Is there any solution without part1 approach, I required define two bean with same name in different namespace without name & @Qualifier

for theme (I am using spring).

Ali Akbarpour
  • 958
  • 2
  • 18
  • 35
  • So your beans are two instances of the same class? Or different classes being in 2 separate classes? – Matt Oct 09 '17 at 12:49
  • @Matt there are two different bean in different namespace with same name – Ali Akbarpour Oct 09 '17 at 12:52
  • By namespace, you mean package right? And a bean is not in a package it is an instance (or several if not singleton) of a class. – Matt Oct 09 '17 at 12:56

1 Answers1

0

By default, Spring will give a name to the bean based on AnnotationBeanNameGenerator. So for same class name (even though on different packages), they will be givem the same name and fail.

As you tried the simplest solution is to give a name to one (or both) of the beans in your @Service annotation.

But then, you won't have to specify this name when injecting it as injection is looking for class instance, and in this case both classes are different.

com.myapp.test1.MyService is a different class than com.myapp.test2.MyService

And if both are annotated with @Service and managed by Spring, you can inject them as follow :

MyService 1 :

package com.myapp.test1;

@Service
public class MyService {

}

MyService 2 :

package com.myapp.test2;

@Service("myService2")
public class MyService {

}

Injecting them :

@Controller
public class MyController {
    @Autowired
    //no need for qualifier here
    private com.myapp.test1.MyService myService1;
    @Autowired
    //no need for qualifier here
    private com.myapp.test2.MyService myService2;
    ...
}

That said, it would be easier to give different names to both classes.

Matt
  • 3,422
  • 1
  • 23
  • 28
  • it has run time error Caused by: org.springframework.context.annotation.ConflictingBeanDefinitionException: Annotation-specified bean name 'MyService ' for bean class [a2.asena.service.test.MyService ] conflicts with existing, non-compatible bean definition of same name and class – Ali Akbarpour Oct 09 '17 at 13:12
  • My bad, I didn't realize this didn't work. I guess by default Spring gives a name based on the class name only and will fail if both class names are the same. You just need to define a different name for your @ Service annotation though and not necessaraly in your @ Autowired – Matt Oct 09 '17 at 13:31
  • thanks for your solution. but I have mentioned it is not what I am looking for. – Ali Akbarpour Oct 09 '17 at 13:39
  • Ok, I updated the answer. Another solution would be not to use Spring's ComponentScan and declare your bean in your config with `@Bean`. This will result with bean being named as the method in the config. – Matt Oct 09 '17 at 13:46