`

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

阅读更多
为了获得完整的RDO,将Dao注入查了一些资料,大概有三种办法:
1、手工设置,对生成的每个对象调用SetDao()。
2、使用AspectJ的AOP在编译时完成对new()的增强。
3、使用Spring的build-time weaving(同2) or load-time weaving。

第一种方法除了比较繁琐,而且会对Spring Context产生依赖。
第二种要求使用AspectJ,对于大项目使用这个东西编译比较痛苦。
第三种不知道有人用过没有?
是否还有其它方法?谢谢。
分享到:
评论
4 楼 Norther 2007-05-14  
注入都比较麻烦 现在的折衷办法就是回归原始 在DomainObject种放弃IOC 采用主动通过context查找
3 楼 pig345 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部分,是否是隔离的还没有测试,目前还在探索中。
2 楼 CurrentJ 2007-04-09  
准备采用如下方式:
1、将DO放入Spring容器,利用Spring Ioc将DAO注入。
2、建立工厂接口,实现一个依赖Spring的工厂类。
3、所有创建DO的地方使用该工厂。
4、放弃ORM,适用Spring Jdbc。
1 楼 drliujia 2007-04-06  
可以考虑static dao,一次性注入

相关推荐

    Spring-Reference_zh_CN(Spring中文参考手册)

    6.8.1. 在Spring中使用AspectJ来为domain object进行依赖注入 6.8.1.1. @Configurable object的单元测试 6.8.1.2. 多application context情况下的处理 6.8.2. Spring中其他的AspectJ切面 6.8.3. 使用Spring IoC来...

    Spring 2.0 开发参考手册

    6.8.1. 在Spring中使用AspectJ来为domain object进行依赖注入 6.8.2. Spring中其他的AspectJ切面 6.8.3. 使用Spring IoC来配置AspectJ的切面 6.8.4. 在Spring应用中使用AspectJ Load-time weaving(LTW) 6.9. ...

    Spring中文帮助文档

    6.8.1. 在Spring中使用AspectJ进行domain object的依赖注入 6.8.2. Spring中其他的AspectJ切面 6.8.3. 使用Spring IoC来配置AspectJ的切面 6.8.4. 在Spring应用中使用AspectJ加载时织入(LTW) 6.9. 更多资源 7...

    Spring API

    6.8.1. 在Spring中使用AspectJ进行domain object的依赖注入 6.8.2. Spring中其他的AspectJ切面 6.8.3. 使用Spring IoC来配置AspectJ的切面 6.8.4. 在Spring应用中使用AspectJ加载时织入(LTW) 6.9. 更多资源 7...

    spring chm文档

    6.8.1. 在Spring中使用AspectJ来为domain object进行依赖注入 6.8.2. Spring中其他的AspectJ切面 6.8.3. 使用Spring IoC来配置AspectJ的切面 6.8.4. 在Spring应用中使用AspectJ Load-time weaving(LTW) 6.9. ...

    spring jpetstore spring附带的例子

    3. 模型(Model):Spring MVC中的模型通常由服务层对象(Service)和领域对象(Domain Object)组成。例如,Product、Category等实体类代表了应用的数据模型,而对应的服务类则封装了业务逻辑,如添加、删除和查询...

    spring_JdbcTemplete使用详解

    ### Spring JDBC 模板类(JdbcTemplate)使用详解 #### 一、Spring JDBC 概述 Spring 提供了一个强大的模板类 `JdbcTemplate` 来简化 JDBC 操作。通过使用 `JdbcTemplate`,开发者能够减少大量的样板代码,提高...

    Service层和DAO层解析

    Spring框架通过依赖注入(Dependency Injection,DI)实现了这一目标,使得Service层可以通过接口引用DAO层的实现,而不是直接创建DAO实例。这增强了系统的可测试性和可扩展性,因为Service层可以通过配置文件或注解...

    spring-hibernate.zip

    这样,我们可以在不侵入业务逻辑代码的情况下,实现事务的回滚和提交。 6. **DAO和Service层设计**:在Spring-Hibernate整合中,通常会创建DAO(Data Access Object)接口和其实现类,用于数据库交互。Service层则...

    struts+hibernate+spring开发教程

    5. 逆向工程:使用Hibernate逆向工程生成与`user`表对应的VO(Value Object)类,通常位于`vo`包下。此外,创建一个名为`dao`的包,将`UserDAO.java`移动到其中,并更新`applicationContext.xml`中的相关配置。 6. ...

    基于struts2 spring2.5 hibernate3的人事管理系统 源码完整包

    在这个基于Struts2、Spring2.5和Hibernate3的人事管理系统中,贫血模型的DAO层意味着领域对象(Domain Objects)与业务逻辑相分离。DAO(Data Access Object)主要负责数据的存取操作,不包含任何业务逻辑,这有利于...

    spring Hibernate与struts整合的过程

    2. **性能测试**:评估系统的性能表现,确保在高并发情况下也能稳定运行。 3. **安全测试**:检查系统是否存在安全漏洞,如SQL注入、XSS攻击等。 通过上述步骤,我们可以成功地将Spring、Hibernate与Struts框架整合...

    Struts+Spring+Hibernate

    例如,Spring配置文件中会声明业务服务和DAO(Data Access Object)的bean,以及与Hibernate的集成设置;Struts配置文件则定义Action和ActionForm,以及它们如何映射到URL路径。 总的来说,Struts+Spring+Hibernate...

    struts2+spring+hibernate

    在本项目中,`domain`包下的实体类(如Equipment)代表数据库中的表,`dao`(Data Access Object)层使用Hibernate的Session接口进行数据库操作,如查询、插入、更新和删除。 **Service层**:服务层是业务逻辑的...

    SSH整合包详解.Struts2.2.3+Spring3.1.0.M2+Hibernate3.6.6

    该框架通过合理的职责划分,将应用分为四层:表示层(Presentation Layer)、业务逻辑层(Business Logic Layer)、数据持久层(Data Persistence Layer)和域模块层(Domain Module Layer)。 1. Struts2:作为...

    Spring.NET + NHibernate + ASP.NET MVC + jQuery + easyUI 中英文双语言小型企业网站Demo

    - **WebSite**、**Service**、**Common**、**Dao**、**Domain**:这些可能是项目的主要代码目录,分别代表Web应用、服务层、公共类库、数据访问层和领域模型。 综上所述,这个项目是一个综合性的企业网站演示,利用...

    ssh架构简介.pdf

    通过Ajax,用户可以在不刷新整个页面的情况下更新部分视图,提升应用的响应速度。 SSH架构在Java Web开发中具有广泛的应用,它通过各层的分离,实现了职责明确、易于维护和扩展的设计。不过随着技术的发展,如今...

    一个springmvc框架

    这些服务类可以使用 Spring 的依赖注入(Dependency Injection, DI)特性,便于测试和维护。 3. **Common**:这个目录可能是通用工具类、枚举、常量或帮助类的集合,它们提供了一些跨模块的通用功能,例如日期时间...

Global site tag (gtag.js) - Google Analytics