Skip to content

Add "self injection" code example #33462

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -197,13 +197,60 @@ regular autowiring candidate selection and are therefore in particular never pri
the contrary, they always end up as lowest precedence.

In practice, you should use self references as a last resort only – for example, for
calling other methods on the same instance through the bean's transactional proxy. As an
alternative, consider factoring out the affected methods to a separate delegate bean in
such a scenario.
calling other methods on the same instance through the bean's transactional proxy,
as the following example shows:

[tabs]
======
Java::
+
[source,java,indent=0,subs="verbatim",role="primary"]
----
public class SimplePojo implements Pojo {

@Autowired
private Pojo self;

public void foo() {
// @Transactional on method bar() works
self.bar();
}

@Transactional
public void bar() {
// some logic...
}
}
----

Kotlin::
+
[source,kotlin,indent=0,subs="verbatim",role="secondary"]
----
class SimplePojo : Pojo {

@Autowired
private lateinit var self: Pojo

fun foo() {
// @Transactional on method bar() works
self.bar()
}

@Transactional
fun bar() {
// some logic...
}
}
----
======

Another alternative is to use `@Resource`, which may obtain a proxy back to the current
bean by its unique name.

Another alternative is to consider factoring out the affected methods to a separate delegate
bean in such a scenario.

======
[NOTE]
====
Expand Down