service 的自调用中this与AOP代理的区别

本文探讨了在Spring框架中,Service层方法的自调用时,直接使用`this.b()`与通过AOP代理`((IService) AopContext.currentProxy()).b()`调用`b()`方法在事务处理上的区别。`this.b()`调用不会触发`b()`方法上的事务增强,而AOP代理方式则可以创建新的事务进行处理。在自调用时,事务只对原始调用方法生效,而在使用AOP代理时,能够正确地管理和切换事务。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

使用ssm过程中,遇到了service自调的问题,于是研究了this与AOP代理两种方法。

测试类:

public interface IService {
	void a();
	void b();
}




@Service("service")
public class ServiceImpl implements IService{

	@Override
	@Transactional(propagation=Propagation.REQUIRED)
	public void a() {

     // this.b();

        ((IService) AopContext.currentProxy()).b();
	}

	@Override
	@Transactional(propagation=Propagation.REQUIRES_NEW)
	public void b() {  }		
}

this.b()的方式:

如果 a 方法使用 this.b( ) 的方式调用方法,b 事务是不会增强的,Spring 文档中说过,只有外部调动才会增强, this 指的是当前对象,而不是 proxy 对象,所以事务不会增强。这时候 b 方法上的                                                                         @Transactional(propagation = Propagation.REQUIRES_NEW) 不起作用,也就是说这时候 b 方法与 a 方法的事务定义是一样的,这一点可以从日志中看出:

Returning cached instance of singleton bean 'service'
Adding transactional method 'ServiceImpl.a' with attribute: PROPAGATION_REQUIRED,ISOLATION_DEFAULT; ''
Returning cached instance of singleton bean 'transactionManager'

//创建新事务(a事务)
Creating new transaction with name [com.llangzh.services.impls.ServiceImpl.a]: PROPAGATION_REQUIRED,ISOLATION_DEFAULT; ''


