美好的一天,
我尝试使用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)}
AudioPlayer界面:
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 - 定义应用程序上下文: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;
}
}
测试用例: 包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();
}
}