Flex 延迟加载问题解决方案
2011-07-16 13:54
66 查看
有很多同学在用使用flex+hibernate做应用开发时都会出现由延迟加载导致的异常:Could not initialize proxy…………….
笔者在使用ibatis2.x做持久层时,也遇到这个问题,究其原因,在于blazeds无法正常序列化Java端返回的代理类。
而hibernate默认会对类属性和集合属性返回代理,在大多数情况下这都是很合理的,即使要在web端访问代理属性,也可以采用openSessionInView模式,将session延伸到web层。但是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端之前,都被拦截,进行转化后,可以正常序列化了。
声明:如需转载,请注明作者-hhlai1990@gmail.com
欢迎大家使用rapid-framework快速开发框架以及rapid-generator代码生成器
http://code.google.com/p/rapid-framework
笔者在使用ibatis2.x做持久层时,也遇到这个问题,究其原因,在于blazeds无法正常序列化Java端返回的代理类。
而hibernate默认会对类属性和集合属性返回代理,在大多数情况下这都是很合理的,即使要在web端访问代理属性,也可以采用openSessionInView模式,将session延伸到web层。但是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端之前,都被拦截,进行转化后,可以正常序列化了。
声明:如需转载,请注明作者-hhlai1990@gmail.com
欢迎大家使用rapid-framework快速开发框架以及rapid-generator代码生成器
http://code.google.com/p/rapid-framework
相关文章推荐
- jquery图片延迟加载方案解决图片太多加载缓慢问题
- Flex中采用主SWF文件加载子SWF文件中添加了Chart后,导致at mx.charts.chartClasses::ChartBase/initStyles()异常的问题解决方法
- hibernate 延迟加载(Lazy)问题spring下解决方法
- Android中ViewPager+Fragment取消(禁止)预加载延迟加载(懒加载)问题解决方案
- json解决hibernate中级联对象延迟加载问题net.sf.json.JSONException: org....
- android中ListView异步加载图片时的图片错位问题解决方案(1)
- 关于Firefox重新加载flex的问题解决
- flex解决用image加载swf文件后页面异常放大的问题
- Android之批量加载图片OOM问题解决方案
- VS2010(64位) DLL 无法正常加载问题解决方案
- 解决Ext中的Tabpanel中items内的各个tab的数据延迟加载问题
- Hibernate+spring数据延迟加载问题解决方案。
- IOS懒加载不调用问题解决方案
- webview 加载H5页面音乐不会自动播放问题解决方案
- SSH项目web.xml文件的常用配置【struts2的过滤器、spring监听器、解决Hibernate延迟加载问题的过滤器、解决中文乱码的过滤器】
- ArcGIS Desktop 打开后一直处于 “正在加载应用程序……” 问题解决方案
- listview中使用checkbox滑动或者加载更多数据错乱问题解决方案
- hibernate 使用延迟加载产生的问题以及对应的解决方法
- android中ListView异步加载图片时的图片错位问题解决方案
- flex解决用image加载swf文件后页面异常放大的问题