1

I'd like to create a parent/framework module and define a service that makes use of some interface methods.

The interface implementation should then be provided by the implementation project. But somehow the injection does not work. Why?

framework module:

package de.test;

    @Service
    public class BaseServiceExecutor {

        @Autowired
        private ICustomService service;

        public void run() {
            service.use();
        }
    }

    interface ICustomService {
        void use();
    }

@Configuration
public class FrameworkAppConfig {

}

implementation module:

package de.test.impl;

@Service
public class MyCustomService implements ICustomService {
    @Override
    void use() {
        //custom impl
    }
}

appContext.xml: (within implementation project)

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

@Configuration
@Import(FrameworkAppConfig.class)
@ComponentScan("de.test")
public class ImplAppConfig

Result:

Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [IcustomService] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
palacsint
  • 28,416
  • 10
  • 82
  • 109
membersound
  • 81,582
  • 193
  • 585
  • 1,120
  • Perhaps component scanning is not picking up MyCustomService because it's missing the correct package. – geoand Mar 24 '14 at 14:50
  • I have the framework in a package that is the same name as in the implementation project (for testing this). The services in my implementation project all get picked up. – membersound Mar 24 '14 at 14:52
  • The error message means that Spring cannot find the desired bean. As @geoand noted, looks like your beans are not detected by the scan. Please post the name of the package per class/interface and how are you scanning them in beans.xml (or however you called your spring xml configuration). – Luiggi Mendoza Mar 24 '14 at 14:57
  • How the implementation module being included? Are you trying to run the framework module without including the implementation module? – geoand Mar 24 '14 at 15:06
  • Not sure what can be the problem since everything seems fine from your posted code. Maybe you're loading another configuration file in your tests or in your app, or oddly, the interface you're seeking for it's not inside the packages to be scanned. – Luiggi Mendoza Mar 24 '14 at 15:10
  • How is the visibility of framework module being defined for implementation module to be seen? – Prasad Mar 24 '14 at 15:14
  • all classes are public so far... – membersound Mar 26 '14 at 07:52

2 Answers2

1

Try adding context:annotation-config to your appContext.xml.

See annotation-config vs component-scan

Community
  • 1
  • 1
0

Your implementation class MyCustomService is not public(unless there is a typo in the code provided). Make it public and spring should pick it up.

Priyesh
  • 2,041
  • 1
  • 17
  • 25