Spring Boot @RepositoryEventHandler not invoked

雨燕双飞 提交于 2019-11-30 04:20:02

问题


I've got a @RepositoryEventHandler set up and it is not being invoked for some unknown reason.

@Component
@RepositoryEventHandler(User.class)
public class UserEventHandler {

    @Autowired
    private PasswordCrypto passwordCrypto;

    @HandleBeforeSave
    public void handleUserSave(User user) {
        if (user.getPassword() != null && !"".equals(user.getPassword())) {
            user.setPassword(passwordCrypto.encrypt(user.getPassword()));
        }
    }

    @HandleBeforeCreate
    public void handleUserCreate(User user) {
        user.setPassword(passwordCrypto.encrypt(user.getPassword()));
    }
}

The Repository:

public interface UserRepository extends CrudRepository<User, Long> {
    Optional<User> findOneByUsername(String username);
}

And my main class:

@SpringBootApplication
@EntityScan("de.ihrig.feuerwehr.hydranet.model")
@EnableJpaRepositories
@ComponentScan({
    "somepath",
    "somepath including the UserEventHandler"
})
public class ServerApplication {

    public static void main(String[] args) {
        SpringApplication.run(ServerApplication.class, args);
    }
}

Thanks for your help in advance, I just cannot find the error.


回答1:


There could be a couple of things going on.

You're repo isn't annotated w/ @RestResource, but I'm guess that was just an oversight.

I'm guessing the comments are correct and you're testing this through a unit test with an entry point that is not going through the REST API. The handlers only capture Repo events that are coming through that vector.

If you want to capture any repo event, regardless of entry point, then you want to use a AbstractRepositoryEventListener

The Spring Data Rest Events documentation covers this. Also see listening for repository events in spring boot.



来源:https://stackoverflow.com/questions/32796049/spring-boot-repositoryeventhandler-not-invoked

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!