2

i'm wondering if spring loads an interface when declared as an @autowired attribute of an implementation class without having annotated the interface as a @component .

let me describe my problem a bit more : i have both an interface and its implementation class have the same name but they reside in different packages . i annotated the implementation as @Component("myImplementation") . but i end up having an exception that says :

conflicts with existing, non-compatible bean definition of same name and class

i'm thinking of excluding interfaces from <context:component-scan , what do you think ?

PS : my interface isn't @Component annotated , the application runs just fine on developpement environement , i only get the error after Proguard obfuscation

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
Genjuro
  • 7,405
  • 7
  • 41
  • 61
  • good question, try it by excluding component-scan by implementing e.g., and post result. – Nandkumar Tekale Apr 25 '12 at 15:20
  • you mean excluding the interfaces from the component-scan right ? – Genjuro Apr 25 '12 at 15:23
  • Can you describe your case in more details? Is there anything special in configuration of ``, etc? – axtavt Apr 25 '12 at 15:26
  • i am scanning all the packages , and it happens that my implementation class and it's interface have the same name , but in different packages. (i can't manage the naming part since i'm obfuscating my code) – Genjuro Apr 25 '12 at 15:32
  • @FrozenFlame *i am scanning all the packages* that's usually a sign of bad architecture. if you can't identify one to three root packages that define your application, you have serious problems – Sean Patrick Floyd Apr 25 '12 at 16:02
  • i do have root packages that define my application . like com.company.project.services and com.company.project.services.impl – Genjuro Apr 25 '12 at 16:07
  • @Nandkumar my interfaces are not annotated with Component , so i don't think they get scanned by spring by component-scan , i only get this error after proguard obfuscation which makes the case even more mysterious . – Genjuro Apr 30 '12 at 12:37

3 Answers3

1

Annotating your implementation with @Component and not annotating your interface is usually the right way to set things up. Spring's auto-wiring will look for a managed bean of a matching type, and your implementation will match for a field typed to the interface. If your interface is not annotated with @Component, or any Spring stereotype annotation, it should not be loaded into the context during a component scan. So, you should not have a problem if the interface and implementation have the same class name.

Are you sure you've tried not annotating the interface? Are you sure you don't have some other class somewhere else in your project that also has the same name as the interface and its implementation?

Kirby
  • 15,127
  • 10
  • 89
  • 104
sdouglass
  • 2,350
  • 16
  • 25
  • my interface isn't annotated with Component("myImpl") , only my implementation is . but still , i'm mysteriously told that the name is asigned to both the interface and the implementation . i'm thinking of using a autowired with Qualifier ("myImpl"). – Genjuro Apr 26 '12 at 09:11
  • Could you include the beginning lines of your interface and implementing? Specifically the package statement, the declaration lines, and any annotations on the classes (you can omit imports). Also, have you checked if you have another annotated class somewhere else in your project that has the same name as your implementation? Spring should only care about annotated classes, so if your interface really isn't annotated then it shouldn't be causing a problem. – sdouglass Apr 26 '12 at 15:36
  • the project runs just fine , i only get this error after obfuscation with proguard . now eventhough the implementation are in the same package and have different names , the same probleme occurs . ps : there's no other class using the same component name + my interface is clean from annotations . i wonder if it's something wrong with proguard . the debugging process is such a nightmare :( – Genjuro Apr 26 '12 at 16:18
  • 1
    Have you tried searching for info on how to use Proguard with Spring annotations and autowiring? A little bit of searching makes me think it's going to be nearly impossible to get things to work the way you want because of how Proguard treats annotations. http://stackoverflow.com/questions/8970505/having-difficulties-dealing-proguard-with-spring – sdouglass Apr 26 '12 at 20:15
  • this is realy a dead for me :D i can't seem to find a solution , and i'm not well positionned to influence myBoss on giving up obfuscation. – Genjuro Apr 27 '12 at 10:47
1

Your proguard.conf should be contain:

## ... preserve class annontation (Java EE 1.6 DI)
# Spring3
#-keep @org.springframework.stereotype.Service class *
-keep @org.springframework.stereotype.Controller class *
#-keep @org.springframework.stereotype.Component class *
#-keep @org.springframework.stereotype.Repository class *

proguard forums has more detailed answers.

Arjan Tijms
  • 37,782
  • 12
  • 108
  • 140
UDOLINE
  • 11
  • 1
0

Well I think moving interfaces in different package would work because you would create object reference of interface and respective implementation beans would be auto wired to those object references. But you should follow naming conventions. There would be problem when differentiating interfaces and implementation classes as names are same. Follow standards like

interface SomeInterface {
//....
}

for implementation class of SomeInterface

class SomeInterfaceImpl implements SomeInterface {
// implementation....
}
Nandkumar Tekale
  • 16,024
  • 8
  • 58
  • 85
  • i agree on the naming conventions , but i'm obfuscating my code , so i can end up with interface's name = a and it's implementation = a (in different packages of course) – Genjuro Apr 25 '12 at 15:47