SpringJava配置-自动连线问题

SpringJava配置-自动连线问题,spring,spring-mvc,Spring,Spring Mvc,你好 我尝试使用java配置方法对Spring Autowire功能进行一个简单的测试,但我得到了如下错误,请任何人对此进行解释,谢谢 错误: Error creating bean with name 'mycom.MyComTest': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationExcepti

你好

我尝试使用java配置方法对Spring Autowire功能进行一个简单的测试,但我得到了如下错误,请任何人对此进行解释,谢谢

错误:

  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)}    
音频播放器接口:

package mycom.dao;

  public interface AudioPlayer {
  void play();
}
CDPlayer类:

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-定义bean

 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-定义应用程序上下文:包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;
    } 
   }
测试用例: 包装真菌

            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();
                }
            }

我尝试将其更改为@Bean,但没有成功。然后您可以使用此链接,该链接提供了有关如何自动连接接口的答案:好的,您需要更改的第一件事是在MyComTest中,您正在调用AudioPlayer接口的对象,但这不是实现接口的方法。所以改变:公共类MyComTest{to:public类MyComTest实现AudioPlayerHi Smogers,基本上我想在CDPlayer类下测试play方法,为什么我们需要在MyComTest类中实现接口?然后我必须在测试用例中重写play方法?我尝试将其更改为@Bean,但没有运气,然后你可以使用这个链接,它提供了关于ho的答案w到autowire接口:好的,您需要更改的第一件事是在MyComTest中,您正在调用AudioPlayer接口的对象,但这不是实现接口的方法。因此更改:公共类MyComTest{to:public类MyComTest实现AudioPlayerHi Smogers,基本上我想在CDPlayer类下测试play方法,为什么我们需要在MyComTest类中实现接口?然后我必须在测试用例中重写play方法?我尝试将其更改为@Bean,但没有运气,然后你可以使用这个链接,它提供了关于ho的答案w到autowire接口:好的,您需要更改的第一件事是在MyComTest中,您正在调用AudioPlayer接口的对象,但这不是实现接口的方法。因此更改:公共类MyComTest{to:public类mycmtest实现AudioPlayerHi Smogers,基本上我想在CDPlayer类下测试play方法,为什么我们需要在mycmtest类中实现接口?那么我必须在测试用例中重写play方法?