Flex4+spring+hibernate+BlazeDS整合案例

http://wenku.baidu.com/link?url=7v5xAyD2nvChQRT60QewpvAASFHMZNvD0OxX11OASYvae8jbVNsr5I000TwwYOlChzq05c50utxcLVb6mRL_r1pSFM1QZ2p9hJvRnpeT-T7

前提是系统安装好flex插件,Myeclipse,jdk,tomcat,并将flex插件集成到Myeclipse中

http://hi.baidu.com/xzwangyu/item/5bdc91c3c3439d310831c65c

一、将webProject和flex4、Blazeds整合

1、下载​B​l​a​z​e​D​S​,地址为:

2、打开Myeclipse,建立web project(eclipse为dynamic web project),如图(图中项目名称为MyFlexPro2实际为MyflexPro):

Flex4+spring+hibernate+BlazeDS整合案例_第1张图片

2、解压缩​B​l​a​z​e​D​S​,将web-inf下所有文件,拷贝覆盖项目的web-inf中,然后发布到tomcat,运行tomcat。

3、之后如下图所示,为项目添加flex项目类型:

Flex4+spring+hibernate+BlazeDS整合案例_第2张图片

Flex4+spring+hibernate+BlazeDS整合案例_第3张图片


下图中的根URL没有改正,应为http://localhost:8080/MyflexPro

Flex4+spring+hibernate+BlazeDS整合案例_第4张图片

点击上图验证配置,通过后才能finish,根文件夹为发布到的tomcat服务器中项目所在路径


finish后,项目大致框架如下图,项目上会出现错误,项目上右键选择属性,解决flex的编译位置为src和java冲突,如下图更改:

Flex4+spring+hibernate+BlazeDS整合案例_第5张图片

ok后,项目即正常。

因为默认生成的MyflexPro.mxml默认在src下,我们手动拷贝到flex_src下,将src下的删除即可。


另外,项目还可能有错误,但是就是找不到项目中哪个文件报错,此时,打开Problems视图,如下:

Flex4+spring+hibernate+BlazeDS整合案例_第6张图片

解决办法如下图:

Flex4+spring+hibernate+BlazeDS整合案例_第7张图片


可能又报出如下错误:

Flex4+spring+hibernate+BlazeDS整合案例_第8张图片


错误原因如下图的注释方式不能在js代码中添加,将其改为//注释方式即可。

Flex4+spring+hibernate+BlazeDS整合案例_第9张图片


测试代码如下图:

Flex4+spring+hibernate+BlazeDS整合案例_第10张图片

Flex4+spring+hibernate+BlazeDS整合案例_第11张图片


访问结果页面:

Flex4+spring+hibernate+BlazeDS整合案例_第12张图片

二、整合Spring

2.1、添加JAR包如下:

org.springframework.aop-3.1.1.RELEASE.jar
org.springframework.asm-3.1.1.RELEASE.jar
org.springframework.beans-3.1.1.RELEASE.jar
org.springframework.context-3.1.1.RELEASE.jar
org.springframework.context.support-3.1.1.RELEASE.jar
org.springframework.core-3.1.1.RELEASE.jar
org.springframework.expression-3.1.1.RELEASE.jar
org.springframework.jdbc-3.1.1.RELEASE.jar
org.springframework.orm-3.1.1.RELEASE.jar
org.springframework.transaction-3.1.1.RELEASE.jar
org.springframework.web-3.1.1.RELEASE.jar
com.springsource.net.sf.cglib-2.2.0.jar
com.springsource.org.aopalliance-1.0.0.jar
com.springsource.org.apache.commons.logging-1.1.1.jar
com.springsource.org.aspectj.tools-1.6.6.RELEASE.jar 

com.springsource.org.aspectj.weaver-1.6.8.RELEASE.jar 

javassist-3.11.0.GA.jar

jta-1.1.jar    (缺少会报Caused by: java.lang.ClassNotFoundException: javax.transaction.TransactionManager错误)

dom4j-1.6.1.jar (缺少会报Caused by: java.lang.ClassNotFoundException: org.dom4j.DocumentException错误)
slf4j-api-1.6.6.jar(缺少会报Caused by: java.lang.ClassNotFoundException: org.slf4j.LoggerFactory错误)
slf4j-log4j12-1.6.6.jar(会警告,hibernate无任何打印信息)

2.2、在项目下新建源文件夹(source folder)conf,新建xml文件applicationContext.xml,内容如下:



	
	

2.3、在web.xml中web-app节点下添加监听,如下:

	
		contextConfigLocation
		classpath:applicationContext.xml
	
	
		org.springframework.web.context.ContextLoaderListener
	

启动项目,即可看到输出中加载了applicationContext配置文件。

三、整合hibernate

3.1、添加一下JAR包:

hibernate3.jar
ojdbc14.jar
c3p0-0.9.1.jar

antlr-2.7.6.jar

commons-collections-3.1.jar (缺失则报Caused by: java.lang.ClassNotFoundException: org.apache.commons.collections.map.LRUMap错误)

3.2、添加数据库连接配置文件到conf目录下

jdbc.properties

jdbc.driverclass=oracle.jdbc.driver.OracleDriver
jdbc.url=jdbc:oracle:thin:@192.168.1.201:1521:sunlight
hibernate.dialect=org.hibernate.dialect.OracleDialect
jdbc.username=its
jdbc.password=vehicle

