Goutam Kumar Goutam Kumar - 2 years ago 91
Java Question

Spring wildcard not working

I am using maven, and included my config file under src/main/resources as "singleton-config.xml".

When I am creating context as

ApplicationContext context = new ClassPathXmlApplicationContext("classpath:singleton-config.xml");

it is working fine but when I use wildcard

ApplicationContext context = new ClassPathXmlApplicationContext("classpath:singleton-*.xml");

It is giving exception

Exception in thread "main" org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'singleton' is defined.


Answer Source

Use a wildcard after the classpath ("classpath*")

ApplicationContext context = new ClassPathXmlApplicationContext("classpath*:singleton-*.xml");


The Classpath*: portability classpath*: prefix

When constructing an XML-based application context, a location string may use the special classpath*: prefix:

ApplicationContext ctx =
    new ClassPathXmlApplicationContext("classpath*:conf/appContext.xml");

This special prefix specifies that all classpath resources that match the given name must be obtained (internally, this essentially happens via a ClassLoader.getResources(…​) call), and then merged to form the final application context definition.

See Wildcards in application context constructor resource paths for more information.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download