Hibernate 实体bean的Guice依赖注入?
对于丰富的域驱动设计,我想在JPA/Hibernate实体bean上使用Guice依赖注入。我正在寻找一个类似于Spring@configurable注释的解决方案,用于非SpringBeanHibernate 实体bean的Guice依赖注入?,hibernate,jpa,domain-driven-design,aop,guice,Hibernate,Jpa,Domain Driven Design,Aop,Guice,对于丰富的域驱动设计,我想在JPA/Hibernate实体bean上使用Guice依赖注入。我正在寻找一个类似于Spring@configurable注释的解决方案,用于非SpringBean 有人知道图书馆吗?有代码示例吗?因为实体是由JPA提供商创建的,所以我看不到Guice何时会起作用。不过,也许可以看看项目的方法。您可以使用AspectJ来实现这一点 创建@Configurable注释: @Retention(RetentionPolicy.RUNTIME) @Target({Eleme
有人知道图书馆吗?有代码示例吗?因为实体是由JPA提供商创建的,所以我看不到Guice何时会起作用。不过,也许可以看看项目的方法。您可以使用AspectJ来实现这一点 创建@Configurable注释:
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.TYPE})
public @interface Configurable {
}
创建类似以下内容的AspectJ@Aspect:
@Aspect
public class ConfigurableInjectionAspect {
private Logger log = Logger.getLogger(getClass().getName());
@Pointcut("@within(Configurable) && execution(*.new(..)) && target(instantiated)")
public void classToBeInjectedOnInstantiation(Object instantiated) {}
@After(value = "classToBeInjectedOnInstantiation(instantiated)",
argNames = "instantiated")
public void onInstantiation(Object instantiated) {
Injector injector = InjectorHolder.getInjector();
if (injector == null) {
log.log(Level.WARNING, "Injector not available at this time");
} else {
injector.injectMembers(instantiated);
}
}
}
为喷油器创建(并使用)保持类:
public final class InjectorHolder {
private static Injector injector;
static void setInjector(Injector injector) {
InjectorHolder.injector = injector;
}
public static Injector getInjector() {
return injector;
}
}
配置META-INF/aop.xml:
<aspectj>
<weaver options="-verbose">
<include within="baz.domain..*"/>
<include within="foo.bar.*"/>
</weaver>
<aspects>
<aspect name="foo.bar.ConfigurableInjectionAspect"/>
</aspects>
</aspectj>
注释您的域类:
@Entity
@Table(name = "Users")
@Configurable
public class User {
private String username;
private String nickname;
private String emailAddress;
@Inject
private transient UserRepository userRepository
public User() {}
}
我发现这个问题有点棘手 假设只有两种方法可以创建类型为
T
的实体对象:
- 从
javax.inject.Provider获取一个
- 从实体管理器查询它(它将调用
带注释的方法)@PostLoad
@MappedSuperclass
public abstract class PersistentDomainObject<K extends Serializable & Comparable<K>>
implements Comparable<PersistentDomainObject<K>>, Serializable {
private static transient Injector injector;
@PostLoad
private final void onAfterLoaded() {
injector.injectMembers(this);
}
@EmbeddedId
private K id;
public K getId() { return id; }
// ... compareTo(), equals(), hashCode(), maybe a @Version member ...
}
糟糕的是,你必须相信没有人会自己创建一个MyDomainEntity
,但会向提供者索要它。这可以通过隐藏构造函数来实现
亲切问候,
avi谢谢,像Salve这样的代码编织解决方案真的可以做到这一点。我尝试了Salve,但它的文档有限,我无法让它做任何事情(甚至没有错误消息)。只希望得到一些简单的示例代码,例如AspectJ或更好的AOP。@Kdeveloper:我没有任何Salve方面的经验,因此我不能推荐它,但它可能会给您一些实现类似功能的想法,这就是为什么我提到了ITE,尽管其中的静态注入在某种程度上不受欢迎,目前,添加AspectJ这样的依赖项对于我的项目来说是负担不起的。此外,该解决方案适合我的问题,并且非常干净。
@MappedSuperclass
public abstract class PersistentDomainObject<K extends Serializable & Comparable<K>>
implements Comparable<PersistentDomainObject<K>>, Serializable {
private static transient Injector injector;
@PostLoad
private final void onAfterLoaded() {
injector.injectMembers(this);
}
@EmbeddedId
private K id;
public K getId() { return id; }
// ... compareTo(), equals(), hashCode(), maybe a @Version member ...
}
@Entity
public class MyDomainEntity extends PersistentDomainObject<SomeEmbeddableIdType>
implements HatLegacyId {
@Inject
private transient MyDomainService myDomainService;
private String name;
// ... common stuff
}