我正在尝试将属性类注入基于 Groovy 的类中,但注入的类为 null。 我确实有另一个属性类正在注入到从 Tomcat 的过滤器接口实现并且工作正常的类中。
这是我启动 Spring 引导应用程序时的堆栈跟踪:
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...
}
}
正在实现的时髦接口:
interface AccountService {
def createAccount(PartnerAccount account);
def deleteAccount(PartnerAccount account);
ApiKey fetchApiKey(PartnerAccount account);
}
属性类:
@Configuration
@ConfigurationProperties(prefix = "stormpath")
class StormpathProperties {
String apiKeyLocation
String accountUrl
}
以及我的应用程序属性文件中的属性名称:
stormpath.apiKeyLocation
stormpath.accountUrl
最后是我的主要应用程序类:
@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...
}
我认为您没有将StormpathAccountService
bean 注入控制器或其他使用它的类,如果不尝试注入它:
@Autowired
StormpathAccountService stormpathAccountService;
或:在其中注入AccountService
接口:
@Autowired
AccountService accountService;
好吧,我想我找到了一个适合我的解决方案。 我手动将 StormpathProperties 文件作为@ComponentScan的一部分。 我不知道为什么这需要明确,其他一切都正确@ComponentScan。