2016-10-11 7 views
1

私は2つのクラスを持っています。 PostおよびComment。投稿@HandleBeforeCreateはうまく動作しますが、コメント@HandleBeforeCreateはありません。なぜだろう?Springデータの残り@HandleBeforeCreateメソッドが呼び出されていません

PostEventHandlerクラス:

@Component 
@RepositoryEventHandler(Post.class) 
public class PostEventHandler { 

    @HandleBeforeCreate 
    public void setPostAuthorname(Post Post) { 
     System.out.println("This method called successfully!"); 
    } 
} 

PostRepositoryインタフェース:

@RepositoryRestResource(collectionResourceRel = "posts", path = "posts") 
public interface PostRepository extends MongoRepository<Post, String> { 

} 

Postクラスなしのカスタムコントローラ/リソースの実装。しかし、私のコメントリポジトリのインターフェースでは、私は、カスタムメソッドを持っており、それは次のようになります。@のよう

@RepositoryRestController 
public class CommentController { 

    @Autowired 
    private AnnounceRepository announceRepository; 

    @Autowired 
    private CommentRepository commentRepository; 

    @RequestMapping(value = "/announces/{announceId}/comments", method = RequestMethod.GET) 
    public ResponseEntity<List<Comment>> getAllComments(@PathVariable("announceId") String announceId) { 
     System.out.println("This method called successfully with a valid PathVariable!); 
     // Custom interface method works fine 
     List<Comment> comments = commentRepository.findAllByAnnounceId(announceId); 
     if (comments != null) { 
      System.out.println("This method called successfully!); 
      return new ResponseEntity<>(comments, HttpStatus.OK); 
     } 
     return new ResponseEntity<>(HttpStatus.NOT_FOUND); 
    } 

    @RequestMapping(value = "/announces/{announceId}/comments", method = RequestMethod.POST) 
    public ResponseEntity<Comment> createComment(@PathVariable("announceId") String announceId, @RequestBody Comment comment) { 
     System.out.println("This method called successfully with a valid PathVariable and Comment object!"); 
     Announce announce = announceRepository.findOne(announceId); 
     if (announce != null) { 
      commentRepository.save(comment); 
      announce.getCommentList().add(comment); 
      announceRepository.save(announce); 
      return new ResponseEntity<>(HttpStatus.CREATED); 
     } 
     return new ResponseEntity<>(HttpStatus.NOT_FOUND); 
    } 
} 

答えて

1

イベントハンドラ(:

@RepositoryRestResource(collectionResourceRel = "comments", path = "comments") 
public interface CommentRepository extends MongoRepository<Comment, String> { 
    // announceId is a field in Comment class. Get method works fine 
    List<Comment> findAllByAnnounceId(String announceId); 
} 

CoomentEventHandlerクラス:

@Component 
@RepositoryEventHandler(Comment.class) 
public class CommentEventHandler { 

    @HandleBeforeCreate 
    public void setCommentAuthorUsername(Comment comment) { 
     System.out.println("This method never gets invoked!"); 
    } 
} 

カスタムCommentController実装HandleBeforeCreate注釈付きメソッド)は、公開されたSpringデータRESTエンドポイントでHTTPリクエストを起動するときにのみ呼び出されます。これが、POSTリクエストで/ postsパスがsetPostAuthornameメソッドをトリガーする理由です。 CommentControllerの場合、カスタムリクエストマッピングメソッドが公開されています。この方法で直接リポジトリを呼び出すと、イベントハンドラがトリガされることはありません。このアプローチを使用する唯一の方法は、CommentControllerにEvent Handler Beanを注入し、対応するメソッドを呼び出して、リポジトリ・メソッドの呼び出し、またはユーザー・インタフェースから直接呼び出すことです。 UIからのcreateCommentメソッドのロジック。公開/コメントAPIパスを介してPOSTリクエストを行う。

よろしくお願いいたします。

関連する問題