Tao Chen
Tao Chen

Reputation: 285

How to let spring auto implement abstract methods

As we know, Spring data can implement the user-defined interface automatcially. How to create the library with the same ablitity? For example:

interface Service {

    @Println("The metod 'a' is invoked)
    void a(); //Hope spring implement it automatically

    @Println("The metod 'b' is invoked)
    void b(); //Hope spring implement it automatically

    void c();
}

This interface has three methods, a, b, and c. the annotation @Println has been declared on a and b, that means they should be implemented automatically. Method c has no annotations, that means it should be implenented by the developer, the developer can define an abstract class that only override methods c, like this

@Compomenet
public abstract class ServiceImpl extends Service {
    @Override
    public void c() {
        System.out.println("User invokes method 'c'");
    }
}

I know how to use JDK proxy/cglib/ASM to generate byte code at runtime, but I don't know how to change the bean registrition behavior of spring framework. I want create a library that can let spring auto implement abstrat methods by my bytecode generator.

Upvotes: 1

Views: 293

Answers (1)

This answer should address your question: Component Scan for custom annotation on Interface

Basically, you extend one class (ClassPathScanningCandidateComponentProvider) to make sure you component scan picks up abstract classes and interfaces; you then provide a ImportBeanDefinitionRegistrar implementation to register the beans.

Upvotes: 1

Related Questions