How to obtain the list of resources read by Spring Framework ApplicationContext
search cancel

How to obtain the list of resources read by Spring Framework ApplicationContext

book

Article ID: 337155

calendar_today

Updated On:

Products

VMware Spring Runtime

Issue/Introduction

This KB describes how to obtain the list of resources read by Spring Framework ApplicationContext by using Spring Framework APIs. 

Environment

Spring Framework 6.0

Resolution

This can be implemented with below code, parameter of context.getResources() can be changed from "classpath*:**/*" to something else like "classpath*:**/*.xml" if you only want to obtain particular resources. 

import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.core.io.Resource;

public class DemoApplication {
    public static void main(String[] args) throws Exception {
        AnnotationConfigApplicationContext context =
                new AnnotationConfigApplicationContext(DemoApplication.class);

        // Example: list all XML files Spring can see
           Resource[] resources = context.getResources("classpath*:**/*");

        System.out.println("=== Resources visible to ApplicationContext ===");
        for (Resource resource : resources) {
            System.out.println(resource.getURL());
        }

        context.close();
    }
}