jeudi 13 avril 2017

Decorating Spring Data Repository

GIVEN:

interface SpringRepository extends CrudRepository, CustomRepository {
    void deleteAllByKey(String key);
    // <...>
}

Lots of classes using this repo:

@Autowired
public MyClass(SpringRepository springRepository) {
    this.springRepository = springRepository;
}

GOAL:

There's a need to decorate SpringRepository to add new behavior to its methods without changing other code:

@Override
void deleteAllByKey(String key) {
    // new behavior here
}

TRIED (unsuccessfully):

@Component
@Primary
public abstract class SpringRepositoryDecorator implements SpringRepository {
    private final SpringRepository springRepository;

    @Autowired
    public SpringRepositoryDecorator(SpringRepository springRepository) {
        this.springRepository = springRepository;
    }

    @Override
    void deleteAllByKey(String key) {
    // new behavior here
    }
}

I've just started learning Spring and i really have a mess in my head. Any help appretiated.

Aucun commentaire:

Enregistrer un commentaire