Another day another problem. Such is the life of the programmer.
So I have an interface called DataCleaner
that has 5 implementing classes.
4 of these implementing classes are designed to call a clean method on the DAO of its respective domain object. So FooCleaner calls fooDao.clean() etc etc.
The 5th cleaner is annotated @Scheduled
and designed to autowire the other 4 in and run them in a specified order (using the @Order
annotation on the other 4 implementing classes.
Interface:
public interface DataCleaner {
public void clean();
}
One of the 4 implementers (They are all essentially the same pattern, only difference is the value inside the @Order
annotation, and the different DAO's):
@Component
@Order(value = 3)
public class FooDataCleanerImpl implements DataCleaner {
@Autowired
private fooDAO fooDao;
@Override
public void clean() {
fooDao.clean();
}
}
Scheduled Implementation:
@Component
public class ScheduledDataCleaner implements DataCleaner {
protected static final Logger logger = LogManager.getLogger(ScheduledDataCleaner.class);
@Autowired
private List<DataCleaner> dataCleaners;
@Override
@Scheduled(cron="0 58 13 * * *")
public void clean() {
for(DataCleaner dataCleaner : dataCleaners){
logger.info(dataCleaner);
dataCleaner.clean();
}
}
}
To my pleasant surprise the Scheduled cleaner didn't get it's own instance added to the list. To be totally clear I am very glad that this doesn't occur, because it would make my life much harder if I had to ensure that it didn't occur, and to avoid the unresolvable recursive call.
But my question is why doesn't this implementation become a candidate for autowiring? It implements the required interface and it's in the application context. Is Spring doing something very clever in the Bean Factory to determine that it can't be autowired onto itself, or is it just something that Spring can't do to singleton instances, that would happen if it was a prototype instance?