使用自动装配

时间:2017-03-25 17:33:36

标签: java spring-boot autowired spring-bean

我有一个 AppController 类,我将 PluginManager 自动装配为:

@Controller
@RequestMapping("/app")
public class AppController {
    private final PluginManager pluginManager;

    @Autowired
    public AppController(PluginManager pluginManager) {
        this.pluginManager = pluginManager;
    }
    return "app";
}

PluginManager bean配置为:

@Configuration
public class AppConfig {

    @Bean
    public PluginManager pluginManager(@Value("${application.service.defs}") String pattern) {
        return new PluginManager(pattern);
    }
}

$ {application.service.defs} 是一个字符串,我在 application.yml 文件中。

PluginManager 对象为:

public class PluginManager {

    private final List<String> serviceDefPatterns = new ArrayList<>();
    public PluginManager(String serviceDefPattern) {
        this.serviceDefPatterns.addAll(Arrays.asList(serviceDefPattern.split(",")));
    }
}

当我以 mvn spring-boot:run 运行cmd时,发生了错误:

org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'appController' defined in file [D:\Workspace\wae-thesis\wae-thesis-application\targe
t\classes\wae\thesis\indiv\app\controller\AppController.class]: Unsatisfied dependency expressed through constructor parameter 0; nested exception is org.springframework.beans.facto
ry.BeanCreationException: Error creating bean with name 'pluginManager' defined in class path resource [wae/thesis/indiv/app/config/AppConfig.class]: Bean instantiation via factory
method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [wae.thesis.indiv.core.plugins.PluginManager]: Factory method 'pluginM
anager' threw exception; nested exception is java.lang.NullPointerException
        at org.springframework.beans.factory.support.ConstructorResolver.createArgumentArray(ConstructorResolver.java:749) ~[spring-beans-4.3.7.RELEASE.jar:4.3.7.RELEASE]
        at org.springframework.beans.factory.support.ConstructorResolver.autowireConstructor(ConstructorResolver.java:189) ~[spring-beans-4.3.7.RELEASE.jar:4.3.7.RELEASE]
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.autowireConstructor(AbstractAutowireCapableBeanFactory.java:1193) ~[spring-beans-4.3.7.RELEAS
E.jar:4.3.7.RELEASE]
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1095) ~[spring-beans-4.3.7.RELEASE
.jar:4.3.7.RELEASE]
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:513) ~[spring-beans-4.3.7.RELEASE.jar:4.
3.7.RELEASE]
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:483) ~[spring-beans-4.3.7.RELEASE.jar:4.3.
7.RELEASE]
        at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:306) ~[spring-beans-4.3.7.RELEASE.jar:4.3.7.RELEASE]
        at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230) ~[spring-beans-4.3.7.RELEASE.jar:4.3.7.RELEASE]

        at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:302) ~[spring-beans-4.3.7.RELEASE.jar:4.3.7.RELEASE]
        at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:197) ~[spring-beans-4.3.7.RELEASE.jar:4.3.7.RELEASE]
        at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:761) ~[spring-beans-4.3.7.RELEASE.jar:4.3.7.
RELEASE]
        at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:866) ~[spring-context-4.3.7.RELEASE.jar:4.3
.7.RELEASE]
        at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:542) ~[spring-context-4.3.7.RELEASE.jar:4.3.7.RELEASE]
        at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.refresh(EmbeddedWebApplicationContext.java:122) ~[spring-boot-1.5.2.RELEASE.jar:1.5.2.RELEASE]
        at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:737) ~[spring-boot-1.5.2.RELEASE.jar:1.5.2.RELEASE]
        at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:370) ~[spring-boot-1.5.2.RELEASE.jar:1.5.2.RELEASE]
        at org.springframework.boot.SpringApplication.run(SpringApplication.java:314) ~[spring-boot-1.5.2.RELEASE.jar:1.5.2.RELEASE]
        at wae.thesis.indiv.app.Application.main(Application.java:13) [classes/:na]
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_121]
        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:1.8.0_121]
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.8.0_121]
        at java.lang.reflect.Method.invoke(Method.java:498) ~[na:1.8.0_121]
        at org.springframework.boot.maven.AbstractRunMojo$LaunchRunner.run(AbstractRunMojo.java:527) [spring-boot-maven-plugin-1.5.2.RELEASE.jar:1.5.2.RELEASE]
        at java.lang.Thread.run(Thread.java:745) [na:1.8.0_121]

当我使用命令 mvn spring-boot:run 运行时,出现了问题。但是,当我使用IntelliJ IDE运行应用程序时,一切正常,问题就消失了。

任何帮助都将不胜感激。谢谢。

0 个答案:

没有答案