论坛首页 Java企业应用论坛

使用Spring的情况下如何将Dao注入DomainObject?

浏览 5109 次
精华帖 (0) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (0)
作者 正文
   发表时间:2007-04-05  
为了获得完整的RDO,将Dao注入查了一些资料,大概有三种办法:
1、手工设置,对生成的每个对象调用SetDao()。
2、使用AspectJ的AOP在编译时完成对new()的增强。
3、使用Spring的build-time weaving(同2) or load-time weaving。

第一种方法除了比较繁琐,而且会对Spring Context产生依赖。
第二种要求使用AspectJ,对于大项目使用这个东西编译比较痛苦。
第三种不知道有人用过没有?
是否还有其它方法?谢谢。
   发表时间:2007-04-06  
可以考虑static dao,一次性注入
0 请登录后投票
   发表时间:2007-04-09  
准备采用如下方式:
1、将DO放入Spring容器,利用Spring Ioc将DAO注入。
2、建立工厂接口,实现一个依赖Spring的工厂类。
3、所有创建DO的地方使用该工厂。
4、放弃ORM,适用Spring Jdbc。
0 请登录后投票
   发表时间:2007-05-14  
最近也在看这个问题,spring确实 和 DomainObject的设计有些矛盾,好像是Spring的软肋,其实跟他的实现有关系(基于反射,而不是AspectJ),因此不能对new的对象进行干预。

前些天花了点时间,考律用AspectJ来搞定。
public aspect DomainSupportAspect issingleton() {

	private interface HasEntityManager {

		public EntityManager getEntityManager();

	}

	public EntityManager HasEntityManager.getEntityManager() {
		return EntityManagerFactoryUtils.getTransactionalEntityManager(emf);
	}

	declare parents:(server.domain..*) implements HasEntityManager;

	@PersistenceUnit
	private static EntityManagerFactory emf;
}

当然代码是直接使用JPA的。
注意,DomainObject需要放到Spring事物里面执行
public class TestService {

	public void printEntityManager(String threadName) {
		...
	}


<?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:aop="http://www.springframework.org/schema/aop"
	xmlns:tx="http://www.springframework.org/schema/tx"
	xsi:schemaLocation="http://www.springframework.org/schema/beans 
	        http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
           http://www.springframework.org/schema/aop 
           http://www.springframework.org/schema/aop/spring-aop-2.0.xsd
           http://www.springframework.org/schema/tx 
           http://www.springframework.org/schema/tx/spring-tx-2.0.xsd">
   <!-- EntityManagerFactory -->
	<bean id="entityManagerFactory"	class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
		<property name="persistenceUnitManager">
			<bean class="org.springframework.orm.jpa.persistenceunit.DefaultPersistenceUnitManager">
				<property name="persistenceXmlLocations">
					<list>
						<value>classpath:jpa-persistence.xml</value>
					</list>
				</property>
			</bean>
		</property> 
		<property name="persistenceUnitName" value="thelog_db_jpa" />
	</bean>
	<!-- JPA Annotations support -->
	<bean class="org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor" />
	<!-- JPA Transaction -->
	<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
	   <property name="entityManagerFactory" ref="entityManagerFactory"/>
	</bean>
	<tx:advice id="allMethodTransactionAdvice" transaction-manager="transactionManager">
	   <tx:attributes>
	      <tx:method name="*"/>
	   </tx:attributes>
	</tx:advice>
	<aop:config>
		<aop:pointcut id="serviceMethods" expression="execution(public * net.common.thelog.server.service..*.*(..))"/>
		<aop:advisor pointcut-ref="serviceMethods" advice-ref="allMethodTransactionAdvice"/>
	</aop:config>
	<bean id="testService" class="net.common.thelog.server.service.TestService"/>
	<bean class="net.common.thelog.server.support.DomainSupportAspect" factory-method="aspectOf" />
</beans>

不过具体的事物/EntityManager部分,是否是隔离的还没有测试,目前还在探索中。
0 请登录后投票
   发表时间:2007-05-14  
注入都比较麻烦 现在的折衷办法就是回归原始 在DomainObject种放弃IOC 采用主动通过context查找
0 请登录后投票
论坛首页 Java企业应用版

跳转论坛:
Global site tag (gtag.js) - Google Analytics