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.