Spring automatic extension of multiple services

I have one base interface and two implementations

public interface AnimalService
{
    public void eat();
}

@Service("animalService")
@Transactional
public class AnimalServiceImpl implements AnimalService
{
    @Override
    public void eat()
    {
        System.out.println("i'm eating");
    }
}

@Service("birdService")
@Transactional
public class BirdServiceImpl extends AnimalServiceImpl
{
    public void fly()
    {
        System.out.println("i'm flying");
    }
}

In my main method, try calling this two utility implementations as follows:

public class test
{
    @Autowired
    private AnimalService animalService;
    @Autowired
    @Qualifier("birdService")
    private AnimalService birdService;

    public static void main(String[] args)
    {
        animalService.eat();
        birdService.eat();
        birdService.fly();
    }
}

This will give a compilation error since birdService cannot find the fly () method. Then I thought, maybe the reason is that I autowire AnimalService instead of BirdServiceImpl, so I change the autowire code as follows:

   @Autowired
   @Qualifier("birdService")
   private AnimalService birdService;

change to:

   @Autowired
   private BirdServiceImpl birdService;

But this will give me a runtime error that "cannot find bean BirdServiceImpl". I have a lot of documents, some say use @Resource. But this does not work for me. Some say registering a bean in a Spring Context, and all my bean registration is done by annotation. I do not want to touch the Spring Context.

-

public interface BirdService extends AnimalService
{
    public void fly();
}

BirdServiceImpl

    public class BirdServiceImpl extends AnimalServiceImpl extends BirdService
    {
        public void fly()
        {
            System.out.println("i'm flying");
        }
    }

:

public class test
{
    @Autowired
    private AnimalService animalService;
    @Autowired
    private BirdService birdService;

    public static void main(String[] args)
    {
        animalService.eat();
        birdService.eat();
        birdService.fly();
    }
}

. . java, . , . spring , .

, ?

+4
2

:

1.

Spring Framework, . AnimalService, , , AnimalService, . , .

2. "Autowiring a class"

Spring, , , . , AOP , , -. .

: Spring Autowiring vs. interface?. , :

autoproxies ,

3.

() / Java?

+2

, , Interface BirdService. , ...

, : ? , CGLIB ( Maven POM Gradle).

Spring AOP , :

, ( ) , - CGLIB.. , - JDK , , JDK .

+1

All Articles