问题描述
Spring 3.0.2,
Hibernate 3.5.0,
Hibernate-Validator 4.0.2.GA
我正在尝试注入Spring依赖于ConstraintValidator使用:
@PersistenceContext
private EntityManager entityManager;
我已经配置了应用程序上下文:
< bean id =validatorclass =org.springframework.validation.beanvalidation.LocalValidatorFactoryBean/>
根据Spring文档,应该允许自定义ConstraintValidators从任何其他方式受益于依赖注入Spring bean
在调试器中,我可以看到Spring调用getBean来创建ConstraintValidator。之后当flush触发preInsert时,会创建并调用一个不同的ConstraintValidator。问题是在这个新的ConstraintValidator中EntityManager为null。我已经尝试在ConstraintValidator中注入其他依赖项,并且这些依赖项始终为空。有没有人知道是否可以将约束注入ConstraintValidator?
在EntityManager中注入一个Spring上下文感知ValidatorFactory的最好方法是使用javax.persistence.validation.factory属性。配置如下:
< bean id =entityManagerFactoryclass =org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean >
< property name =dataSourceref =dataSource/>
< property name =jpaVendorAdapter>
< bean class =org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter>
< property name =databasePlatformvalue =org.hibernate.dialect.HSQLDialect/>
< / bean>
< / property>
< property name =jpaPropertyMap>
< map>
< entry key =javax.persistence.validation.factoryvalue-ref =validator/>
< / map>
< / property>
< / bean>
< bean id =validatorclass =org.springframework.validation.beanvalidation.LocalValidatorFactoryBean/>
享受!
Spring 3.0.2,Hibernate 3.5.0,Hibernate-Validator 4.0.2.GA
I am trying to inject Spring dependencies into a ConstraintValidator using:
@PersistenceContext
private EntityManager entityManager;
I have configured the application context with:
<bean id="validator" class="org.springframework.validation.beanvalidation.LocalValidatorFactoryBean"/>
Which, according to the Spring documentation, should allow "custom ConstraintValidators to benefit from dependency injection like any other Spring bean"
Within the debugger I can see Spring calling getBean to create the ConstraintValidator. Later when flush triggers the preInsert, a different ConstraintValidator is created and called. The problem is the EntityManager is null within this new ConstraintValidator. I’ve tried injecting other dependencies within the ConstraintValidator and these are always null.
Does anyone know if it is possible to inject dependencies into a ConstraintValidator?
The best way to inject a Spring context aware ValidatorFactory inside your EntityManager is by using the javax.persistence.validation.factory property. Configuration goes as follows:
<bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="jpaVendorAdapter">
<bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
<property name="databasePlatform" value="org.hibernate.dialect.HSQLDialect" />
</bean>
</property>
<property name="jpaPropertyMap">
<map>
<entry key="javax.persistence.validation.factory" value-ref="validator" />
</map>
</property>
</bean>
<bean id="validator" class="org.springframework.validation.beanvalidation.LocalValidatorFactoryBean"/>
Enjoy!
这篇关于JSR-303依赖注入和Hibernate的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!