spring中当我用自动扫描和使用annotation 自动注册bean时,现在flex或者ext要引service怎么引用?

在spring中,当我用自动扫描和使用annotation 自动注册bean时,applicationContext.xml中没有显示的标明bean的id,现在我在flex或者ext要引service怎么引用?这时应该是没法写注释吧?它的id是什么?

其实我就是想调一个servic,但又没有配置文件中明确管理,所以不知怎样去引用,有什么办法呢?

<destination id="abc">
       <properties >
           <source>??</source >
           <factory>spring</factory >
       </properties>



下面写一下我的配置,大家帮忙看看
flex主文件

Java代码 复制代码
  1. public function onResult(event:ResultEvent):void{    
  2.                 //users=ArrayCollection(event.result);    
  3.                 Alert.show("get data successfully");    
  4.             }    
  5.             public function onFault(event:FaultEvent):void{    
  6.                 Alert.show("Error"+event.fault);    
  7.             }    
  8.   
  9. 。。。。。。   
  10.   
  11.     <mx:RemoteObject id="testFlex" destination="testFlexService">   
  12.         <mx:method name="save" result="onResult(event)" fault="onFault(event)"/>    
  13.     </mx:RemoteObject>  
public function onResult(event:ResultEvent):void{ 
				//users=ArrayCollection(event.result); 
				Alert.show("get data successfully"); 
			} 
			public function onFault(event:FaultEvent):void{ 
				Alert.show("Error"+event.fault); 
			} 

。。。。。。

	<mx:RemoteObject id="testFlex" destination="testFlexService">
		<mx:method name="save" result="onResult(event)" fault="onFault(event)"/> 
	</mx:RemoteObject>


remoting-config.xml文件

Java代码 复制代码
  1. <service id="remoting-service"    
  2.     class="flex.messaging.services.RemotingService">   
  3.   
  4.     <adapters>   
  5.         <adapter-definition id="java-object" class="flex.messaging.services.remoting.adapters.JavaAdapter" default="true"/>   
  6.     </adapters>   
  7.     <destination id="testFlexService">   
  8.        <properties>   
  9.            <source>testFlexService</source>   
  10.            <factory>spring</factory>   
  11.        </properties>   
  12.     </destination>   
  13.        
  14.     <default-channels>   
  15.         <channel ref="my-amf"/>   
  16.     </default-channels>   
  17.   
  18. </service>  
<service id="remoting-service" 
    class="flex.messaging.services.RemotingService">

    <adapters>
        <adapter-definition id="java-object" class="flex.messaging.services.remoting.adapters.JavaAdapter" default="true"/>
    </adapters>
    <destination id="testFlexService">
       <properties>
           <source>testFlexService</source>
           <factory>spring</factory>
       </properties>
    </destination>
	
    <default-channels>
        <channel ref="my-amf"/>
    </default-channels>

</service>


service-config.xml文件

Java代码 复制代码
  1. <services>   
  2.         <service-include file-path="remoting-config.xml" />   
  3.         <service-include file-path="proxy-config.xml" />   
  4.         <service-include file-path="messaging-config.xml" />           
  5.     </services>   
  6.     <factories>   
  7.      <factory id="spring" class="flex.messaging.service.SpringFactory"/>   
  8.     </factories>  
<services>
        <service-include file-path="remoting-config.xml" />
        <service-include file-path="proxy-config.xml" />
        <service-include file-path="messaging-config.xml" />        
    </services>
    <factories>
	 <factory id="spring" class="flex.messaging.service.SpringFactory"/>
	</factories>


其中flex.messaging.service.SpringFactory是网上下的说是官方包
web.xml文件

