spring +hibernate+flex搭建

搭建过程参考: http://www.pefj.info/archives/21


我的环境:spring3.1
          flex4/flex4.5
          hibernate3.6.1
          jdk1.6
          myeclipse9.0
          tomcat6

因为搭建过程网上很多,就不介绍,很多人教程搭建还是出错。下面是配置完之后的配置文件:

src下面:

1.jdbc.property
jdbc.driverClassName=com.mysql.jdbc.Driver
jdbc.url=jdbc\:mysql\://localhost\:3306/flexmail
jdbc.username=root
jdbc.password=root
jdbc.maxActive=10
jdbc.maxIdle=6
jdbc.maxWait=10000
jdbc.defaultAutoCommit=true
jdbc.poolPreparedStatements=true


2.将hibernate配置写到spring配置文件 src下面的bean.xml 中

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:flex="http://www.springframework.org/schema/flex"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
	xmlns:context="http://www.springframework.org/schema/context"
	xmlns:aop="http://www.springframework.org/schema/aop" 
	xmlns:tx="http://www.springframework.org/schema/tx"
	xsi:schemaLocation="http://www.springframework.org/schema/beans
         http://www.springframework.org/schema/beans/spring-beans-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://www.springframework.org/schema/aop 
         http://www.springframework.org/schema/aop/spring-aop-3.0.xsd ">

<!-- ==========================annotation写 @Component,service 扫描的包====================================== -->
	<context:annotation-config />
	<context:component-scan base-package="com.flex"></context:component-scan>
	
<!-- ========================================连接池配置==================================== -->
	<bean
		class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
		<property name="locations" value="classpath:jdbc.properties" />
	</bean>
	<bean id="dataSource" destroy-method="close"
		class="org.apache.commons.dbcp.BasicDataSource">
		<property name="driverClassName" value="${jdbc.driverClassName}" />
		<property name="url" value="${jdbc.url}" />
		<property name="username" value="${jdbc.username}" />
		<property name="password" value="${jdbc.password}" />
		<property name="maxActive" value="${jdbc.maxActive}" />
		<property name="maxIdle" value="${jdbc.maxIdle}" />
		<property name="maxWait" value="${jdbc.maxWait}" />
		<property name="defaultAutoCommit" value="${jdbc.defaultAutoCommit}" />
		<property name="poolPreparedStatements" value="${jdbc.poolPreparedStatements}" />
	</bean>

<!--=============================== 配置SessionFactory===================================== -->
	<bean id="sessionFactory"
		class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
		<property name="dataSource" ref="dataSource" />

		<property name="packagesToScan">
			<list>
				<value>com.agoni.OA.model</value>
			</list>
		</property>

		<property name="hibernateProperties">
			<props>
				<prop key="hibernate.dialect"> org.hibernate.dialect.MySQLInnoDBDialect</prop>
				<prop key="hibernate.show_sql">true</prop>
				<prop key="hibernate.format_sql">true</prop>
				<prop key="hibernate.hbm2ddl.auto">update</prop>
				<!--<prop key="hibernate.current_session_context_class">thread</prop> -->
			</props>
		</property>
	</bean>
	
	<!--==============================配置事务管理器以及切面========================================= -->
	<bean id="hibernateTemplate" class="org.springframework.orm.hibernate3.HibernateTemplate">
		<property name="sessionFactory" ref="sessionFactory"></property>
	</bean>

	<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="find*" read-only="true"  propagation="REQUIRED"/>
			<tx:method name="serach*"  propagation="REQUIRED"/>
			<tx:method name="add*"  propagation="REQUIRED"/>
			<tx:method name="update*" propagation="REQUIRED"/>
			<tx:method name="modify*"  propagation="REQUIRED"/>
			<tx:method name="delete*"  propagation="REQUIRED"/>
			<tx:method name="*" propagation="REQUIRED" rollback-for="Throwable.class"  />
		</tx:attributes>
	</tx:advice>
	
	<aop:config>
		<aop:pointcut expression="execution(public * com.flexmail.service..*.*(..))"
			id="servicePointcut" />
		<aop:advisor pointcut-ref="servicePointcut" advice-ref="txAdvice" />
	</aop:config>
       
</beans>



