1

Here is my interface:

public interface Shape{
    public int size();
}

And the two classes implementing it:

@Repository("Circle")
public class Circle implements Shape
{
       public int size()
       {
             //code
             return size;
       }
}

And

@Repository("Square")
public class Square implements Shape
{
       public int size()
       {
             //code
             return size;
       }
}

Now, can I use the following code?

public class MyTest
{
       @Resource(name="Circle")
       Shape circle;
       @Resource(name="Square")
       Shape square;
       public int size()
       {
             int size = circle.size() + square.size();//Is this correct?
             return size;
       }
}

For some reason my code is giving me null pointer exception, perhaps is there another way to tell the resource annotation which sub-class I wanted to use? (Should I be using other annotation instead??)

Ryan Fung
  • 2,069
  • 9
  • 38
  • 60

0 Answers0