使用@Autowired时,使用名称创建bean时出错

时间:2017-03-22 09:36:47

标签: 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.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)

我的SSLoginSuccess

@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

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

2 个答案:

答案 0 :(得分:0)

  1. 您无法自动装配抽象类

  2. 该类需要使用@Component进行元注释,以便通过组件扫描进行拾取,以便自动装配。

答案 1 :(得分:0)

您在该ServiceConfig上缺少@Component注释,并且spring找不到要映射到您的自动装配属性的任何类。另外,电线领域不是好的做法,你可以更好的电线设定器。(如果你想到电线弹簧现场检查会告诉你弹簧开发人员不建议接线领域。)