I need to self-inject prototype bean. As I know, it is possible if bean scope="singleton", but in this case I get message from spring : "The dependencies of some of the beans in the application context form a cycle: postMan2"
My bean:
@Service
@Scope("prototype")
public class PostMan2 implements PostMans2 {
private PostMans2 postman;
@Async
public Future<String> deliverLetter(String message, int i) {
postman.test();
String res = "result!";
return new AsyncResult<String>(res);
}
@Override
public void test() {
System.out.println("Self injection example thread name="+name);
}
@PostConstruct
private void init() {
postman = ctx.getBean(PostMans2.class);
}
}
Invoking:
@Service
public class PostOffice implements PostOffices {
@Autowired
ApplicationContext ctx;
@Override
public void creatingPostmans() {
PostMans2 thr = ctx.getBean(PostMans2.class);
Future<String> fut = thr.deliverLetter("Some letter", 100);
while (!fut.isDone()) {
Thread.sleep(1000);
}
System.out.println("ending of PostMan's jobs...");
}
}
How to improve my code?