Spring Seam : ManagedPersistentContext and Injection (Spring -> CDI)
gonzalad Jul 21, 2011 5:14 AMHello,
I create a sample app with spring some time ago.
I've managed pretty easily to :
- inject spring bean into CDI
- configure Seam 3 ManagedPersistentContext from Spring (à la Seam 2)
Here's what I did (perhaps it can help someone) :
I - Injecting Spring beans into CDI components
2 choices for now :
use the Spring CDI bridge (Rick Hightower)
create a Producer yourself
Approach 1 : Using Spring / CDI Bridge
See http://rick-hightower.blogspot.com/2011/04/cdi-and-spring-living-in-harmony.html
With this approach you just need to use :
@Inject @Spring(name="fooBar") FooSpringBean springBean;
Personnaly, I don't like having to use @Spring special annotation.
Would have prefered to just @Inject and if bean is a Spring bean, then Spring bean is injected, otherwhise
just classic CDI bean.
Approach 2 - Create Producer yourself
@ApplicationScoped
public class SpringServices {
@Produces
@RequestScoped
public FooSpringBean getFooSpringBean() {
return (FooSpringBean) ContextLoader.getCurrentWebApplicationContext().getBean(FooSpringBean.class);
}
}
And then you just need to use :
@Inject FooSpringBean springBean;
II - Seam-managed persistence context Configured in Spring
<bean id="entityManagerFactory" class="tmp.pag.proto.seam3.ioc.spring.SeamManagedEntityManagerFactoryBean"> <property name="persistenceContextName" value="entityManager" /> </bean> <bean id="springEntityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean"> <property name="jpaVendorAdapter"> <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter"/> </property> <property name="jpaPropertyMap"> <map> <entry key="hibernate.cache.use_second_level_cache" value="true"/> <entry key="hibernate.cache.use_query_cache" value="true"/> <entry key="hibernate.cache.provider_class" value="net.sf.ehcache.hibernate.SingletonEhCacheProvider"/> <entry key="hibernate.show_sql" value="false" /> <entry key="hibernate.use_sql_comments" value="false" /> <entry key="hibernate.format_sql" value="false" /> <entry key="hibernate.dialect" value="org.hibernate.dialect.Oracle10gDialect" /> <entry key="hibernate.temp.use_jdbc_metadata_defaults" value="false"/> </map> </property> <property name="dataSource" ref="dataSource"/> </bean>
--------------------------------------------------------
package tmp.pag.proto.seam3.jpa;
import javax.enterprise.context.ConversationScoped;
import javax.enterprise.inject.Default;
import javax.enterprise.inject.Produces;
import javax.persistence.EntityManagerFactory;
import org.jboss.seam.solder.core.ExtensionManaged;
import org.springframework.web.context.ContextLoader;
public class ManagedPersistenceContext {
/**
* Inspired from http://docs.jboss.org/seam/3/persistence/latest/reference/en-US/html/persistence.html#d0e211
* But uses Spring bean named 'springEntityManagerFactory'
*/
@ExtensionManaged
@Produces
@ConversationScoped
@Default
public EntityManagerFactory getEntityManagerFactory() {
return ContextLoader.getCurrentWebApplicationContext().getBean("springEntityManagerFactory", EntityManagerFactory.class);
}
}
--------------------------------------------------------
package tmp.pag.proto.seam3.ioc.spring;
import javax.enterprise.inject.spi.BeanManager;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.persistence.EntityManagerFactory;
import javax.persistence.PersistenceException;
import org.jboss.seam.faces.util.BeanManagerUtils;
import org.jboss.seam.solder.core.Veto;
import org.springframework.orm.jpa.AbstractEntityManagerFactoryBean;
@Veto
public class SeamManagedEntityManagerFactoryBean extends
AbstractEntityManagerFactoryBean {
private EntityManagerFactory entityManagerFactory;
private String persistenceContextName;
@Override
protected EntityManagerFactory createNativeEntityManagerFactory()
throws PersistenceException {
return getDelegate();
}
@Override
public String getPersistenceUnitName() {
String persistenceUnitName = super.getPersistenceUnitName();
if (persistenceUnitName == null || "".equals(persistenceUnitName)) {
return persistenceContextName;
}
return persistenceUnitName;
}
public void setPersistenceContextName(String persistenceContextName) {
this.persistenceContextName = persistenceContextName;
}
private EntityManagerFactory getDelegate() {
if (entityManagerFactory == null) {
initEntityManagerFactory();
}
return entityManagerFactory;
}
private void initEntityManagerFactory() {
InitialContext lContext;
try {
lContext = new InitialContext();
BeanManager lBeanManager = (BeanManager) lContext
.lookup("java:comp/env/BeanManager");
entityManagerFactory = BeanManagerUtils.getContextualInstance(
lBeanManager, EntityManagerFactory.class);
} catch (NamingException e) {
throw new RuntimeException(e);
}
}
}