Take the 2-minute tour ×

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 :


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

share|improve this question
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 ? –  FrozenFlame Apr 25 '12 at 15:23
Can you describe your case in more details? Is there anything special in configuration of <context:component-scan>, 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) –  FrozenFlame Apr 25 '12 at 15:32
@FrozenFlame : yes –  Nandkumar Tekale Apr 25 '12 at 15:37
show 3 more comments

3 Answers

Annotating your implementation with @Component and not annotating your interface is usually the right way to set things up. Spring's autowiring 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?

share|improve this answer
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"). –  FrozenFlame Apr 26 '12 at 9: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 :( –  FrozenFlame Apr 26 '12 at 16:18
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. stackoverflow.com/questions/8970505/… –  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. –  FrozenFlame Apr 27 '12 at 10:47
add comment

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.

share|improve this answer
add comment

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....
share|improve this answer
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) –  FrozenFlame Apr 25 '12 at 15:47
add comment

Your Answer


