2014-11-04 59 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 { 

} 
相关问题