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?

Say No To Censorship
  • 537
  • 1
  • 15
  • 32

2 Answers2

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*:

WeMakeSoftware
  • 9,039
  • 5
  • 34
  • 52
  • 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? – Say No To Censorship Feb 25 '16 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` – WeMakeSoftware Feb 25 '16 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... – Say No To Censorship Feb 25 '16 at 13:06
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).

Steve C
  • 18,876
  • 5
  • 34
  • 37
  • 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` – Say No To Censorship Feb 25 '16 at 15:10
  • Maybe an issue with missing jar for this bean: – Say No To Censorship Feb 25 '16 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. – Say No To Censorship Feb 29 '16 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](http://docs.spring.io/spring/docs/3.0.x/javadoc-api/org/springframework/web/context/ContextLoader.html) – Steve C Mar 01 '16 at 12:43
  • But, I'm not in a traditional Spring MVC environment, I tried [this](http://stackoverflow.com/a/11014796/1119716) and it was not reading my `application-config.xml` – Say No To Censorship Mar 01 '16 at 15:42