2014-11-04 58 views
0

我們正在建立一個先進/複雜的多模塊構建,包括gradle,groovy和spring-boot。以編程方式添加另一個PropertyResourceConfigurer,使用彈簧啓動

除了使用@EnableAutoConfiguration自動選取應用* .yml文件,我們想註冊「自定義」 PropertyResourceConfigurer處理放在一個單獨的「配置」模塊的通用性,可以跨多個彈簧被重用啓動應用。

然而,添加在@Configuration註解類以下時,啓動失敗,異常

@Configuration 
class CommonConfig { 

    @Autowired 
    Environment env; 

@Bean (name = 'geit') 
    PropertyResourceConfigurer geitProperties() { 
     PropertyResourceConfigurer configurer = new PropertyPlaceholderConfigurer(); 

     Resource[] resources = new Resource[env.activeProfiles.length]; 
     println "Environment2 : ${env}" 
     env.activeProfiles.eachWithIndex() { 
      env, i -> resources[i] = new UrlResource(getURL(String.format("classpath:environment/%s.properties", env))) 
     } 

     configurer.setLocations(resources) 
     return configurer 
    } 

} 

的例外是

4fa8a851930816b4d09ecb1/springloaded-1.2.1.RELEASE.jar] 
    at org.codehaus.groovy.runtime.NullObject.getProperty(NullObject.java:57) 
2014-11-04 14:13:56.975 ERROR 48941 --- [   main] o.s.boot.SpringApplication    : Application startup failed 
    at org.codehaus.groovy.runtime.InvokerHelper.getProperty(InvokerHelper.java:168) 

    at org.codehaus.groovy.runtime.callsite.NullCallSite.getProperty(NullCallSite.java:44) 
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'geit' defined in class path resource [geit/config/CommonConfig.class]: Instantiation of bean failed; nested exception is org.springframework.beans.factory.BeanDefinitionStoreException: Factory method [public org.springframework.beans.factory.config.PropertyResourceConfigurer geit.config.CommonConfig.geitProperties()] threw exception; nested exception is java.lang.NullPointerException: Cannot get property 'activeProfiles' on null object 
    at org.codehaus.groovy.runtime.callsite.AbstractCallSite.callGetProperty(AbstractCallSite.java:227) 
    at org.springframework.beans.factory.support.ConstructorResolver.instantiateUsingFactoryMethod(ConstructorResolver.java:601) 
    at geit.config.CommonConfig.geitProperties(CommonConfig.groovy:55) 
    at geit.config.CommonConfig$$EnhancerBySpringCGLIB$$c13414af.CGLIB$geitProperties$21(<generated>) 
    at geit.config.CommonConfig$$EnhancerBySpringCGLIB$$c13414af$$FastClassBySpringCGLIB$$e7b06e5c.invoke(<generated>) 
    at org.springframework.cglib.proxy.MethodProxy.invokeSuper(MethodProxy.java:228) 
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateUsingFactoryMethod(AbstractAutowireCapableBeanFactory.java:1113) 
    at org.springframework.context.annotation.ConfigurationClassEnhancer$BeanMethodInterceptor.intercept(ConfigurationClassEnhancer.java:312) 
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFaat geit.config.CommonConfig$$EnhancerBySpringCGLIB$$c13414af.geitProperties(<generated>) 
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) 
ry.java:1008) 
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57) 
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) 
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:505) 
    at java.lang.reflect.Method.invoke(Method.java:606) 
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:476) 
    at org.springsource.loaded.ri.ReflectiveInterceptor.jlrMethodInvoke(ReflectiveInterceptor.java:1270) 
    at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:302) 
    at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:166) 
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:229) 
    ... 22 more 
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:298) 
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:198) 
    at org.springframework.context.support.PostProcessorRegistrationDelegate.invokeBeanFactoryPostProcessors(PostProcessorRegistrationDelegate.java:150) 
    at org.springframework.context.support.AbstractApplicationContext.invokeBeanFactoryPostProcessors(AbstractApplicationContext.java:606) 
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:462) 
    at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.refresh(EmbeddedWebApplicationContext.java:109) 
    at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:692) 
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:322) 
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:962) 
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:951) 
    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 geit.AdminApplication.main(AdminApplication.groovy:19) 
Caused by: org.springframework.beans.factory.BeanDefinitionStoreException: Factory method [public org.springframework.beans.factory.config.PropertyResourceConfigurer geit.config.CommonConfig.geitProperties()] threw exception; nested exception is java.lang.NullPointerException: Cannot get property 'activeProfiles' on null object 
    at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:188) 
    at org.springframework.beans.factory.support.ConstructorResolver.instantiateUsingFactoryMethod(ConstructorResolver.java:590) 
    ... 21 common frames omitted 
Caused by: java.lang.NullPointerException: Cannot get property 'activeProfiles' on null object 
    at org.codehaus.groovy.runtime.NullObject.getProperty(NullObject.java:57) 
    at org.codehaus.groovy.runtime.InvokerHelper.getProperty(InvokerHelper.java:168) 
    at org.codehaus.groovy.runtime.callsite.NullCallSite.getProperty(NullCallSite.java:44) 
    at org.codehaus.groovy.runtime.callsite.AbstractCallSite.callGetProperty(AbstractCallSite.java:227) 
    at geit.config.CommonConfig.geitProperties(CommonConfig.groovy:55) 
    at geit.config.CommonConfig$$EnhancerBySpringCGLIB$$c13414af.CGLIB$geitProperties$21(<generated>) 
    at geit.config.CommonConfig$$EnhancerBySpringCGLIB$$c13414af$$FastClassBySpringCGLIB$$e7b06e5c.invoke(<generated>) 
    at org.springframework.cglib.proxy.MethodProxy.invokeSuper(MethodProxy.java:228) 
    at org.springframework.context.annotation.ConfigurationClassEnhancer$BeanMethodInterceptor.intercept(ConfigurationClassEnhancer.java:312) 
    at geit.config.CommonConfig$$EnhancerBySpringCGLIB$$c13414af.geitProperties(<generated>) 
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) 
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57) 
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) 
    at java.lang.reflect.Method.invoke(Method.java:606) 
    at org.springsource.loaded.ri.ReflectiveInterceptor.jlrMethodInvoke(ReflectiveInterceptor.java:1270) 
    at org.springframework.beans.factory.support.SimpleInstantiationStrategy.iate(SimpleInstantiationStrategy.java:166) 
    ... 22 common frames omitted 

然而,加入以下CommonConfig

@Bean 
    public String profileConfigBean() { 

     println "Environment : ${env}" 

     env.activeProfiles.each { 
      println it 
     } 

     'devprofilebean' 
    } 

這可以正確打印所有活動的配置文件。

即使添加一個空的PropertyResourceConfigurer導致啓動失敗

回答

0

實測值的工作溶液。

我爲每個環境添加了一個用@PropertySource註釋的類,用於默認屬性。

例子: 一個用於DEV

@Configuration 
@Profile(Profiles.DEV) 
@PropertySource("classpath:/environment/dev.properties") 
class CommonDevConfig { 

} 

和一個默認

@Configuration 
@PropertySource("classpath:/environment/default.properties") 
class CommonConfig { 

} 
相關問題