c3p0.acquireIncrement=5
c3p0.initialPoolSize=30
c3p0.minPoolSize=30
c3p0.maxPoolSize=100
c3p0.maxStatements=0
c3p0.maxStatementsPerConnection=5
c3p0.checkoutTimeout=30000
c3p0.idleConnectionTestPeriod=60

#hibernate.hbm2ddl.auto=none
hibernate.hbm2ddl.auto=update
hibernate.show_sql=true

在applicationContext.xml中添加如下内容(组件扫描 节点下的内容需要做相应更改):

	
	
	
	
	
	
		
		
		
		
		
		
		
		
		
		
		
		
		
	
	
	
	
		
		
			
				${hibernate.dialect}
				${hibernate.hbm2ddl.auto}
				${hibernate.show_sql}
			
		
		
			
				classpath:com/zxn/project/domain
			
		
	
	
	
	
		
	
	
	
	
		
			
			
			
			
			
			
			
			
			
			
		
	
	
	
		
	
	
	
	
		
	

3.3、新建SpringFactory.java内容如下(flex与spring集成需要):

package cn.dyst.vmamcm.aftsrv.util;
import org.springframework.context.ApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.NoSuchBeanDefinitionException;

import flex.messaging.FactoryInstance;
import flex.messaging.FlexFactory;
import flex.messaging.config.ConfigMap;
import flex.messaging.services.ServiceException;

/**
 * This interface is implemented by factory components which provide
 * instances to the flex messaging framework.  To configure flex data services
 * to use this factory, add the following lines to your services-config.xml
 * file (located in the WEB-INF/flex directory of your web application).
 *
 *	
 *     
 *  
 *
 * You also must configure the web application to use spring and must copy the spring.jar
 * file into your WEB-INF/lib directory.  To configure your app server to use spring,
 * you add the following lines to your WEB-INF/web.xml file:
 *
 *   
 *        contextConfigLocation
 *        /WEB-INF/applicationContext.xml
 *   
 *
 *   
 *       org.springframework.web.context.ContextLoaderListener
 *   
 * 
 * Then you put your spring bean configuration in WEB-INF/applicationContext.xml (as per the
 * line above).  For example:
 * 
 *  
 *  
 *   
 *  
 *    
 *  
 *  
 * Now you are ready to define a destination in flex that maps to this existing service. 
 * To do this you'd add this to your WEB-INF/flex/remoting-config.xml:
 *
 *  
 *      
 *          spring
 *          weatherBean
 *      
 *  
 *
 * @author Jeff Vroom
 */
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.
     * 

* 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; } } } }


之后,在 services-config.xml文件中配置如下:

     
        
    
最后,在remoting-config.xml中添加代码(举例)如下:

	
		
			spring
			xxxService
		 
	 

注意:

xxxService为使用spring管理的bean(注解方式或者xml方式),xxxService必须需要实现类和接口类,如xxxService与xxxServiceImpl
就可以在页面访问数据库了。

四、添加Log4j到项目中

4.1、添加jar包

log4j-1.2.17.jar

4.2、添加log4j.properties配置文件到conf目录下,

内容如下:

### set log levels - for more verbose logging change 'info' to 'debug' ###
log4j.rootLogger=warn, stdout, file, error

### direct log messages to stdout ###
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.Target=System.out
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d{ABSOLUTE} %5p %c{1}:%L - %m%n

### direct messages to file ###
log4j.appender.file=org.apache.log4j.DailyMaxRollingFileAppender
log4j.appender.file.layout=org.apache.log4j.PatternLayout
log4j.appender.file.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} [%t:%r]-[%p] %m%n
log4j.appender.file.File=/log.log
log4j.appender.file.datePattern='.'yyyy-MM-dd'.log'
log4j.appender.file.maxBackupIndex=30
og4j.appender.file.Append=true
log4j.appender.file.Threshold=debug

### direct exception messages to separate file ###
log4j.appender.error=org.apache.log4j.DailyMaxRollingFileAppender
log4j.appender.error.File=/error.log
log4j.appender.error.Append=true
log4j.appender.error.Threshold=error
log4j.appender.error.layout=org.apache.log4j.PatternLayout
log4j.appender.error.datePattern='.'yyyy-MM-dd'.log'
### 设置日志最多保存的天数 ###
log4j.appender.error.maxBackupIndex=30
log4j.appender.error.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} [%t:%r]-[%p] %m%n

#log4j.logger.org.hibernate=info
log4j.logger.org.hibernate=info

### log HQL query parser activity
#log4j.logger.org.hibernate.hql.ast.AST=debug

### log just the SQL
#log4j.logger.org.hibernate.SQL=debug

### log JDBC bind parameters ###
log4j.logger.org.hibernate.type=info
#log4j.logger.org.hibernate.type=debug

### log schema export/update ###
log4j.logger.org.hibernate.tool.hbm2ddl=debug

### log HQL parse trees
#log4j.logger.org.hibernate.hql=debug

### log cache activity ###
#log4j.logger.org.hibernate.cache=debug

### log transaction activity
#log4j.logger.org.hibernate.transaction=debug

### log JDBC resource acquisition
#log4j.logger.org.hibernate.jdbc=debug

### enable the following line if you want to track down connection ###
### leakages when using DriverManagerConnectionProvider ###
#log4j.logger.org.hibernate.connection.DriverManagerConnectionProvider=trace
整合完毕。

你可能感兴趣的:(flex,javaWeb)