How can I do to invoke methods in one service each other by spring?
I have one Service, I make it manage by spring by annotation @Service, but I found In this service, methods invoke each other is not manage by spring, So some annotation in spring I use makes no sense.
@Service
class Service {
method a(){
b(); // it's not invoked by spring, actually, it's invoked in a common way
}
method b(){
}
}
It's work by using SpringContextHolder.getBean(Service.class).b();
.But I wanna to know there is a more convenient way ?
Thank you very much.
update
I found something confused. It's ok to inject itself eventually.
And I found something wrong when I use my custom annotation @LogTime
over a method, which results in the inject component is null !!!!!
for example:
// @LogTime
public Response execute(Request request) {
try {
return okHttp.client.newCall(request).execute();
} catch (IOException e) {
log.error("OkHttp wrong", e);
throw new SystemException("OkHttp wrong", e);
}
}
When I use @LogTime
annotation I created over this method, the client
component is null
!!!!!!
update finally
@Component
public class OkHttp {
private final OkHttpClient client;
private final OkHttp okHttp;
public OkHttp(OkHttpClient client, @Lazy OkHttp okHttp) {
this.client = client;
this.okHttp = okHttp;
}
}
summarize:
It's ok to inject self, but it's not ok to use okHttp.client.method()
.
the okHttp.client
is null
and client
is not null, It's ok to replace okHttp.client.method
with client.method()
directly.
the client is managed by spring, so we can achieve the same goal.