1

I'm trying to reuse our Spring-JDBC based DAO classes and code in a project which has a traditional Java (controller) servlet (not Spring's Dispatcher servlet). So as shown below, I tried to launch the application-config.xml manually using ClassPathXmlApplicationContext. However, I get the error shown further below.

private static final String CONFIG_PATH = "classpath*:application-config.xml";

private signupDao SignupDao;


ApplicationContext context = new ClassPathXmlApplicationContext(CONFIG_PATH);

signupDao = context.getBean(SignupDao.class);

org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [com.abc.dao.SignupDao] is defined at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBean(DefaultListableBeanFactory.java:371) at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBean(DefaultListableBeanFactory.java:331) at org.springframework.context.support.AbstractApplicationContext.getBean(AbstractApplicationContext.java:968)

Earlier, I did not add the following entry to application-config.xml but even after adding it, I still get the same error as above ("No qualifying bean of type is defined").

<bean id="signupDao" class="com.abc.dao.SignupDao"></bean>

Any ideas?

The controller servlet is not my own, I get it from a third party, I'm only trying to extend their handlers which is where I'm trying to use Spring autowiring, JDBC etc.

EDIT:

The only bean I'm able to load is context.getBean(MessageSource.class), none of my beans can be loaded. getBeanDefinitionNames is returning empty array when I tried earlier. Do you think this is a classpath issue?

2
  • Try removing the * from your CONFIG_PATH
    – Steve C
    Commented Feb 25, 2016 at 13:24
  • I get this error if I do that: Caused by: java.io.FileNotFoundException: class path resource [application-config.xml] cannot be opened because it does not exist Commented Feb 25, 2016 at 13:32

2 Answers 2

2
  1. Remove the classpath*: portion from the CONFIG_PATH because ClassPathXmlApplicationContext already looks in the class path for the file (look at it's name);

  2. Ensure that your application-config.xml file is in the root of your web application's WEB-INF/classes directory, otherwise it will not be in the classpath (this assumes that the file is not packaged in another jar).

5
  • Thanks much, this worked, I was using an example from another project for classpath*:application-config.xml, did not think that would be the problem. I got past the initial error and now stuck at: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.springframework.validation.beanvalidation.MethodValidationPostProcessor#0' defined in class path resource [application-config.xml]: Invocation of init method failed; nested exception is java.lang.NoClassDefFoundError: Could not initialize class org.hibernate.validator.internal.engine.ConfigurationImpl Commented Feb 25, 2016 at 15:10
  • Maybe an issue with missing jar for this bean: <bean class="org.springframework.validation.beanvalidation.MethodValidationPostProcessor"></bean> Commented Feb 25, 2016 at 15:14
  • Hi, do you know if there's a way to avoid calling new ClassPathXmlApplicationContext("application-config.xml") in each of my handlers and I don't know which handlers gets called the first time. Is there a huge overhead to loading application-config.xml at multiple places in your application? Thanks for any ideas. Commented Feb 29, 2016 at 18:46
  • You could use the normal org.springframework.web.context.ContextLoaderListener configured in your web.xml and then grab the ApplicationContext containing your Spring beans from the ServletContext in your handlers. See the javadoc for org.springframework.web.context.ContextLoader
    – Steve C
    Commented Mar 1, 2016 at 12:43
  • But, I'm not in a traditional Spring MVC environment, I tried this and it was not reading my application-config.xml Commented Mar 1, 2016 at 15:42
2

To narrow down the problem make sure that the ApplicationContext you're creating is successfully created.

This should return all of the beans' names from the given application context.

 context.getBeanDefinitionNames();

Update

If the ApplicationContext does not list any beans from your xml config, that might be an indication that the resource file is not accessible by the ClassPathXmlApplicationContext.

One way to check is to do something like this:

ClassPathXmlApplicationContext.class.getClassLoader().getResourceAsStream("application-config.xml")

This should return a valid not null stream in case the resource is accessible.

Note the format of the resource name. It should not include any prefixes like classpath: or classpath*:

3
  • The only bean I'm able to load is context.getBean(MessageSource.class), none of my beans can be loaded. getBeanDefinitionNames is returning empty array when I tried earlier. Do you think this is a classpath issue? Commented Feb 25, 2016 at 12:48
  • That indicates that no beans have been read from classpath resource (most probably the resource cannot be located with the specified name). Check that the resource is available to ClassPathXmlApplicationContext Commented Feb 25, 2016 at 13:02
  • If I change classpath*:application-config.xml to just application-config.xml, I get an error saying unable to locate application-config.xml, so I think it's able to locate application-config.xml correctly... Commented Feb 25, 2016 at 13:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.