redfox26
redfox26

Reputation: 2070

Bean unknown with spring

In a spring 3.2 project, I am having problems starting an application. Some bean doesn't seem to be recognized by the system.

I get this error:

   27-May-2014 12:41:05.879 SEVERE [http-nio-8084-exec-5] org.apache.catalina.core.StandardContext.listenerStart Exception sending context initialized event to listener instance of class org.springframework.web.context.ContextLoaderListener
  org.springframework.beans.factory.BeanCreationException: Error creating bean with  name 'reportResource': Injection of autowired dependencies failed; nested exception is  org.springframework.beans.factory.BeanCreationException: Could not autowire field: private  com.prjx.domain.ReportFacade com.prjx.web.resources.ReportResource.reportFacade; nested  exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying  bean of type [com.prjx.domain.facade.ReportFacade] 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)}


@Controller
public class ReportResource
{
  @Autowired
  private UserFacade userFacade;

  @Autowired
  private ReportFacade reportFacade;
...
}


@Component
public interface ReportFacade{
    ...
}

 public class ReportFacadeImpl implements ReportFacade
{
   ...
}

in my application-context.xml, i have

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

How can I resolve this?

Upvotes: 0

Views: 798

Answers (2)

Karibasappa G C
Karibasappa G C

Reputation: 2732

@Component
public interface ReportFacade{
    ...
}

can never inject the dependency because its an interface.

so do something like below

  public interface ReportFacade{
        ...
    }

@Component
  public class ReportFacadeImpl implements ReportFacade{
        ...
    }

then

@Autowired
private ReportFacade reportFacade;

will inject its implementor ReportFacadeImpl .

make sure componentscan inside spring config file has the package entry for interface and class correctly.

Upvotes: 2

JB Nizet
JB Nizet

Reputation: 692101

You haven't defined an implementation for your ReportFacade interface.

Spring is no magic. It can't read your brain to know what a bean should do.

So you need to create an implementation of the ReportFacade interface, put it in a package scanned by Spring, and annotate this implementation with @Component. The interface itself shouldn't have the @Component annotation.

Upvotes: 1

Related Questions