调用webservice的方法中的方法可以互相调用吗

service中的方法可以互相调用吗? - ITeye问答
在spring中service中的每个方法都是一个事务,如果serviceA调用serviceB中的方法,这样是不是会出现事务的嵌套?如果事务A嵌套事务B,事务B执行成功,事务A执行失败,事务B会回滚吗?
这个问题困扰了我好久……
问题补充:/topic/35907
刚看到一篇文章讲了spring的事务传播属性,如果设置为PROPAGATION_REQUIRED 的话,service可以相互调用,并且只会有一个事务,不会出现事务的嵌套,那么我想知道下面的配置,怎么确定spring的事务传播属性呢?
&!-- Jpa 事务配置 --&
&bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager"&
&property name="entityManagerFactory" ref="entityManagerFactory" /&
&!-- 使用annotation定义事务 --&
&tx:annotation-driven transaction-manager="transactionManager"
proxy-target-class="true" /&
采纳的答案
事务传播行为类型
PROPAGATION_REQUIRED
如果当前没有事务,就新建一个事务,如果已经存在一个事务中,加入到这个事务中。这是最常见的选择。
PROPAGATION_SUPPORTS
支持当前事务,如果当前没有事务,就以非事务方式执行。
PROPAGATION_MANDATORY
使用当前的事务,如果当前没有事务,就抛出异常。
PROPAGATION_REQUIRES_NEW
新建事务,如果当前存在事务,把当前事务挂起。
PROPAGATION_NOT_SUPPORTED
以非事务方式执行操作,如果当前存在事务,就把当前事务挂起。
PROPAGATION_NEVER
以非事务方式执行,如果当前存在事务,则抛出异常。
PROPAGATION_NESTED
如果当前存在事务,则在嵌套事务内执行。如果当前没有事务,则执行与PROPAGATION_REQUIRED类似的操作。
可见事务是可以嵌套执行的,根据事务的一致性,应该A,B都执行成功以后才算完成,一个不成功,就回滚。
先说我的结论,事务B会回滚。
另外,我觉得写几行代码测一下应该不会很麻烦
Service层互相调用就证明你的代码结构设计严重不良。
&property name="transactionAttributes"&&
&&&&&&&&&&& &props&&
&&&&&&&&&&&&&&& &prop key="*"&PROPAGATION_REQUIRED&/prop&
&&&&&&&&&&& &/props&&
&&&&&&& &/property&
已解决问题
未解决问题本帖子已过去太久远了,不再提供回复功能。一个模块中的service层能不能相互引用? - 知乎7被浏览3559分享邀请回答21 条评论分享收藏感谢收起7701人阅读
android开发(14)
原创作品,允许转载,转载时请务必以超链接形式标明文章&&、作者信息和本声明。否则将追究法律责任。
如何在Activity中调用服务服务中的方法呢,下面我们用一个例子简单的说一下
需求:服务中有一个方法,返回值为String&内容为“张三”,现在我们通过调用服务中的方法把“张三”显示在主Activity上。
新建一个服务类MyService,继承Service
package&cn.cbd.
import&android.app.S
import&android.content.I
import&android.os.B
import&android.os.IB
public&class&MyService&extends&Service
public&void&onCreate()
super.onCreate();
public&IBinder
onBind(Intent intent)
return&new&MyBinder();
public&void&onDestroy()
super.onDestroy();
public&class&MyBinder&extends&Binder
public&String
return&getName();
在服务中自定义一个方法,我们要在activity中调用这个方法
public&String
return&&张三&;
在上面我们看到,getName()这个方法是我们在Service中定义的,现在我们要在主Activity中调用这个方法。怎么调用呢,上面MyService中有一个onBind(Intent&intent)方法&返回值类型为IBinder&activity和service就是通过这个方法来进行通信。&所以呢&我们为了方便起见,就自定义了一个类MyBinder继承Binder&,在MyBinder类中定义一个方法getname(),返回getName()的值。
Service也是android四大组件之一,所以呢,不要忘记在清单文件中对其进行配置
下面我们看Activity这一面:
package&cn.cbd.
import&android.app.A
import&ponentN
import&android.content.C
import&android.content.I
import&android.content.ServiceC
import&android.os.B
import&android.os.IB
import&android.view.V
import&android.view.View.OnClickL
import&android.widget.B
import&android.widget.TextV
public&class&ServiceTestActivity&extends&Activity
private&Button
private&TextView
private&MyServiceConnection
private&MyService.MyBinder
public&void&onCreate(Bundle
savedInstanceState)
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
btn_bindService
= (Button) findViewById(R.id.btn_bindService);
= (TextView) findViewById(R.id.tv_show);
btn_bindService.setOnClickListener(new&OnClickListener()
public&void&onClick(View
service =&new&Intent(ServiceTestActivity.this,
MyService.class);
=&new&MyServiceConnection();
bindService(service,
conn, Context.BIND_AUTO_CREATE);
自定义一个类,实现ServiceConnection接口,并重写其两个方法
@author Administrator
public&class&MyServiceConnection&implements&ServiceConnection
public&void&onServiceConnected(ComponentName
name, IBinder service)
= (MyService.MyBinder)
tv_show.setText(myBinder.getname());
public&void&onServiceDisconnected(ComponentName
protected&void&onDestroy()
unbindService(conn);
super.onDestroy();
&&相关文章推荐
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
访问:940055次
积分:7175
积分:7175
排名:第3083名
原创:19篇
转载:279篇
评论:65条
(2)(7)(1)(3)(8)(3)(1)(1)(1)(2)(12)(10)(7)(6)(2)(7)(11)(1)(1)(1)(2)(1)(39)(10)(6)(6)(5)(2)(1)(1)(17)(26)(3)(2)(12)(1)(14)(4)(13)(3)(7)(30)(6)Android中BindService方式使用的理解(转) - Hi, Sun - ITeye博客
博客分类:
最近学习了一下Android里面的Service的应用,在BindService部分小卡了一下,主要是开始没有彻底理解为什么要这么实现。
BindService和Started Service都是Service,有什么地方不一样呢:
1. Started Service中使用StartService()方法来进行方法的调用,调用者和服务之间没有联系,即使调用者退出了,服务依然在进行【onCreate()-
&onStartCommand()-&startService()-&onDestroy()】,注意其中没有onStart(),主要是被onStartCommand()方法给取代了,onStart方法不推荐使用了。
2. BindService中使用bindService()方法来绑定服务,调用者和绑定者绑在一起,调用者一旦退出服务也就终止了【onCreate()-&onBind()-&onUnbind()-&onDestroy()】。
调用者Activity:
MainAcitvity
package com.zys.
import com.zys.service.BindService.MyB
import android.R.
import android.app.A
import ponentN
import android.content.C
import android.content.I
import android.content.ServiceC
import android.os.B
import android.os.IB
import android.view.V
import android.view.View.OnClickL
import android.widget.B
public class MainActivity extends Activity {
private Button startB
private Button stopB
private boolean
/** Called when the activity is first created. */
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
flag = false;
startBtn = (Button)this.findViewById(R.id.startBtn);
stopBtn = (Button)this.findViewById(R.id.stopBtn);
startBtn.setOnClickListener(listener);
stopBtn.setOnClickListener(listener);
private OnClickListener listener = new OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
switch (v.getId()) {
case R.id.startBtn:
bindService();
case R.id.stopBtn:
private void bindService(){
Intent intent = new Intent(MainActivity.this,BindService.class);
bindService(intent, conn, Context.BIND_AUTO_CREATE);
private void unBind(){
if(flag == true){
unbindService(conn);
flag = false;
private ServiceConnection conn = new ServiceConnection() {
public void onServiceDisconnected(ComponentName name) {
// TODO Auto-generated method stub
public void onServiceConnected(ComponentName name, IBinder service) {
// TODO Auto-generated method stub
MyBinder binder = (MyBinder)
BindService bindService = binder.getService();
bindService.MyMethod();
flag = true;
服务BindService
BindService
package com.zys.
import java.io.FileD
import android.app.S
import android.content.I
import android.os.B
import android.os.IB
import android.os.II
import android.os.P
import android.os.RemoteE
import android.util.L
public class BindService extends Service {
private static final String TAG = "BindService";
public void MyMethod(){
Log.i(TAG, "BindService--&MyMethod()");
public IBinder onBind(Intent intent) {
// TODO Auto-generated method stub
return myB
public class MyBinder extends Binder{
public BindService getService(){
return BindService.this;
private MyBinder myBinder = new MyBinder();
由于Android 中的Service使用了onBind 的方法去绑定服务,返回一个Ibinder对象进行操作,而我们要获取具体的Service方法的内容的时候,我们需要Ibinder对象返回具体的Service对象才能操作,所以说具体的Service对象必须首先实现Binder对象,这个样子的话我们才能利用bindService的方法对Service进行绑定,获取Binder对象之后获取具体的Service对象,然后才获取Service中的方法等等。所以我们需要注意的是bindService的方式去绑定服务获取的必定是实现了Binder的对象,所以这是我们必须使用Binder的方式去获取Service的方式而不是直接使用Service的类,这个是Android内部实现所约束的。
方法过程如下:
Intent intent = new Intent(MainActivity.this,BindService.class)-&新建了BindService对象-&新建了MyBinder对象
-&bindService(intent, conn, Context.BIND_AUTO_CREATE);-&onBind()函数
-----传递MyBinder对象-------&onServiceConnected()
--& 通过传递的Binder对象获取刚刚和Binder对象对应的BindService 对象  --&调用Service中定义的方法。
这个其中必须通过Binder对象,因为是通过Binder对象来传递的,通过Binder对象获取Service对象,然后获取所需的服务,所以Service必须实现Binder,以便传递和使用。
------------------------------------------
奇怪的事情真多,我的service怎么就有onStart方法,难得是版本的原因了?
package com.endual.
import android.app.S
import android.content.I
import android.os.IB
public class Myservice extends Service{
public IBinder onBind(Intent intent) {
System.out.println("----------&&onBind");
public void onCreate() {
System.out.println("----------&&onCreate");
super.onCreate();
public void onDestroy() {
System.out.println("----------&&onDestroy");
super.onDestroy();
public void onRebind(Intent intent) {
System.out.println("----------&&onRebind");
super.onRebind(intent);
public void onStart(Intent intent, int startId) {
System.out.println("----------&&onStart");
super.onStart(intent, startId);
public int onStartCommand(Intent intent, int flags, int startId) {
System.out.println("----------&&onStartCommand");
return super.onStartCommand(intent, flags, startId);
public boolean onUnbind(Intent intent) {
System.out.println("----------&&onUnbind");
return super.onUnbind(intent);
浏览 40367
浏览: 1659486 次
来自: 杭州
java实现操作word中的表格内容,用插件实现的话,可以试试 ...
Maven多模块spring + springMVC + JP ...
博主还有wlsvm.zip这个压缩包吗?链接打不开了
f604a33a817e427

我要回帖

更多关于 调用webservice的方法 的文章

 

随机推荐