DirectoryCleaner { @PostConstruct public void clean() throws IOException { if (temporaryDirectory != null && new File(temporaryDirectory).exists()) { Path directory = FileSystems.getDefault() .getPath(temporaryDirectory); Files.walkFileTree(directory, new CleanupFileVisitor(directory)); } } @Autowired  DirectoryCleaner(@Value("${s3.temp.dir:#{null}}") String temporaryDirectory); @PostConstruct void clean();  }
@Test public void clean() throws IOException { Path dir = Files.createTempDirectory("test"); DirectoryCleaner cleaner = new DirectoryCleaner( dir.toString()); Path file = Files.createTempFile(dir, "a", "b"); Assert.assertTrue(Files.exists(file)); cleaner.clean(); Assert.assertFalse(Files.exists(file)); }