昨天到今天发了我整整两天配置Flex+Spring+Hibernate,开始一直报一个错误,
nested exception is java.lang.NoSuchMethodError: org.objectweb.asm.ClassVisitor.visit(IILjava/lang/String;Ljava/lang/String;
[Ljava/lang/String;Ljava/lang/String;)V
我在网上查了资料原因是因为Hibernate中用到asm与spring aop 中用到Asm冲突,当时我犯一个很大的错误,那就是我除了在classpath中引入了相关的spring,hibernate包以外,还把那些jar包全拷贝到了WEB-INF/lib下面,我在删除WEB-INF/lib下面,Hibernate中的如下包:
asm.jar
asm-attrs.jar
cglib-2.1.3.jar
留下了spring aop 中的如下包:
asm.jar
asm-commons.jar
asm-util.jar
cglib-nodep.jar
本来按道理可以运行了,但还是报同样的错误,我试了一次又一次,每次都是失败.直到刚我才发现,原来用myeclipse引入Hibernate与spring时,它会把上面所提到的所有jar包全部引入进来.这样就导致了错误的开始...所以我觉得我们配置上述环境的时候,最好还是自己设置自己的user lib(window-->preferences-->Java-->Buildpath-->Usre Librayries-->new)在这里分别创建user_spring与user_hibernate,其中usr_spring中要引入spring aop libraries,spring core libraries,spring persistence jdbc libraries,spring persisitence core libraries.
user_hibernate包下要引入除了
asm.jar
asm-attrs.jar
cglib-2.1.3.jar
之外的所有Hibernate jar包.
包图如下:
其中spring aop 中的包如下:
而hibernate包中,已经没有了
asm.jar
asm-attrs.jar
cglib-2.1.3.jar
spring+hibenate+flex的配置过程,还需要一个SpringFactory
它的代码如下:
package dxm.blog.common; import org.springframework.beans.BeansException; import org.springframework.beans.factory.NoSuchBeanDefinitionException; import org.springframework.context.ApplicationContext; import org.springframework.web.context.support.WebApplicationContextUtils; import flex.messaging.FactoryInstance; import flex.messaging.FlexFactory; import flex.messaging.config.ConfigMap; import flex.messaging.services.ServiceException; public class SpringFactory implements FlexFactory { private static final String SOURCE = "source"; /** * This method can be used to initialize the factory itself. It is called with configuration * parameters from the factory tag which defines the id of the factory. */ public void initialize(String id, ConfigMap configMap) {} /** * This method is called when we initialize the definition of an instance * which will be looked up by this factory. It should validate that * the properties supplied are valid to define an instance. * Any valid properties used for this configuration must be accessed to * avoid warnings about unused configuration elements. If your factory * is only used for application scoped components, this method can simply * return a factory instance which delegates the creation of the component * to the FactoryInstance's lookup method. */ public FactoryInstance createFactoryInstance(String id, ConfigMap properties) { SpringFactoryInstance instance = new SpringFactoryInstance(this, id, properties); instance.setSource(properties.getPropertyAsString(SOURCE, instance.getId())); return instance; } // end method createFactoryInstance() /** * Returns the instance specified by the source * and properties arguments. For the factory, this may mean * constructing a new instance, optionally registering it in some other * name space such as the session or JNDI, and then returning it * or it may mean creating a new instance and returning it. * This method is called for each request to operate on the * given item by the system so it should be relatively efficient. * <p> * If your factory does not support the scope property, it * report an error if scope is supplied in the properties * for this instance. */ public Object lookup(FactoryInstance inst) { SpringFactoryInstance factoryInstance = (SpringFactoryInstance) inst; return factoryInstance.lookup(); } static class SpringFactoryInstance extends FactoryInstance { SpringFactoryInstance(SpringFactory factory, String id, ConfigMap properties) { super(factory, id, properties); } public String toString() { return "SpringFactory instance for id=" + getId() + " source=" + getSource() + " scope=" + getScope(); } public Object lookup() { ApplicationContext appContext = WebApplicationContextUtils.getWebApplicationContext(flex.messaging.FlexContext.getServletConfig().getServletContext()); String beanName = getSource(); try { return appContext.getBean(beanName); } catch (NoSuchBeanDefinitionException nexc) { ServiceException e = new ServiceException(); String msg = "Spring service named '" + beanName + "' does not exist."; e.setMessage(msg); e.setRootCause(nexc); e.setDetails(msg); e.setCode("Server.Processing"); throw e; } catch (BeansException bexc) { ServiceException e = new ServiceException(); String msg = "Unable to create Spring service named '" + beanName + "' "; e.setMessage(msg); e.setRootCause(bexc); e.setDetails(msg); e.setCode("Server.Processing"); throw e; } } } }
以上代码显然是在网上找的,你把这个java文件放到你的某个包下,然后再到services-config.xml中的最后加入如入代码:
<factories> <factory id="spring" class="dxm.blog.common."/> </factories>
然后你在远程方法的配置中WEB-INF/flex/remoting-config.xml中配置你的远程方法时如下:
<destination id="userHandler"> <properties> <factory>spring</factory> <source>userHandler</source> </properties> </destination>
其中的spring对应上面配置的SpringFactory至于其它spring与hibernater的集成与javaweb工程里面是一样的.
在web.xml中配置要添加对flex的一些配置,其它的与以前一样,具体如下:
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" "http://java.sun.com/dtd/web-app_2_3.dtd"> <web-app> <display-name>Flex</display-name> <description> LiveCycle Data Services Application </description> <context-param> <param-name>flex.class.path</param-name> <param-value> /WEB-INF/flex/hotfixes,/WEB-INF/flex/jars </param-value> </context-param> <context-param> <param-name> contextConfigLocation </param-name> <param-value> /WEB-INF/applicationContext-*.xml </param-value> </context-param> <!-- hibernate --> <filter> <filter-name>hibernateFilter</filter-name> <filter-class> org.springframework.orm.hibernate3.support. OpenSessionInViewFilter </filter-class> </filter> <filter-mapping> <filter-name>hibernateFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <listener> <listener-class> flex.messaging.HttpFlexSession </listener-class> </listener> <listener> <listener-class> org.springframework.web.context.ContextLoaderListener </listener-class> </listener> <!-- MessageBroker Servlet --> <servlet> <servlet-name>MessageBrokerServlet</servlet-name> <display-name>MessageBrokerServlet</display-name> <servlet-class> flex.messaging.MessageBrokerServlet </servlet-class> <init-param> <param-name>services.configuration.file</param-name> <param-value> /WEB-INF/flex/services-config.xml </param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>MessageBrokerServlet</servlet-name> <url-pattern>/messagebroker/*</url-pattern> </servlet-mapping> <welcome-file-list> <welcome-file>index.html</welcome-file> <welcome-file>index.htm</welcome-file> </welcome-file-list> </web-app>
以附件中,我将
一:flex第一步的配置文件(包括flexbuilder的安装,以前flex web环境的搭建,还有简单的flex与java通信)以 二:flex+hibernate+spring的简单工程 (我的个人博客设计)
发给大家,有兴趣的朋友可以下载,希望能为你们节省一些时间.不要重走我的弯路.
这个东西整整花了我两天,到最后还是把问题给解决了,有一种体会,那就是不管遇到什么困难,只要你肯坚持,到最后没有战胜不了的,最大的困难其实是你自己.