Spring
Entête de fichier
Avec Spring 2, l'entête de fichier a été modifiée pour utiliser les schémas.
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:jee="http://www.springframework.org/schema/jee"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
http://www.springframework.org/schema/jee
http://www.springframework.org/schema/jee/spring-jee-2.0.xsd">
Considérations générales
L'attribut ref d'un bean fait référence à un autre bean dont l'attribut id est égal.
Session Factory Hibernate
<bean id="sessionFactoryHib"
class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="mappingResources">
<list>
<value>mon/package/xyz.hbm.xml</value>
</list>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.HSQLDialect</prop>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.jdbc.batch_size">2</prop>
<prop key="hibernate.default_schema">MAIN</prop>
<prop key="hibernate.transaction.flush_before_completion">true</prop>
<!-- Auto close pas utile pour JUnit mais à ne pas oublier pour App Server -->
<prop key="hibernate.transaction.auto_close_session">false</prop>
<prop key="hibernate.hbm2ddl.auto">create</prop>
</props>
</property>
</bean>
Datasource
Sans serveur d'application<bean id="dataSource"
class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName">
<value>org.hsqldb.jdbcDriver</value>
</property>
<property name="url">
<value>jdbc:hsqldb:data/test</value>
</property>
<property name="username"><value>sa</value></property>
<property name="password"><value></value></property>
</bean>
Avec serveur d'application
<jee:jndi-lookup id="datasource" jndi-name="java:comp/env/jdbc/ds" />
Gestion des transactions
For distributed transactions across multiple Hibernate session factories, simply combine JtaTransactionManager as a transaction strategy with multiple LocalSessionFactoryBean definitions. Each of your DAOs then gets one specific SessionFactory referenc passed into it's respective bean property.
<bean id="serviceBean" class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean"> <property name="transactionManager" ref="myTransactionManager"/> <property name="target" ref="serviceBeanTarget"/> <property name="transactionAttributes"> <props> <prop key="*">PROPAGATION_REQUIRED </props> </property> </bean> ... <bean id="beanDao" class="x.y.dao.impl.MonDao"> <property name="sessionFactory" ref="sessionFactoryHibXYZ"/> </bean>Pour une seule Session Factory Hibernate
<bean id="myTransactionManager"
class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactoryHib"/>
</bean>
Pour une transaction multiple
<bean id="myTransactionManager"
class="org.springframework.transaction.jta.JtaTransactionManager" />
Différents transaction manager
- Bitronix Transaction Manager
- JOTM - Java Open Transaction Manager Dernière version 2005, pas mal de comments négatifs.
- Atomikos
Accéder au fichier de configuration
L'accès au fichier de configuration provoque l'initialisation des singletons.
Application
ClassPathXmlApplicationContext factory ;
factory = new ClassPathXmlApplicationContext("fichierConfigSpring.xml");
dao = (IYbFactureDao) factory.getBean("facDaoProxy");
ou
ClassPathResource res = new ClassPathResource("fichierConfigSpring.xml") ;
XmlBeanFactory factory = new XmlBeanFactory(res) ;
dao = (IYbFactureDao) factory.getBean("facDaoProxy");
Accéder à un EJB
<bean id="ejbProxy" class="org.springframework.ejb.access.SimpleRemoteStatelessSessionProxyFactoryBean"> <property name="jndiName" value="ejb/jndiName" /> <property name="lookupHomeOnStartup" value="false"/> <property name="cacheHome" value="false"/> <property name="businessInterface" value="pck.IMonItf" /> <property name="refreshHomeOnConnectFailure" value="true" /> <property name="jndiEnvironment"> <props> <prop key="java.naming.factory.initial">com.ibm.websphere.naming.WsnInitialContextFactory <prop key="java.naming.provider.url">iiop://localhost:2809/ <prop key="com.ibm.websphere.naming.jndicache.cacheobject">none </props> </property> </bean>
Transactions EJB - CMT
Pas besoin de déclarer un TransactionProxyFactoryBean car la transaction est gérée par le conteneur!
Liens
- Site officiel
- Introduction à Spring 2.0
- Implementing Transaction Suspension in Spring
- Using the WebSphere transaction manager with Spring and Hibernate
- Using Spring and Hibernate with WebSphere Application Server
- JTA/XA transactions without the J2EE container using Spring
- JOTM Transactions In Spring And Hibernate
- www.springbyexample.org
- Creating prototype Spring beans on demand using lookup-method