1

Good day,

I trying a simple test on Spring Autowire feature using java config approach, but i got the error as below, could anyone please shed some light on this, thanks ahead!

Error:

  Error creating bean with name 'mycom.MyComTest': Injection of autowired    
  dependencies failed; nested exception is  
  org.springframework.beans.factory.BeanCreationException: Could not   
  autowire 
  field: private mycom.dao.AudioPlayer mycom.MyComTest.iModel; nested   
  exception is 
  org.springframework.beans.factory.NoSuchBeanDefinitionException: No  
  qualifying bean of type [mycom.dao.AudioPlayer] 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)}    

AudioPlayer Interface:

package mycom.dao;

  public interface AudioPlayer {
  void play();
}

CDPlayer class:

package mycom.dao;

import org.springframework.stereotype.Component;

@Component
public class CDPlayer implements AudioPlayer {

   @Override
   public void play() {
    System.out.println("this is playing by CD....");

 }

}

Webconfig - define beans

 package mycom.init;
 import java.util.Locale;
 import mycom.MyComTest;
 import org.springframework.context.MessageSource;
 import org.springframework.context.annotation.Bean;
 import org.springframework.context.annotation.ComponentScan;
 import org.springframework.context.annotation.Configuration;
 import org.springframework.context.support.ResourceBundleMessageSource;
 import org.springframework.web.servlet.LocaleResolver;
 import org.springframework.web.servlet.config.annotation.EnableWebMvc;
 import  
 org.springframework.web.servlet.config.annotation.InterceptorRegistry;
 import 
 org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
 import 
 org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
 import org.springframework.web.servlet.i18n.LocaleChangeInterceptor;
 import org.springframework.web.servlet.i18n.SessionLocaleResolver;
 import org.springframework.web.servlet.view.InternalResourceViewResolver;

 @Configuration
 @EnableWebMvc
 @ComponentScan(basePackages="mycom.*")
 public class WebConfig extends WebMvcConfigurerAdapter{

 @Bean
 public InternalResourceViewResolver getInternalResourceViewResolver(){
    InternalResourceViewResolver resolver = new   
    InternalResourceViewResolver();
    resolver.setPrefix("/WEB-INF/view/");
    resolver.setSuffix(".jsp");
    return resolver;
 }         
}

WebInitalizaer - define Application Context: package mycom.init;

 import javax.servlet.ServletContext;
 import javax.servlet.ServletException;
 import javax.servlet.ServletRegistration;

 import org.springframework.web.WebApplicationInitializer;
 import org.springframework.web.context.ContextLoaderListener;
 import org.springframework.web.context.WebApplicationContext;
 import       

org.springframework.web.context.support.AnnotationConfigWebApplicationContext;

 import org.springframework.web.servlet.DispatcherServlet;


 public class WebInitializer implements WebApplicationInitializer  {

 @Override
 public void onStartup(ServletContext servletContext) throws  
 ServletException {
    WebApplicationContext context = getContext();
    servletContext.addListener(new ContextLoaderListener(context));
    ServletRegistration.Dynamic dispatcher = 
    servletContext.addServlet("DispatcherServlet", new   
    DispatcherServlet(context));
    dispatcher.setLoadOnStartup(1);
    dispatcher.addMapping("/");
    dispatcher.addMapping("*.jsp"); 
    dispatcher.addMapping("*.json");
    dispatcher.addMapping("*.xml");
  }

    private AnnotationConfigWebApplicationContext getContext() {
           AnnotationConfigWebApplicationContext context = new   
           AnnotationConfigWebApplicationContext();
          context.setConfigLocation("mycom.init.WebConfig");
          return context;
    } 
   }

Test case: package mycom;

            import org.junit.Test;
            import org.junit.runner.RunWith;
            import org.springframework.beans.factory.annotation.Autowired;
            import org.springframework.test.context.ContextConfiguration;
            import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;





            import mycom.dao.AudioPlayer;
            import mycom.init.WebInitializer;


            @RunWith(SpringJUnit4ClassRunner.class)
            @ContextConfiguration(classes=WebInitializer.class)
            public class MyComTest {

                @Autowired //I tried to change @Bean here but not working
                private AudioPlayer iModel;

                @Test
                public void play(){
                    System.out.println("testing ...");
                    iModel.play();
                }
            }
  • I tried changed that to @Bean, but have no luck on it – user3299876 Jun 22 '15 at 14:30
  • then you can use this link which provides an answer on how to autowire interfaces: http://stackoverflow.com/questions/12899372/spring-why-do-we-autowire-the-interface-and-not-the-implemented-class – smoggers Jun 22 '15 at 14:42
  • ok first thing you need to change is in MyComTest you are calling an object of the AudioPlayer interface but this is not the way to implent interfaces. so change: public class MyComTest { to: public class MyComTest implements AudioPlayer – smoggers Jun 22 '15 at 14:54
  • Hi Smoggers, basically i want to test the play method under CDPlayer class, why we need to implement the interface in MyComTest class? then i have to override the play method in test case? – user3299876 Jun 22 '15 at 16:23

0 Answers0