简体   繁体   中英

Spring: Override save method in CrudRepository

I would like to add some code in save method in CrudRepository but keep orginal functionality. After that save method should do what before, save to repository but additionally execute extra action. Is it possible? If so, how could I do it properly?

Thank you in advance for any tips

Edit

So, I would like to create method like this:

@Override
public default <S extends MyClass> S save(S myClassItem) {
    //here my functionality 
    super.save(myClassItem); //from CrudRepository
    return myClassItem;
}

I don't think it's possible to user super in such a method (default and overridden). So you must change the name of the method - which might not be exactly what you are looking for :

public interface TestRepository extends CrudRepository<MyClass, String> {

    default MyClass mySave(MyClass myClassItem) {
        save(myClassItem);
        return myClassItem;
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM