Giriş
Request ve Session scope web uygulamalarında kullanılır
import org.springframework.context.ConfigurableApplicationContext;
Açıklaması şöyle. ApplicationContext arayüzünden kalıtır.When you use an application context, i.e. get beans from it you only use it as an ApplicationContext but when you manage its life cycle (initialization and destruction) you use methods from ConfigurableApplicationContextconstructor
@SpringBootApplication
@EnableDiscoveryClient
public class Application {
public static void main(String[] args) {
ConfigurableApplicationContext context =
new SpringApplicationBuilder(Application.class)
.properties("spring.config.name:application,db-config")
.build().run(args);
ConfigurableEnvironment environment = context.getEnvironment();
}
}
close metodu@Service
public class MyService {
@Autowired
ApplicationContext context;
@PreDestroy
public void cleanUp() throws Exception {
((ConfigurableApplicationContext)context).close();
}
}
Örnek@SpringBootApplication
@ComponentScan(basePackages = "lk.tradeportal")
@EnableWebMvc
@ImportResource(locations = "classpath:tradeportal-servlet-config.xml")
public class TRADEPORTALStarter extends SpringBootServletInitializer {
private static ConfigurableApplicationContext context;
public static void main(String[] args) {
SpringApplication application = new SpringApplication(TRADEPORTALStarter.class);
context = application.run(args);
application.setRegisterShutdownHook(true);
}
@PreDestroy
private static void closeAppContext(){
context.close();
}
...
}
getEnvironment metoduConfigurableEnvironment environment = context.getEnvironment();
private CacheControl cacheControl = CacheControl.noStore().mustRevalidate();
protected <T> ResponseEntity<TNRestResponse<T>> createEntity(TNRestResponse<T> res) {
return ResponseEntity.ok().cacheControl(cacheControl).body(res);
}
import org.springframework.core.io.Resource;
import org.springframework.core.io.support.ResourceUtils;
getResourcePatternResolver metodu+ resources
+ elcordelaciutat
- FileA.txt
- FileB.txt
+ a-dir
- c.txt
+ c-dir
- d.txt
+ b-dir
- b.txt
Şöyle yaparız. Tüm txt dosyalarına erişebiliriz.private ResourceLoader loader;
private List<String> getFiles() throws IOException {
Resource[] resources = ResourcePatternUtils
.getResourcePatternResolver(loader)
.getResources("classpath*:elcordelaciutat/*.txt");
return Arrays.stream(resources)
.map(p -> p.getFilename().toUpperCase())
.sorted()
.collect(toList());
}