F​l​e​x​4​+​s​p​r​i​n​g​+​h​i​b​e​r​n​a​t​e​+​B​l​a​z​e​D​S​整合案例

时间:2023-12-27 15:10:13

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):

F​l​e​x​4​+​s​p​r​i​n​g​+​h​i​b​e​r​n​a​t​e​+​B​l​a​z​e​D​S​整合案例

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

3、之后例如以下图所看到的,为项目加入flex项目类型:

F​l​e​x​4​+​s​p​r​i​n​g​+​h​i​b​e​r​n​a​t​e​+​B​l​a​z​e​D​S​整合案例

F​l​e​x​4​+​s​p​r​i​n​g​+​h​i​b​e​r​n​a​t​e​+​B​l​a​z​e​D​S​整合案例

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

F​l​e​x​4​+​s​p​r​i​n​g​+​h​i​b​e​r​n​a​t​e​+​B​l​a​z​e​D​S​整合案例

点击上图验证配置,通过后才干finish,根目录为公布到的tomcatserver中项目所在路径

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

F​l​e​x​4​+​s​p​r​i​n​g​+​h​i​b​e​r​n​a​t​e​+​B​l​a​z​e​D​S​整合案例

ok后,项目即正常。

由于默认生成的MyflexPro.mxml默认在src下,我们手动复制到flex_src下,将src下的删除就可以。

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

F​l​e​x​4​+​s​p​r​i​n​g​+​h​i​b​e​r​n​a​t​e​+​B​l​a​z​e​D​S​整合案例

解决的方法例如以下图:

F​l​e​x​4​+​s​p​r​i​n​g​+​h​i​b​e​r​n​a​t​e​+​B​l​a​z​e​D​S​整合案例

可能又报出例如以下错误:

F​l​e​x​4​+​s​p​r​i​n​g​+​h​i​b​e​r​n​a​t​e​+​B​l​a​z​e​D​S​整合案例

错误原因例如以下图的凝视方式<!-- -->不能在js代码中加入,将其改为//凝视方式就可以。

F​l​e​x​4​+​s​p​r​i​n​g​+​h​i​b​e​r​n​a​t​e​+​B​l​a​z​e​D​S​整合案例

測试代码例如以下图:

F​l​e​x​4​+​s​p​r​i​n​g​+​h​i​b​e​r​n​a​t​e​+​B​l​a​z​e​D​S​整合案例

F​l​e​x​4​+​s​p​r​i​n​g​+​h​i​b​e​r​n​a​t​e​+​B​l​a​z​e​D​S​整合案例

訪问结果页面:

F​l​e​x​4​+​s​p​r​i​n​g​+​h​i​b​e​r​n​a​t​e​+​B​l​a​z​e​D​S​整合案例

二、整合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,内容例如以下:

<?xml version="1.0"?>
<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:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:jaxws="http://cxf.apache.org/jaxws"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd "> </beans>

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

	<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>

启动项目,就可以看到输出中载入了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中加入例如以下内容(组件扫描 节点下的内容须要做对应更改):

	<!-- 分散配置,指定属性文件地址 -->
<context:property-placeholder location="classpath:jdbc.properties"/>
<!-- 组件扫描 -->
<context:component-scan base-package="com.zxn.project.dao.impl,com.zxn.project.service.impl" />
<!-- 配置c3p0数据源 -->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="${jdbc.driverclass}" />
<property name="jdbcUrl" value="${jdbc.url}" />
<property name="user" value="${jdbc.username}" />
<property name="password" value="${jdbc.password}" /> <property name="acquireIncrement" value="${c3p0.acquireIncrement}" />
<property name="initialPoolSize" value="${c3p0.initialPoolSize}" />
<property name="minPoolSize" value="${c3p0.minPoolSize}" />
<property name="maxPoolSize" value="${c3p0.maxPoolSize}" />
<property name="maxStatements" value="${c3p0.maxStatements}" />
<property name="maxStatementsPerConnection" value="${c3p0.maxStatementsPerConnection}" />
<property name="checkoutTimeout" value="${c3p0.checkoutTimeout}" />
<property name="idleConnectionTestPeriod" value="${c3p0.idleConnectionTestPeriod}" />
</bean> <!-- 本地会话工厂bean(spring整合hibernate资源和核心入口)-->
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">${hibernate.dialect}</prop>
<prop key="hibernate.hbm2ddl.auto">${hibernate.hbm2ddl.auto}</prop>
<prop key="hibernate.show_sql">${hibernate.show_sql}</prop>
</props>
</property>
<property name="mappingDirectoryLocations">
<list>
<value>classpath:com/zxn/project/domain</value>
</list>
</property>
</bean> <!-- hibernate事务管理器,在service层面上实现事务管理,可以达到平台无关性 -->
<bean id="txManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory" />
</bean> <!-- 事务通知 -->
<tx:advice id="txAdvice" transaction-manager="txManager">
<tx:attributes>
<tx:method name="save*" propagation="REQUIRED" isolation="DEFAULT"/>
<tx:method name="update*" propagation="REQUIRED" isolation="DEFAULT"/>
<tx:method name="delete*" propagation="REQUIRED" isolation="DEFAULT"/>
<tx:method name="batch*" propagation="REQUIRED" isolation="DEFAULT"/> <tx:method name="get*" propagation="REQUIRED" isolation="DEFAULT" read-only="true"/>
<tx:method name="load*" propagation="REQUIRED" isolation="DEFAULT" read-only="true"/>
<tx:method name="find*" propagation="REQUIRED" isolation="DEFAULT" read-only="true"/> <tx:method name="*" propagation="REQUIRED" isolation="DEFAULT" read-only="true"/>
</tx:attributes>
</tx:advice> <bean id="hibernateTemplate" class="org.springframework.orm.hibernate3.HibernateTemplate">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean> <!-- aop配置 -->
<aop:config>
<aop:advisor advice-ref="txAdvice" pointcut="execution(* *..*Service.*(..))"/>
</aop:config>

3.3、新建SpringFactory.java内容例如以下(我是手动集成,该类没自己主动生成):

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).
*
* <factories>
* <factory id="spring" class="flex.samples.factories.SpringFactory" />
* </factories>
*
* 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:
*
* <context-param>
* <param-name>contextConfigLocation</param-name>
* <param-value>/WEB-INF/applicationContext.xml</param-value>
* </context-param>
*
* <listener>
* <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
* </listener>
*
* Then you put your spring bean configuration in WEB-INF/applicationContext.xml (as per the
* line above). For example:
*
* <?xml version="1.0" encoding="UTF-8"?>
* <!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">
*
* <beans>
* <bean name="weatherBean" class="dev.weather.WeatherService" singleton="true"/>
* </beans>
*
* 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:
*
* <destination id="WeatherService">
* <properties>
* <factory>spring</factory>
* <source>weatherBean</source>
* </properties>
* </destination>
*
* @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.
* <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;
}
} } }

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

     <factories>
<factory id="spring" class="cn.dyst.vmamcm.aftsrv.util.SpringFactory"/>
</factories>

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

	<destination id="xxxService">
<properties>
<factory>spring</factory>
<source>xxxService</source>
</properties>
</destination>

就能够在页面訪问数据库了。

四、加入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

整合完成。