Java代码 复制代码
  1. <display-name>flextest</display-name>   
  2.     <description>BlazeDS Application</description>   
  3.   
  4.     <context-param>   
  5.         <param-name>flex.class.path</param-name>   
  6.         <param-value>/WEB-INF/flex/hotfixes,/WEB-INF/flex/jars</param-value>   
  7.     </context-param>   
  8.  <!-- MessageBroker Servlet -->   
  9.     <servlet>   
  10.         <servlet-name>MessageBrokerServlet</servlet-name>   
  11.         <display-name>MessageBrokerServlet</display-name>   
  12.         <servlet-class>flex.messaging.MessageBrokerServlet</servlet-class>   
  13.         <init-param>   
  14.             <param-name>services.configuration.file</param-name>   
  15.             <param-value>/WEB-INF/flex/services-config.xml</param-value>   
  16.        </init-param>   
  17.         <load-on-startup>1</load-on-startup>   
  18.     </servlet>   
  19.   
  20.     <servlet-mapping>   
  21.         <servlet-name>MessageBrokerServlet</servlet-name>   
  22.         <url-pattern>/messagebroker/*</url-pattern>   
  23.     </servlet-mapping>  
<display-name>flextest</display-name>
    <description>BlazeDS 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>
 <!-- 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>


spring的配置文件

Java代码 复制代码
  1. <context:component-scan base-package="com.superwebchat" />   
  2. <bean id="testFlexService" class="com.superwebchat.test.servicetest.TestFlexService"></bean>  
<context:component-scan base-package="com.superwebchat" />
<bean id="testFlexService" class="com.superwebchat.test.servicetest.TestFlexService"></bean>


其实我的spring配置中是用自动扫描和annotation注释的。
由于怕service不能被flex里引用,所以加上一个显式声明<bean id="testFlexService" class="com.superwebchat.test.servicetest.TestFlexService"></bean>

java文件如下

Java代码 复制代码
  1. public class TestFlexService {   
  2.     public void TestFlexService(){   
  3.            
  4.     }   
  5.     public String save(){   
  6.         System.out.println("test flex service1 --------------");   
  7.         return "aabbcc";   
  8.     }   
  9.   
  10. }  
public class TestFlexService {
	public void TestFlexService(){
		
	}
	public String save(){
		System.out.println("test flex service1 --------------");
		return "aabbcc";
	}

}




tomcate没报错
flex错误返回提示是:

Error[RPC Fault faultString="Send failed" faultCode="Client.Error.MessageSend" faultDetail="Channel.Connect.Failed error NetConnection.Call.Failed: HTTP: Status 404: url: 'http://localhost:8080/WebContent/messagebroker/amf'"]

原来是这个url: 'http://localhost:8080/WebContent/messagebroker/amf'"]
有问题,少了项目名,加个endpoint试试

用的是struts2零配置插件convention,又出现了
WARN  [org.apache.struts2.dispatcher.Dispatcher] - Could not find action or result
There is no Action mapped for namespace /messagebroker and action name amf. - [unknown location]

除非我把

Java代码 复制代码
  1. <filter-mapping>   
  2.         <filter-name>struts2CleanupFilter</filter-name>   
  3.         <url-pattern>*</url-pattern>   
  4.         <dispatcher>REQUEST</dispatcher>   
  5.         <dispatcher>FORWARD</dispatcher>   
  6.     </filter-mapping>  
<filter-mapping>
		<filter-name>struts2CleanupFilter</filter-name>
		<url-pattern>*</url-pattern>
		<dispatcher>REQUEST</dispatcher>
		<dispatcher>FORWARD</dispatcher>
	</filter-mapping>


改为

Java代码 复制代码
  1. <filter-mapping>   
  2.         <filter-name>struts2CleanupFilter</filter-name>   
  3.         <url-pattern>*.action</url-pattern>   
  4.         <dispatcher>REQUEST</dispatcher>   
  5.         <dispatcher>FORWARD</dispatcher>   
  6.     </filter-mapping>  
<filter-mapping>
		<filter-name>struts2CleanupFilter</filter-name>
		<url-pattern>*.action</url-pattern>
		<dispatcher>REQUEST</dispatcher>
		<dispatcher>FORWARD</dispatcher>
	</filter-mapping>


但这样的话,我直接访问.action外的页面时就报The Struts dispatcher cannot be found.  This is usually caused by using Struts tags without the associated filter
错,好像是在一些用到struts标签的页面报错。

------------------------------------------------------------------------------------------------------------------
问题补充:
其实和action中type=redirect也有关。没事了,有struts2标签的地方别type=redirect就行了


该问题已经关闭: 问题已解决,如上所述

 

也可以调action,传对象参数

flex中与java对象对应的as对象

package com.Model
{
 /**
  *
  * @author Administrator
  * 与java的pojo对应消息对象
  */ 
 
 [RemoteClass(alias="com.superwebchat.entity.entities.Meetingrecord")]
 public class javaMessage
 {
  public var ids:Number;
  //发言者
   public var nickname:String;
  
  //发言内容
  public var msg:String;
  
  //发言时间
  public var chattime:Date;
  
。。。。。。  
  
  public function getIds():Number {
   return ids;
  }
  public function setIds(ids:Number):void {
   this.ids = ids;
  }
  
  public function getNickname():String{
   return nickname;
  }
  public function setNickname(nickname:String ):void {
   this.nickname = nickname;
  }
  
  public function getMsg():String{
   return msg;
  }
  public function setMsg(msg:String ):void {
   this.msg = msg;
  }

  public function getChattime():Date{
   return chattime;
  }
  public function setChattime(chattime:Date ):void {
   this.chattime = chattime;
  }

 

<destination id="meetingrecordAction">
       <properties>
           <source>com.superwebchat.web.MeetingrecordAction</source>
       </properties>
    </destination>

 

这样还有问题

比如我这样配置

<destination id="meetingrecordAction">
       <properties>
           <source>meetingrecordAction</source>

           <factory>spring</factory>
       </properties>
    </destination>

 

要在Action中对它进行注释,它里面的@autowired的service才会有值

@Controller("meetingrecordAction")//默认就是首字母小写的类名,加上controller就会被spring管理,被注入service,flex就可以调用

 

但是发现在我传了一个as对象进来,service.saveOrUpdate()方法执行的竟然是update,但我是要它新增一条记录的,

为什么会是更新呢,我的对象的id是没有设值的,打印出来看也是0,它怎么就执行了update语句,也不是insert语句呢?

 

 

情况是这样的
我用flex remoting object
sping管理的方式传了一个对象到
action的一个方法save(Meetingrecord mr)中
在这个方法中我想实现新增一条记录的功能
在action加了@Controll注释,所以在flex才能调到它,并且里面的service对象才有值。
hibernate并没有保存这个mr对象
控制台打印出的是update语句
我dao中save方法是getCurrentSession().saveOrUpdate(obj);
为什么是update语句而不是insert语句的?
而且查看数据库也并没有那一条数据被update过
service是有@Transaction注释的,不用flex的话都能运行正确的。
另外,我把saveOrUpdate()方法改成了save()方法
控制台报出下面的错误:
[quote]
Hibernate:
    select
        hibernate_sequence.nextval
    from
        dual
Hibernate:
    insert
    into
        meetingrecord
        (acceptnickname, chattime, fontcolor, fontsize, fontstyle, fonttextdecoration, fontweight, isprivatetalk, msg, nickname, ids)
    values
        (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
2010-05-27 11:38:29,750 [http-8080-11] WARN  [org.hibernate.util.JDBCExceptionReporter] - SQL Error: 1, SQLState: 23000
2010-05-27 11:38:29,750 [http-8080-11] ERROR [org.hibernate.util.JDBCExceptionReporter] - ORA-00001: 违反唯一约束条件 (ROOT.MRIDS_PRIMARY)

2010-05-27 11:38:29,750 [http-8080-11] WARN  [org.hibernate.util.JDBCExceptionReporter] - SQL Error: 1, SQLState: 23000
2010-05-27 11:38:29,750 [http-8080-11] ERROR [org.hibernate.util.JDBCExceptionReporter] - ORA-00001: 违反唯一约束条件 (ROOT.MRIDS_PRIMARY)

2010-05-27 11:38:29,750 [http-8080-11] ERROR [org.hibernate.event.def.AbstractFlushingEventListener] - Could not synchronize database state with session
org.hibernate.exception.ConstraintViolationException: Could not execute JDBC batch update
 at org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:94)
 at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:66)
 at org.hibernate.jdbc.AbstractBatcher.executeBatch(AbstractBatcher.java:275)
 at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:266)
 at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:167)
 at org.hibernate.event.def.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:321)
 at org.hibernate.event.def.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:50)
 at org.hibernate.impl.SessionImpl.flush(SessionImpl.java:1028)
 at org.hibernate.impl.SessionImpl.managedFlush(SessionImpl.java:366)
 at org.hibernate.transaction.JDBCTransaction.commit(JDBCTransaction.java:137)
 at org.springframework.orm.hibernate3.HibernateTransactionManager.doCommit(HibernateTransactionManager.java:655)
 at org.springframework.transaction.support.AbstractPlatformTransactionManager.processCommit(AbstractPlatformTransactionManager.java:732)
 at org.springframework.transaction.support.AbstractPlatformTransactionManager.commit(AbstractPlatformTransactionManager.java:701)
 at org.springframework.transaction.interceptor.TransactionAspectSupport.commitTransactionAfterReturning(TransactionAspectSupport.java:321)
 at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:116)
 at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:171)
 at org.springframework.aop.framework.Cglib2AopProxy$DynamicAdvisedInterceptor.intercept(Cglib2AopProxy.java:635)
 at com.superwebchat.service.MeetingrecordManager$$EnhancerByCGLIB$$247fc532.saveOrUpdateMeetingrecord(<generated>)
 at com.superwebchat.web.MeetingrecordAction.save(MeetingrecordAction.java:59)
 at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
 at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
 at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
 at java.lang.reflect.Method.invoke(Method.java:597)
 at flex.messaging.services.remoting.adapters.JavaAdapter.invoke(JavaAdapter.java:421)
 at flex.messaging.services.RemotingService.serviceMessage(RemotingService.java:183)
 at flex.messaging.MessageBroker.routeMessageToService(MessageBroker.java:1503)
 at flex.messaging.endpoints.AbstractEndpoint.serviceMessage(AbstractEndpoint.java:884)
 at flex.messaging.endpoints.amf.MessageBrokerFilter.invoke(MessageBrokerFilter.java:121)
 at flex.messaging.endpoints.amf.LegacyFilter.invoke(LegacyFilter.java:158)
 at flex.messaging.endpoints.amf.SessionFilter.invoke(SessionFilter.java:44)
 at flex.messaging.endpoints.amf.BatchProcessFilter.invoke(BatchProcessFilter.java:67)
 at flex.messaging.endpoints.amf.SerializationFilter.invoke(SerializationFilter.java:146)
 at flex.messaging.endpoints.BaseHTTPEndpoint.service(BaseHTTPEndpoint.java:278)
 at flex.messaging.MessageBrokerServlet.service(MessageBrokerServlet.java:322)
 at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
 at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
 at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
 at org.springframework.orm.hibernate3.support.OpenSessionInViewFilter.doFilterInternal(OpenSessionInViewFilter.java:198)
 at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:76)
 at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
 at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
 at org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:96)
 at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:76)
 at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
 at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
 at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233)
 at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)
 at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:128)
 at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
 at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
 at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:286)
 at org.apache.coyote.http11.Http11AprProcessor.process(Http11AprProcessor.java:857)
 at org.apache.coyote.http11.Http11AprProtocol$Http11ConnectionHandler.process(Http11AprProtocol.java:565)
 at org.apache.tomcat.util.net.AprEndpoint$Worker.run(AprEndpoint.java:1509)
 at java.lang.Thread.run(Thread.java:619)
