| public class Foo { @TransactionAttribute(TransactionAttributeType.REQUIRED) public bar () { // 执行某些操作 ... } } |
| @SecurityDomain("other") public class Foo { @RolesAllowed({"managers"}) @TransactionAttribute(TransactionAttributeType.REQUIRED) public bar () { // 执行某些操作 ... } } |
| <!-- Setup the transaction interceptor --> <bean id="foo" class="org.springframework.transaction .interceptor.TransactionProxyFactoryBean"> <property name="target"> <bean class="Foo"/> </property> <property name="transactionManager"> <ref bean="transactionManager"/> </property> <property name="transactionAttributeSource"> <ref bean="attributeSource"/> </property> </bean> <!-- Setup the transaction manager for Hibernate --> <bean id="transactionManager" class="org.springframework.orm .hibernate.HibernateTransactionManager"> <property name="sessionFactory"> <!-- you need to setup the sessionFactory bean in yet another XML element -- omitted here --> <ref bean="sessionFactory"/> </property> </bean> <!-- Specify which methods to apply transaction --> <bean id="transactionAttributeSource" class="org.springframework.transaction .interceptor.NameMatchTransactionAttributeSource"> <property name="properties"> <props> <prop key="bar"> </props> </property> </bean> |
| <bean id="autoproxy" class="org.springframework.aop.framework.autoproxy .DefaultAdvisorAutoProxyCreator"/> <bean id="transactionAttributeSource" class="org.springframework.transaction.interceptor .AttributesTransactionAttributeSource" autowire="constructor"/> <bean id="transactionInterceptor" class="org.springframework.transaction.interceptor .TransactionInterceptor" autowire="byType"/> <bean id="transactionAdvisor" class="org.springframework.transaction.interceptor .TransactionAttributeSourceAdvisor" autowire="constructor"/> <bean id="attributes" class="org.springframework.metadata.commons .CommonsAttributes"/> |
| public class FooDao { @Resource (name="DefaultDS") DataSource myDb; // 使用 myDb 获取数据库的JDBC连接 } |
| @Resource public void setSessionContext (SessionContext ctx) { sessionCtx = ctx; } |
| @Stateful public class FooBean implements Foo, Serializable { @PersistenceContext( type=PersistenceContextType.EXTENDED ) protected EntityManager em; public Foo getFoo (Integer id) { return (Foo) em.find(Foo.class, id); } } |
| public class FooDao { HibernateTemplate hibernateTemplate; public void setHibernateTemplate (HibernateTemplate ht) { hibernateTemplate = ht; } // 使用 Hibernate 模板访问数据 public Foo getFoo (Integer id) { return (Foo) hibernateTemplate.load (Foo.class, id); } } |
| <bean id="dataSource" class="org.springframework .jndi.JndiObjectFactoryBean"> <property name="jndiname"> <value>java:comp/env/jdbc/MyDataSource</value> </property> </bean> <bean id="sessionFactory" class="org.springframework.orm .hibernate.LocalSessionFactoryBean"> <property name="dataSource"> <ref bean="dataSource"/> </property> </bean> <bean id="hibernateTemplate" class="org.springframework.orm .hibernate.HibernateTemplate"> <property name="sessionFactory"> <ref bean="sessionFactory"/> </property> </bean> <bean id="fooDao" class="FooDao"> <property name="hibernateTemplate"> <ref bean="hibernateTemplate"/> </property> </bean> <!-- The hibernateTemplate can be injected into more DAO objects --> |
关注此文的读者还看过: