Java 使用@Autowired时创建名为的bean时出错
我的登录成功Java 使用@Autowired时创建名为的bean时出错,java,spring,spring-mvc,selenium-webdriver,Java,Spring,Spring Mvc,Selenium Webdriver,我的登录成功 [2017-03-22 15:01:56:301] [main] ERROR TestContextManager - Caught exception while allowing TestExecutionListener [org.springframework.test.context.support.DependencyInjectionTestExecutionListener@d6da883] to prepare test instance [com.fisc.te
[2017-03-22 15:01:56:301] [main] ERROR TestContextManager - Caught exception while allowing TestExecutionListener [org.springframework.test.context.support.DependencyInjectionTestExecutionListener@d6da883] to prepare test instance [com.fisc.testcases.login.SSLoginSuccess@80ec1f8]
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'com.fisc.testcases.login.SSLoginSuccess': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private com.fisc.config.ServiceConfig com.fisc.testcases.login.SSLoginSuccess.config; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [com.fisc.config.ServiceConfig] 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)}
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:334)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1214)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.autowireBeanProperties(AbstractAutowireCapableBeanFactory.java:385)
at org.springframework.test.context.support.DependencyInjectionTestExecutionListener.injectDependencies(DependencyInjectionTestExecutionListener.java:118)
at org.springframework.test.context.support.DependencyInjectionTestExecutionListener.prepareTestInstance(DependencyInjectionTestExecutionListener.java:83)
at org.springframework.test.context.TestContextManager.prepareTestInstance(TestContextManager.java:228)
at org.springframework.test.context.testng.AbstractTestNGSpringContextTests.springTestContextPrepareTestInstance(AbstractTestNGSpringContextTests.java:149)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at org.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:104)
at org.testng.internal.Invoker.invokeConfigurationMethod(Invoker.java:515)
at org.testng.internal.Invoker.invokeConfigurations(Invoker.java:217)
at org.testng.internal.Invoker.invokeConfigurations(Invoker.java:144)
at org.testng.internal.TestMethodWorker.invokeBeforeClassMethods(TestMethodWorker.java:169)
at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:108)
服务配置
@WebAppConfiguration
@Configuration
@ComponentScan("com.fisc.config")
public class SSLoginSuccess extends AbstractBase {
private static final Logger LOGGER = LoggerFactory.getLogger(SSLoginSuccess.class);
@Autowired
private ServiceConfig config;
/*@Autowired
public void SelfServiceConfig(SelfServiceConfig config){
this.config = config;
}*/
//private WebDriver d1;
@BeforeTest
private void openSSLoginPage() throws IOException {
// Launch the Internet Explorer and get the SS Login page
//d1 = new InternetExplorerDriver();
driver.get(GlobalConstant.SELFSERVICE_URL);
}
@AfterTest
private void closeBrowser() throws IOException {
// Launch the Internet Explorer and get the SS Login page
if (driver != null) {
driver.close();
driver = null;
}
}
/**
* Verify Successful Login with correct user id and password
*
*/
@Test
public void test_successful_login() throws IOException {
LOGGER.info(" Verify Successful Login with correct userid and password ");
公共抽象类ServiceConfig{
私有地图配置;
private ConversionService ConversionService=新的DefaultConversionService();
公共T获取(字符串键,类类型){
返回conversionService.convert(config.get(key),type);
}
公共字符串获取(字符串键){
返回get(key,String.class);
}
公共映射getConfig(){
返回配置;
}
公共无效设置配置(映射配置){
this.config=config;
}
}
该ServiceConfig上缺少@Component注释,spring找不到任何要映射到autowired属性的类。另外,连接字段不是一个好的做法,您可以使用更好的wire setter(如果您尝试使用idea连接spring字段,则检查会告诉您spring开发人员不建议连接字段)。您在ServiceConfig上缺少@Component注释,spring找不到任何类映射到您的autowired属性。另外,连接字段不是一个好的做法,您可以使用更好的wire setter(如果您尝试在idea中连接spring字段,检查将告诉您spring开发人员不建议连接字段。)您不能自动连接抽象类抽象类不能自动连接。你需要创建另一个类来扩展你的抽象类,然后你的autowire应该可以工作。你不能自动连接抽象类抽象类不能是autowire。您需要创建另一个扩展抽象类的类,然后autowire就可以工作了。
public abstract class ServiceConfig {
private Map<String, String> config;
private ConversionService conversionService = new DefaultConversionService();
public <T> T get(String key, Class<T> type) {
return conversionService.convert(config.get(key), type);
}
public String get(String key) {
return get(key, String.class);
}
public Map<String, String> getConfig() {
return config;
}
public void setConfig(Map<String, String> config) {
this.config = config;
}
}