【发布时间】:2015-03-07 00:23:36
【问题描述】:
我正在尝试将属性类注入到基于 Groovy 的类中,但注入的类为空。我确实有另一个属性类被注入到从 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
最后是我的主要应用程序类:
@Configuration
@ComponentScan
@EnableAutoConfiguration
@EnableTransactionManagement
@EnableConfigurationProperties
class Application {
static void main(String[] args) {
SpringApplication.run Application, args
}
}
出于类加载器的原因,我查看了其他一些建议,例如实现基于 Java 的接口而不是基于 Groovy 的接口,但到目前为止,这对我没有用。我还尝试将注入方法更改为基于构造函数。类已成功注入,但实际属性为空。
我已经花了几天的时间在这里敲击键盘,所以任何进一步的帮助和/或解释为什么它不起作用将不胜感激。
编辑: 这是使用 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...
}
【问题讨论】:
-
你有控制器类吗?可以展示一下吗?
-
我有另一个服务类,它只是简单地调用接口,例如:accountService.createAccount(account) 接口现在就在那里。
-
你有注入 StormpathAccountService 或 AccountService 吗?(使用@Autowired)
-
是的。最新的编辑应该表明这一点。
标签: spring groovy spring-boot