0

I have a question regarding how to implement Marker interface through spring. I need to point my marker interface to its implemented classes. Itself marker interface dont have any of function declaration.

interface A { 

    /* No Method declaration */ 

}

@Component("c")
Class B implement A {

     void function1(){ .. }
     void function2(){ .. }

}

@Component("c")
Class C implement A {

    void function3(){ .. }
    void function4(){ .. }
}

Now Some where in my business Logic, i would like to use @Autowire though my interface to point any of one implementation.

@Autowired
@Qualifier("b")
A aB;

@Autowired
@Qualifier("c")
A aC;

It dont work. Will you please help to implement in correct way…! I was expecting thorugh reflection it should give me the list of method available in the implemented classes but it dont.

Added More Details

The only thing i would like to do is, I would like to reply IResponse to my business methods, instead of different tyoe. Its ok for me if i would have to @Authowired direct to implementation like

@Autowired B aB;

but i thought if there is some way my IDE and Spring do some logic and when i @Autowired my interface towards implementation then it should be able to pick my Implementation class and should show me the business methods. Its not magic, when i am using qualified. I just want from spring to show me business methods through reflection.

Ji_Ji
  • 1
  • 1
  • What are you planning to do with aC and aB? What you describing in code works fine in Spring, but you will not be able invoke any methods on those objects – geoand Mar 26 '14 at 21:38
  • Yup, that should would fine. Let's see your configuration and the rest of the beans involved. – Sotirios Delimanolis Mar 26 '14 at 23:35
  • Thanks for the reply, i added more comments what i am trying to do. – Ji_Ji Mar 27 '14 at 13:13
  • Thanks guys , I was actually trying to do some nonsense magic...! – Ji_Ji Mar 27 '14 at 13:42
  • Thanks for contributing an answer to Stack Overflow! Infect, i was just trying to having different implementation classes and want to access those method list by implementing one marker interface. I just trying to explore if spring + IDE can available for me if i point out my interface towards my implementation class. – Ji_Ji Mar 27 '14 at 13:46

1 Answers1

0

What is the point of injecting A exactly? You probably expect Spring to do some kind of "magic" when it injects the dependency. It does not. I will simply inject the bean that you have required if it can inject it. In your case, the Qualifer value gives a hint as to which bean instance spring injects.

Except the typo (it's @Autowired, not @Autowire), this code should indeed inject B and C. But as you declared them as being A in your code you'll get a simple java.lang.Object basically. That's just a fundamental principle of Java that is strongly typed language.

Stephane Nicoll
  • 31,977
  • 9
  • 97
  • 89
  • I added more comments, – Ji_Ji Mar 27 '14 at 13:13
  • 1
    I am sorry, I really don't understand what you want to do. What's the point of injecting A and then get the methods by reflection? Reflection is based on types, not on instance so if you ask the method of `A.class`, you'll get what `java.lang.Object` provides. You could do something like `aB.getClass()` and discover the methods available there through reflection. I still don't see why you want to do that. – Stephane Nicoll Mar 27 '14 at 13:25
  • You are right. I was actually trying to do some nonsense magic...! – Ji_Ji Mar 27 '14 at 13:41