Hibernate entity autowire

后端 未结 3 2022
迷失自我
迷失自我 2021-01-05 04:53

Can you please advice me, how can I nicely enable Spring autowiring for Hibernate entities?

Let\'s say I have an entity and would like to have mail-sender there:

相关标签:
3条回答
  • 2021-01-05 05:19

    It is possible! (And it is the default style in Spring Roo!)

    All what you need is to add the @Configurable annotation to your Entity. Activate the annotation in the configuration <context:spring-configured/> and using AspectJ weaving.

    There is a Chapter in the Spring Reference: 7.8.1 Using AspectJ to dependency inject domain objects with Spring

    See also:

    • http://www.chrissearle.org/node/285
    • http://www.olivergierke.de/wordpress/2009/05/using-springs-configurable-in-three-easy-steps/

    BTW I strongly recommend to use AspectJ compile time weaving, when possible.

    0 讨论(0)
  • 2021-01-05 05:32

    There seems to be a better way than using aspectj-weaving, namely using a hibernate LoadEventListener, explained in this post.

    0 讨论(0)
  • 2021-01-05 05:38

    The way I do it is to use AutowiredAnnotationBeanPostProcessor.

    In your entity's constructor you ask the AutowiredAnnotationBeanPostProcessor to inject "this".

    My comments at the end of this article details the technique. The article talks about a similar method of autowiring Hibernate entities.

    0 讨论(0)
提交回复
热议问题