0

What I'm doing: I'm loading my Spring application external JAR into another non-spring application. I'm doing it like this:

ApplicationContext ap = new ClassPathXmlApplicationContext("classpath:/META-INF/spring/application-context.xml");
MyService myService = (MyService) ap.getBean("myBusinessService");

this is the exception that I receive:

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'myBusinessService': Injection of autowired dependencies failed; nested exception is
org.springframework.beans.factory.BeanCreationException: Could not autowire field: private com.test.domain.dao.MyDAO com.test.domain.service.impl.MyBusinessService.viaggioDAO; nested exception is
org.springframework.beans.factory.NoSuchBeanDefinitionException: No matching bean of type [com.test.domain.dao.MyDAO] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency.

Package inside those jar are:

  • com.test.domain.service for service Interfaces
  • com.test.domain.services.impl for service implementations
  • com.test.domain.dao for DAO interfaces
  • com.test.domain.dao.impl for DAO implementation

Question: Why am I receiving this error?

EDIT: more info about my app.

application-context.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:mvc="http://www.springframework.org/schema/mvc"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
                           http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
                           http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">

    <!-- Spring IoC Context -->
    <context:component-scan base-package="com.test" />

    <import resource="root-config.xml" />
    <import resource="classpath:/root-context.xml" />

    <!-- Enables the Spring MVC @Controller programming model -->
    <mvc:annotation-driven />

</beans>

MyBusinessService.java

@Service(value="myBusinessService")
public class MyBusinessService implements MyService {
    @Autowired
    private MyDAO myDAO;

    @Override
    public List<Stuff> getAllStuff() throws SQLException {
        List<Stuff> stuff = this.myDAO.findAllStuff();
        return stuff;
    }
}
Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
abierto
  • 1,447
  • 7
  • 29
  • 57
  • Could you please let us know if the `@Repository` annotation is given in DAO interface or DAO implementation? – Mithun Jan 30 '15 at 13:00
  • It's given in the DAO implementation. And the Service one is on the Service implementation too. – abierto Jan 30 '15 at 13:19
  • @abiertoCan you please share application-context.xml and com.test.domain.service.impl.MyBusinessService.java class with us. – erhun Jan 30 '15 at 14:49
  • @erhun added both files. – abierto Jan 30 '15 at 15:56
  • @abiertowhere did you scan this package "com.test.domain.dao.MyDAO"? Is there any context:component-scan at root-config.xml or root-context.xml? Becasue in application-context.xml no context:component-scan defined for com.test.domain.dao.MyDAO as i see. – erhun Jan 31 '15 at 11:02
  • @erthun Sorry, I made a bad copy/paste mistake, now I've edited my application-context. – abierto Feb 02 '15 at 08:15

1 Answers1

0

It is the visiblity problem , check whether you have listed all packages in your dispatcher-servlet.xml. Add,

<context:component-scan base-package="com.test.domain" />

to make all your package visible to your dispatcher

Also ensure that , the bean you are trying to autowire shoud have valid qualifier as @Component or @Service

If you are using the xml configuration make sure , you define the bean in the dispatcher . Before autowiring it

Santhosh
  • 8,181
  • 4
  • 29
  • 56
  • My spring jar is not a web application, but my new application will run inside a cointainer (i.e. tomcat). All my services and daos got their relative Service or Repository annotation. Even Component scan is done on my xml. what do you mean with "define the bean in the dispatcher before autowiring it"? – abierto Jan 30 '15 at 12:18