为什么在控制器或服务中,我们可以到达延迟加载的代理对象,但不能在 AbstractUserDetailsAuthenticationProvider 的子类中访问?
Why in controllers or services, we can reach lazy loaded proxy object but not in a subclass of AbstractUserDetailsAuthenticationProvider?
我问了这个问题,但它已经关闭,我还没有得到确切的答案;
Spring @Transactional annotation is not working in the provider class which is a subclass of AbstractUserDetailsAuthenticationProvider
我读了这个答案;
Spring - @Transactional - What happens in background?
他们说了内部方法调用和外部方法调用。但这适用于任何控制器或服务。为什么不在注释为 @Component 的提供者 class 中?为什么 Spring 或 Hibernate 不能在提供程序 class 中打开会话,即使使用 @Transactional 注释也是如此?这与 spring 安全有关吗?有什么区别?
The following images shows a Conceptual view of calling a method on a
transactional proxy:
现在有了这些信息,只有当通过具有此注释方法的 class 的代理对象调用该方法时,带有 @Transactional
注释的方法才会启动事务。在您之前的问题中,专家将此调用称为外部调用。
以你的例子为例,
抽象方法AbstractUserDetailsAuthenticationProvider.retrieveUser() is called from AbstractUserDetailsAuthenticationProvider.authenticate()的实现,是一个自调用。这就是专家所说的 internal call 。另请注意,方法 authenticate()
不是 @Transactional
部分下的文档
In proxy mode (which is the default), only external method calls
coming in through the proxy are intercepted. This means that
self-invocation (in effect, a method within the target object calling
another method of the target object) does not lead to an actual
transaction at runtime even if the invoked method is marked with
@Transactional. Also, the proxy must be fully initialized to provide
the expected behavior, so you should not rely on this feature in your
initialization code (that is, @PostConstruct).
在您的提供程序 class 中用 @Component
注释,对代理的调用到达一个未用 @Transactional
注释的方法并进行自调用或内部调用用 @Transactional
注释的方法,它不像前面解释的那样工作
对于 Controller 或 Service,首先调用带有 @Transactional
注释的方法(外部调用),这会启动一个事务。该方法上下文中的代码流在事务中,所有后续方法都参与该事务,您看不到 - no Session
异常。
希望对您有所帮助
我问了这个问题,但它已经关闭,我还没有得到确切的答案; Spring @Transactional annotation is not working in the provider class which is a subclass of AbstractUserDetailsAuthenticationProvider
我读了这个答案; Spring - @Transactional - What happens in background?
他们说了内部方法调用和外部方法调用。但这适用于任何控制器或服务。为什么不在注释为 @Component 的提供者 class 中?为什么 Spring 或 Hibernate 不能在提供程序 class 中打开会话,即使使用 @Transactional 注释也是如此?这与 spring 安全有关吗?有什么区别?
The following images shows a Conceptual view of calling a method on a transactional proxy:
现在有了这些信息,只有当通过具有此注释方法的 class 的代理对象调用该方法时,带有 @Transactional
注释的方法才会启动事务。在您之前的问题中,专家将此调用称为外部调用。
以你的例子为例,
抽象方法AbstractUserDetailsAuthenticationProvider.retrieveUser() is called from AbstractUserDetailsAuthenticationProvider.authenticate()的实现,是一个自调用。这就是专家所说的 internal call 。另请注意,方法 authenticate()
不是 @Transactional
In proxy mode (which is the default), only external method calls coming in through the proxy are intercepted. This means that self-invocation (in effect, a method within the target object calling another method of the target object) does not lead to an actual transaction at runtime even if the invoked method is marked with @Transactional. Also, the proxy must be fully initialized to provide the expected behavior, so you should not rely on this feature in your initialization code (that is, @PostConstruct).
在您的提供程序 class 中用 @Component
注释,对代理的调用到达一个未用 @Transactional
注释的方法并进行自调用或内部调用用 @Transactional
注释的方法,它不像前面解释的那样工作
对于 Controller 或 Service,首先调用带有 @Transactional
注释的方法(外部调用),这会启动一个事务。该方法上下文中的代码流在事务中,所有后续方法都参与该事务,您看不到 - no Session
异常。
希望对您有所帮助