在实现Groovy接口的Groovy类中,@ Autowired依赖项为null

时间:2015-01-08 19:08:38

标签: spring groovy spring-boot

我正在尝试将属性类注入基于Groovy的类中,但注入的类为null。我确实有另一个属性类被注入到一个从Tomcat的Filter接口实现的类中,并且工作正常。

启动Spring Boot应用程序时,这是我的堆栈跟踪:

    Exception in thread "main" org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'stormpathAccountService' defined in file [/Users/jfitzgerald/Projects/parsezilla-api-partner/build/classes/main/com/schoolzilla/api/application/credentials/StormpathAccountService.class]: Instantiation of bean failed; nested exception is org.springframework.beans.BeanInstantiationException: Could not instantiate bean class [com.schoolzilla.api.application.credentials.StormpathAccountService]: Constructor threw exception; nested exception is java.lang.NullPointerException
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateBean(AbstractAutowireCapableBeanFactory.java:1077)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1022)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:504)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:475)
    at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:302)
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:228)
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:298)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:193)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:706)
    at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:762)
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:482)
    at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.refresh(EmbeddedWebApplicationContext.java:109)
    at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:691)
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:320)
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:952)
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:941)
    at org.springframework.boot.SpringApplication$run.call(Unknown Source)
    at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCall(CallSiteArray.java:45)
    at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:108)
    at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:120)
    at com.schoolzilla.api.Application.main(Application.groovy:18)
Caused by: org.springframework.beans.BeanInstantiationException: Could not instantiate bean class [com.schoolzilla.api.application.credentials.StormpathAccountService]: Constructor threw exception; nested exception is java.lang.NullPointerException
    at org.springframework.beans.BeanUtils.instantiateClass(BeanUtils.java:164)
    at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:89)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateBean(AbstractAutowireCapableBeanFactory.java:1070)
    ... 20 more
Caused by: java.lang.NullPointerException
    at org.codehaus.groovy.runtime.callsite.AbstractCallSite.createGroovyObjectGetPropertySite(AbstractCallSite.java:254)
    at org.codehaus.groovy.runtime.callsite.AbstractCallSite.acceptGroovyObjectGetProperty(AbstractCallSite.java:239)
    at org.codehaus.groovy.runtime.callsite.AbstractCallSite.callGroovyObjectGetProperty(AbstractCallSite.java:231)
    at com.schoolzilla.api.application.credentials.StormpathAccountService.<init>(StormpathAccountService.groovy:37)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
    at java.lang.reflect.Constructor.newInstance(Constructor.java:408)
    at org.springframework.beans.BeanUtils.instantiateClass(BeanUtils.java:148)
    ... 22 more

以下是给我提出问题的代码:

@Service
class StormpathAccountService implements AccountService {

//This is where my problem lies
    @Autowired
    StormpathProperties stormpathProperties

    private def logger = LogFactory.getLog(StormpathAccountService)

    private def path = System.getProperty("user.home") + stormpathProperties.apiKeyLocation

//more stuff here...

    }
}

正在实施Groovy接口:

interface AccountService {
    def createAccount(PartnerAccount account);
    def deleteAccount(PartnerAccount account);
    ApiKey fetchApiKey(PartnerAccount account);
}

属性类:

@Configuration
@ConfigurationProperties(prefix = "stormpath")
class StormpathProperties {
    String apiKeyLocation
    String accountUrl
}

我的application.properties文件中的属性名称:

stormpath.apiKeyLocation
stormpath.accountUrl

最后我的主要Application类:

@Configuration
@ComponentScan
@EnableAutoConfiguration
@EnableTransactionManagement
@EnableConfigurationProperties
class Application {

    static void main(String[] args) {
        SpringApplication.run Application, args
    }

}

我已经查看了其他一些建议,例如为了类加载器的原因而实现基于Java的界面而不是基于Groovy的界面,但到目前为止这对我没有用。我也尝试将注入方法改为基于构造函数。该类已成功注入,但实际属性为null。

我已经花了几天时间在这里敲击键盘,所以任何进一步的帮助和/或解释为什么它不起作用将不胜感激。

编辑: 以下是使用AccountService.groovy的其他服务:

@Service
@Scope(proxyMode = ScopedProxyMode.TARGET_CLASS)
class PartnerApplicationService {

    def logger = LoggerFactory.getLogger(PartnerApplicationService)

    @Autowired
    PartnerApplicationRepository repository

    @Autowired
    CredentialsRepository credentialsRepository

    @Autowired
    PartnerService partnerService

    @Autowired
    AccountService accountService

//lots more stuff...
}

2 个答案:

答案 0 :(得分:0)

我认为您没有将StormpathAccountService bean注入控制器或其他使用它的classe,如果不尝试注入它:

@Autowired
StormpathAccountService stormpathAccountService;

在其中注入AccountService接口:

@Autowired
AccountService accountService;

答案 1 :(得分:0)

嗯,我想我找到了一个适合我的解决方案。我手动收尾包括StormpathProperties文件作为@ComponentScan的一部分。我不知道为什么需要明确地将其他所有内容都与@ComponentScan正确相关。