Caused by: java.sql.BatchUpdateException: ORA-00001: 违反唯一约束条件 (ROOT.MRIDS_PRIMARY)

 at oracle.jdbc.driver.DatabaseError.throwBatchUpdateException(DatabaseError.java:342)
 at oracle.jdbc.driver.OraclePreparedStatement.executeBatch(OraclePreparedStatement.java:10656)
 at com.mchange.v2.c3p0.impl.NewProxyPreparedStatement.executeBatch(NewProxyPreparedStatement.java:1723)
 at org.hibernate.jdbc.BatchingBatcher.doExecuteBatch(BatchingBatcher.java:70)
 at org.hibernate.jdbc.AbstractBatcher.executeBatch(AbstractBatcher.java:268)
 ... 52 more
[/quote]

 

 

问题补充:
是的,把saveOrUpdate()方法改成save()后就可以保存了,
至于为什么会报唯一性冲突的问题是sequence的问题
//- ORA-00001: 违反唯一约束条件 (ROOT.MRIDS_PRIMARY)
//大概是注释id时对于oracle的sequence 必需加上 allocationSize = 1, initialValue = 1
//没有加上去,声明一个sequence,默认会生成一个hibernate_sequence,
//可以在oracle中的sequence里面看到
//但是每个表都用这个就会生成id重复。
//要为每一个单独注释一个sequence
//并且把表中的数据清空
@Id
    @SequenceGenerator(name = "generatorSequence", allocationSize = 1, initialValue = 1, sequenceName = "MEETINGRECORD_SEQUENCE")
    @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "generatorSequence")

但还是不知道为什么用saveOrUpdate方法的时候执行的是update语句
而不是insert语句。
id显示是0,会不会是要id等于null才对呢?我试试吧

------------------------------------------------------------------------------------------------------------------
问题补充:
可能是数据库中的数据是其它库中拷贝过来的,所以sequence也是不对的。

你可能感兴趣的:(java,spring,bean,Flex,ext)