最近在搞flex+java,用了spring和hibernate,通过blazeds通信。但是我发现blazeds无法实现hibernate的延迟加载功能,更可恨的是当进行通信的时候,对象中的所有关联关系都被强制加载了,这与blazeds的原理有关(blazeds将对象所有的属性进行了遍历,造成了属性的加载)。网上有些flex的延迟加载解决方案,比如dbhibernate,但是并不好用。
我现在的解决方法是:去掉了绝大部分的关联关系,关掉了延迟加载功能(没有了关联关系,延迟加载也没有意义了),这样就靠自己维护关系了。只是比以前多出了一点代码量。
你也可以直接不用hibernate。
或者自己对hibernate取到的对象进行一次封装,再传给flex,这样就不会被强制加载了
针对这个问题,有一些开源的解决方案:dpHibernate,Glead等,但笔者感觉这些框架侵入性太强,不宜采用。
在不引入第三方框架的情况下,可以采取两个办法:1.增加一层Vo类,将类属性和集合属性的代理进行转化,然后与as映射,传给flex端 ,这样序列化是ok的。
2.采用Spring 的aop解决方案,对可能返回无法正常序列化的pojo方法进行拦截。步骤如下:
1.开启@AspectJ
<aop:aspectj-autoproxy/>
2.编写针对具有代理属性类的转化工具类:假如Student类具有多对一的类属性Grade:
public class Student {
private Long id;
private Grade grade;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public Grade getGrade() {
return grade;
}
public void setGrade(Grade grade) {
this.grade = grade;
}
}
用代码生成器生成StudentConvert.java :
public class StudentConverter {
public static Student convert2Student(Object sourceObj) {
Student source=(Student) sourceObj;
Student target = new Student();
target.setId(source.getId());
//Hibernate.initialize(source.getGrade()); 可以选择将类或集合代理初始化
//target.setGrade(source.getGrade());
return target;
}
public static List convert2StudentList(List oldlist) {
List newlist= new ArrayList();
for(Object sourceObj : oldlist) {
newlist.add(convert2Student(sourceObj));
}
return newlist;
}
}
3.编写aop的advisor进行环绕通知,假设StudentDao 有两个方法
Student findById(Long id)
List findAll();
由代码生成器生成对应的advisor如下 HibernateAdvisor.java :
@Aspect
public class HibernateAdvisor {
//insert aop code for hibernate
///////////////////////////
// Student readme
///////////////////////////
/**
* 必须为final String类型的,注解里要使用的变量只能是静态常量类型的
*/
// 切入点定义
public static final String StudentList = "execution(* com.lai.flex.service.StudentDao.findAll(..))";
public static final String Student = "execution(* com.lai.flex.service.StudentDao.findBy*(..))";
@Around(StudentList)
public List convertStudentList(ProceedingJoinPoint joinPoint) {
//System.out.println("Convert之前");
Object[] args = joinPoint.getArgs();
Object obj = null;
List newlist= null;
try {
obj = joinPoint.proceed(args);
List old= (List) obj;
newlist=(StudentConverter.convert2StudentList(oldlist));
} catch (Throwable e) {
e.printStackTrace();
}
//System.out.println("Convert之后"); // 方法执行后的代理处理
return newlist;
}
@Around(Student)
public Student convertStudent(ProceedingJoinPoint joinPoint) {
//System.out.println("Convert之前");
Object[] args = joinPoint.getArgs();
Object obj = null;
Student newsStudent = null;
try {
obj = joinPoint.proceed(args);
newsStudent = StudentConverter.convert2Student(obj);
} catch (Throwable e) {
e.printStackTrace();
}
//System.out.println("Convert之后"); // 方法执行后的代理处理
return newsStudent;
}
// generator-insert-location
}
最后,由StudentDao方法返回的代理类和集合属性在序列化到flex端之前,都被拦截,进行转化后,可以正常序列化了。
转自http://67566894.iteye.com/blog/686601
http://archive.cnblogs.com/a/1843018/
分享到:
相关推荐
介绍几种解决Flex 与 Hibernate 的延迟加载问题。我在实际项目中使用的是Gilead。
Flex 延迟加载 问题说明 处理 实现
完美的Flex多语言支持解决方案
flexBulider页面加载动态图片的显示
flex整合hibernate,spring,详细配置
适用于有spring,hibernate搭建有了解的并且知道flex开发的人员,只是基础的spring+hibernate+flex项目搭建,新手入门哦!
Flex之URLLoader加载XML 使用URLLoader加载XML
flex4.5+spring3+hibernate的项目实例,登录实例
这个实例是我看到得flex+hibernate 最经典的实例
偌大的一个csdn竟然都没有发现一个完整的整合版flex,spring,hibernate。无奈之下自己经过一个半的努力实现了flex调用springAOP管理的java方法,项目中也有我写好的hibernate方法。
Flex中加载SWF方法,介绍了flex 对 swf 加载和处理
很详细的实现了flex游戏的预加载,可以使用图片,也可以重绘技术,任你选择
FLex整合hibernate spring
flex自定义的预加载进度条动画的一个demo,非常使用。
这个是一个完整的Flex与java通信的一个小例子,实现了增删改查功能,这只是一个小例子 有不妥当的地方希望能包涵。 涉及 技术:Flex+struts2+hibernate+mysql
自己写的Flex+spring+hibernate示例,精简易懂
NULL 博文链接:https://demojava.iteye.com/blog/1337123
flex arcgis 加载天地图2.0地图源码
Flex加载进度流程图以及说明文字,通过它可以粗线条体了解Flex工程师的设计思路。
Flex+Spring+Hibernate 整合的方式有多种,本文是利用flex builder插件,将flex与javaEE服务器端整合到一个工程里面,以便程序员可以在一个IDE里完成开发。