incremented pending_acquires: 1
incremented pending_acquires: 2
incremented pending_acquires: 3
com.mchange.v2.resourcepool.BasicResourcePool@5b1669c0 config: [start -> 3; min -> 3; max -> 15; inc -> 3; num_acq_attempts -> 30; acq_attempt_delay -> 1000; check_idle_resources_delay -> 0; mox_resource_age -> 0; max_idle_time -> 0; excess_max_idle_time -> 0; destroy_unreturned_resc_time -> 0; expiration_enforcement_delay -> 0; break_on_acquisition_failure -> false; debug_store_checkout_exceptions -> false]
Initializing c3p0 pool... com.mchange.v2.c3p0.ComboPooledDataSource [ acquireIncrement -> 3, acquireRetryAttempts -> 30, acquireRetryDelay -> 1000, autoCommitOnClose -> false, automaticTestTable -> null, breakAfterAcquireFailure -> false, checkoutTimeout -> 0, connectionCustomizerClassName -> null, connectionTesterClassName -> com.mchange.v2.c3p0.impl.DefaultConnectionTester, dataSourceName -> 1hge15xa2dme3vb1ky7nif|71d44a3, debugUnreturnedConnectionStackTraces -> false, description -> null, driverClass -> com.mysql.jdbc.Driver, factoryClassLocation -> null, forceIgnoreUnresolvedTransactions -> false, identityToken -> 1hge15xa2dme3vb1ky7nif|71d44a3, idleConnectionTestPeriod -> 0, initialPoolSize -> 3, jdbcUrl -> jdbc:mysql://localhost:3306/ssm, maxAdministrativeTaskTime -> 0, maxConnectionAge -> 0, maxIdleTime -> 0, maxIdleTimeExcessConnections -> 0, maxPoolSize -> 15, maxStatements -> 0, maxStatementsPerConnection -> 0, minPoolSize -> 3, numHelperThreads -> 3, numThreadsAwaitingCheckoutDefaultUser -> 0, preferredTestQuery -> null, properties -> {user=******, password=******}, propertyCycle -> 0, testConnectionOnCheckin -> false, testConnectionOnCheckout -> false, unreturnedConnectionTimeout -> 0, usesTraditionalReflectiveProxies -> false ]
acquire test -- pool size: 0; target_pool_size: 3; desired target? 1
awaitAvailable(): [unknown]
trace com.mchange.v2.resourcepool.BasicResourcePool@5b1669c0 [managed: 0, unused: 0, excluded: 0]
com.mchange.v2.c3p0.impl.C3P0PooledConnectionPool$1PooledConnectionResourcePoolManager@4360c56.acquireResource() returning. 
com.mchange.v2.c3p0.impl.C3P0PooledConnectionPool$1PooledConnectionResourcePoolManager@4360c56.acquireResource() returning. 
com.mchange.v2.c3p0.impl.C3P0PooledConnectionPool$1PooledConnectionResourcePoolManager@4360c56.acquireResource() returning. 
trace com.mchange.v2.resourcepool.BasicResourcePool@5b1669c0 [managed: 1, unused: 1, excluded: 0]
decremented pending_acquires: 2
trace com.mchange.v2.resourcepool.BasicResourcePool@5b1669c0 [managed: 2, unused: 1, excluded: 0] (e.g. com.mchange.v2.c3p0.impl.NewPooledConnection@7a21477a)
decremented pending_acquires: 1
trace com.mchange.v2.resourcepool.BasicResourcePool@5b1669c0 [managed: 3, unused: 2, excluded: 0] (e.g. com.mchange.v2.c3p0.impl.NewPooledConnection@7a21477a)
decremented pending_acquires: 0
trace com.mchange.v2.resourcepool.BasicResourcePool@5b1669c0 [managed: 3, unused: 2, excluded: 0] (e.g. com.mchange.v2.c3p0.impl.NewPooledConnection@7a21477a)

Acquired Connection [com.mchange.v2.c3p0.impl.NewProxyConnection@7d0b7e3c] for JDBC transaction
Switching JDBC Connection [com.mchange.v2.c3p0.impl.NewProxyConnection@7d0b7e3c] to manual commit

 

Initiating transaction commit   //提交事务(a事务)


Committing JDBC transaction on Connection [com.mchange.v2.c3p0.impl.NewProxyConnection@7d0b7e3c]

 

//释放资源(a事务)
Releasing JDBC Connection [com.mchange.v2.c3p0.impl.NewProxyConnection@7d0b7e3c] after transaction


Returning JDBC Connection to DataSource
trace com.mchange.v2.resourcepool.BasicResourcePool@5b1669c0 [managed: 3, unused: 2, excluded: 0] (e.g. com.mchange.v2.c3p0.impl.NewPooledConnection@7a21477a)
  

我们可以看到事务切面只为 a 创建了事务,所有操作都是对 a 进行的,并没有对 b 进行事务上的处理,这就证明了通过

this.b() 的方式不会对 b 进行事务增强。

AOP代理方式 ((IService) AopContext.currentProxy()).b():

通过 ThreadLocal 暴露 AOP 代理对象在Spring配置文件中添加配置:

        <!-- 使用AOP代理 -->
	<aop:aspectj-autoproxy expose-proxy="true" />

测试看日志:

Returning cached instance of singleton bean 'service'
Adding transactional method 'ServiceImpl.a' with attribute: PROPAGATION_REQUIRED,ISOLATION_DEFAULT; ''
Returning cached instance of singleton bean 'transactionManager'


//创建新事务(a事务)
Creating new transaction with name [com.llangzh.services.impls.ServiceImpl.a]: PROPAGATION_REQUIRED,ISOLATION_DEFAULT; ''

......

Acquired Connection [com.mchange.v2.c3p0.impl.NewProxyConnection@7d0b7e3c] for JDBC transaction
Switching JDBC Connection [com.mchange.v2.c3p0.impl.NewProxyConnection@7d0b7e3c] to manual commit


//悬浮当前事务(a事务),创建新事务(b事务)
Suspending current transaction, creating new transaction with name [com.llangzh.services.impls.ServiceImpl.b]


Acquired Connection [com.mchange.v2.c3p0.impl.NewProxyConnection@235ecd9f] for JDBC transaction
Switching JDBC Connection [com.mchange.v2.c3p0.impl.NewProxyConnection@235ecd9f] to manual commit


Initiating transaction commit   //b事务提交


Committing JDBC transaction on Connection [com.mchange.v2.c3p0.impl.NewProxyConnection@235ecd9f]
Releasing JDBC Connection [com.mchange.v2.c3p0.impl.NewProxyConnection@235ecd9f] after transaction
Returning JDBC Connection to DataSource


Resuming suspended transaction after completion of inner transaction    //内部事务(b事务)结束后,继续悬浮的事务(a事务)

 

Initiating transaction commit  //a事务提交


Committing JDBC transaction on Connection [com.mchange.v2.c3p0.impl.NewProxyConnection@7d0b7e3c]
Releasing JDBC Connection [com.mchange.v2.c3p0.impl.NewProxyConnection@7d0b7e3c] after transaction
Returning JDBC Connection to DataSource

 

通过日志可以看到事务切面为 b 创建了事务,并且在 b 事务执行过程中,a 事务一直处于悬浮状态,只有当 b 事务处理完成后,才会继续 a 事务。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值