3.我在web.xml同级目录下新建一个 springMvc.xml,用来管理flex的bean
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:flex="http://www.springframework.org/schema/flex" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="  
               http://www.springframework.org/schema/beans  
               http://www.springframework.org/schema/beans/spring-beans-3.0.xsd  
               http://www.springframework.org/schema/flex  
               http://www.springframework.org/schema/flex/spring-flex-1.0.xsd">

	
	
	<bean id="javaAdapter"
		class="org.springframework.flex.core.ManageableComponentFactoryBean">
		<constructor-arg
			value="flex.messaging.services.remoting.adapters.JavaAdapter" />
	</bean>
	
	<!--注解方式注入bean-->
    <flex:message-broker >
	   <flex:remoting-service default-channels="my-amf" />
	</flex:message-broker>

</beans>  




4.web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
	xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
	version="2.5">

	<display-name>BlazeDS</display-name>
	<description>BlazeDS Application</description>

	<!-- =========================== 错误页面 配置====================================== -->

	<welcome-file-list>
		<welcome-file>index.html</welcome-file>
		<welcome-file>index.htm</welcome-file>
	</welcome-file-list>

	<error-page>
		<error-code>404</error-code>
		<location>/404.jsp</location>
	</error-page>
	<error-page>
		<error-code>500</error-code>
		<location>/error.jsp</location>
	</error-page>


	<!-- ============================spring 配置====================================== -->
	<context-param id="spring">
		<param-name>contextConfigLocation</param-name>
		<param-value>classpath:beans.xml</param-value>
	</context-param>
	
	<listener>
		<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
	</listener>


	<!-- ============================hibernate 配置====================================== -->
	<filter>
		<filter-name>openSessionInView</filter-name>
		<filter-class>org.springframework.orm.hibernate3.support.OpenSessionInViewFilter</filter-class>
		<init-param>
			<param-name>singleSession</param-name>
			<param-value>true</param-value>
		</init-param>
	</filter>
	<filter-mapping>
		<filter-name>openSessionInView</filter-name>
		<url-pattern>/*</url-pattern>
	</filter-mapping>

	<!-- ============================编码 配置====================================== -->
	<filter>
		<filter-name>encodingFilter</filter-name>
		<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
		<init-param>
			<param-name>encoding</param-name>
			<param-value>UTF-8</param-value>
		</init-param>
	</filter>
	<filter-mapping>
		<filter-name>encodingFilter</filter-name>
		<url-pattern>/*</url-pattern>
	</filter-mapping>




	<!-- ============================flex的所有 配置====================================== -->
	<!-- Http Flex Session attribute and binding listener support -->
	<listener>
		<listener-class>flex.messaging.HttpFlexSession</listener-class>
	</listener>


	<servlet>
		<servlet-name>MessageBrokerServle</servlet-name>
		<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
		
		<init-param>
			 <param-name>contextConfigLocation</param-name>
		     <param-value>/WEB-INF/springMvc.xml</param-value>
		</init-param>
		<load-on-startup>1</load-on-startup>
	</servlet>

	<servlet-mapping>
		<servlet-name>MessageBrokerServle</servlet-name>
		<url-pattern>/spring/*</url-pattern>
	</servlet-mapping>


</web-app>



5.WEB-INF/flex/services-config.xml
<?xml version="1.0" encoding="UTF-8"?>
<services-config>

    <services>
        <service-include file-path="proxy-config.xml" />
        <service-include file-path="messaging-config.xml" />  
        <default-channels>
	           <channel ref="my-amf"/>
	     </default-channels>
    </services>

    <security>
        <login-command class="flex.messaging.security.TomcatLoginCommand" server="Tomcat"/>
        <!-- Uncomment the correct app server
        <login-command class="flex.messaging.security.TomcatLoginCommand" server="JBoss">
		<login-command class="flex.messaging.security.JRunLoginCommand" server="JRun"/>        
        <login-command class="flex.messaging.security.WeblogicLoginCommand" server="Weblogic"/>
        <login-command class="flex.messaging.security.WebSphereLoginCommand" server="WebSphere"/>
        -->

        <!-- 
        <security-constraint id="basic-read-access">
            <auth-method>Basic</auth-method>
            <roles>
                <role>guests</role>
                <role>accountants</role>
                <role>employees</role>
                <role>managers</role>
            </roles>
        </security-constraint>
         -->
    </security>

    <channels>

        <channel-definition id="my-amf" class="mx.messaging.channels.AMFChannel">
            <endpoint url="http://127.0.0.1:8080/shf/spring/messagebroker/amf" class="flex.messaging.endpoints.AMFEndpoint"/>
            <properties>
                <polling-enabled>false</polling-enabled>
            </properties>
        </channel-definition>

        <channel-definition id="my-secure-amf" class="mx.messaging.channels.SecureAMFChannel">
            <endpoint url="https://{server.name}:{server.port}/{context.root}/spring/messagebroker/amfsecure" class="flex.messaging.endpoints.SecureAMFEndpoint"/>
            <properties>
                <add-no-cache-headers>false</add-no-cache-headers>
            </properties>
        </channel-definition>

        <channel-definition id="my-polling-amf" class="mx.messaging.channels.AMFChannel">
            <endpoint url="http://{server.name}:{server.port}/{context.root}/spring/messagebroker/amfChannel" class="flex.messaging.endpoints.AMFEndpoint"/>
            <properties>
                 <polling-enabled>true</polling-enabled>   
                <polling-interval-seconds>4</polling-interval-seconds>
            </properties>
        </channel-definition>

        <!--
        <channel-definition id="my-http" class="mx.messaging.channels.HTTPChannel">
            <endpoint url="http://{server.name}:{server.port}/{context.root}/messagebroker/http" class="flex.messaging.endpoints.HTTPEndpoint"/>
        </channel-definition>

        <channel-definition id="my-secure-http" class="mx.messaging.channels.SecureHTTPChannel">
            <endpoint url="https://{server.name}:{server.port}/{context.root}/messagebroker/httpsecure" class="flex.messaging.endpoints.SecureHTTPEndpoint"/>
            <properties>
                <add-no-cache-headers>false</add-no-cache-headers>
            </properties>
        </channel-definition>
        -->
    </channels>

    <logging>
        <target class="flex.messaging.log.ConsoleTarget" level="Error">
            <properties>
                <prefix>[BlazeDS] </prefix>
                <includeDate>false</includeDate>
                <includeTime>false</includeTime>
                <includeLevel>false</includeLevel>
                <includeCategory>false</includeCategory>
            </properties>
            <filters>
                <pattern>Endpoint.*</pattern>
                <pattern>Service.*</pattern>
                <pattern>Configuration</pattern>
            </filters>
        </target>
    </logging>

    <system>
        <redeploy>
            <enabled>false</enabled>
            <!-- 
            <watch-interval>20</watch-interval>
            <watch-file>{context.root}/WEB-INF/flex/services-config.xml</watch-file>
            <watch-file>{context.root}/WEB-INF/flex/proxy-config.xml</watch-file>
            <watch-file>{context.root}/WEB-INF/flex/remoting-config.xml</watch-file>
            <watch-file>{context.root}/WEB-INF/flex/messaging-config.xml</watch-file>
            <watch-file>{context.root}/WEB-INF/flex/data-management-config.xml</watch-file>
            <touch-file>{context.root}/WEB-INF/web.xml</touch-file>
             -->
        </redeploy>
    </system>

</services-config>


7.还有个proxy-config.xml,messaging-config.xml两个配置文件都是直接从blazeds解压出来的。至于remoting-config.xml就不需要了。

8.写一个测试类:hibernate没有测试,因为部署的时候就会更新表,应该没问题
注意的是这个测试类要在 bean.xml 扫描的包下面,当然子包也可以。
import org.springframework.flex.remoting.RemotingDestination;
import org.springframework.stereotype.Service;

@Service("myService")
@RemotingDestination(channels={"my-amf"})
public class MyService {

	public String sayHello(String name) {
	     System.out.println(name);
	     return "hello "+name;
	}
}




然后flex调用:

		<!--flex 与普通java类通信-->
		<s:RemoteObject id="say" destination="myService"
						endpoint="http://127.0.0.1:8080/shf/spring/messagebroker/amf"  result="resultHandler(event)"   
						fault="faultHandler(event)">
		</s:RemoteObject>


你可能感兴趣的:(Flex)