2015-09-16 4 views
0

У меня есть следующий выпуск.Свойства профиля по умолчанию Spring

Я использую следующую конфигурацию:

<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> 
    <property name="locations"> 
     <list> 
      <value>classpath:config/main.properties</value> 
      <value>classpath:config/application-${spring.profiles.active}.properties</value> 
     </list> 
    </property> 
    <property name="ignoreUnresolvablePlaceholders" value="true"/> 
    <property name="ignoreResourceNotFound" value="true"/> 
    <property name="searchSystemEnvironment" value="true"/> 
    <property name="systemPropertiesModeName" value="SYSTEM_PROPERTIES_MODE_OVERRIDE"/> 
</bean> 

И это отлично работает, если я использую -Dspring.profiles.active аргумент. Но если я не использую этот аргумент, и полагаться на

<context-param> 
     <param-name>spring.profiles.active</param-name> 
     <param-value>dev</param-value> 
</context-param> 

тогда я получаю следующее исключение:

org.springframework.beans.factory.BeanDefinitionStoreException: Invalid bean definition with name 'org.springframework.beans.factory.config.PropertyPlaceholderConfigurer#0' defined in URL [file:/Users/mlinkov/Projects/target/WEB-INF/classes/main-context.xml]: Could not resolve placeholder 'spring.profiles.active' in string value "classpath:config/application-${spring.profiles.active}.properties"; nested exception is java.lang.IllegalArgumentException: Could not resolve placeholder 'spring.profiles.active' in string value "classpath:config/application-${spring.profiles.active}.properties" 
at org.springframework.beans.factory.config.PlaceholderConfigurerSupport.doProcessProperties(PlaceholderConfigurerSupport.java:211) 
at org.springframework.beans.factory.config.PropertyPlaceholderConfigurer.processProperties(PropertyPlaceholderConfigurer.java:222) 
at org.springframework.beans.factory.config.PropertyResourceConfigurer.postProcessBeanFactory(PropertyResourceConfigurer.java:86) 
at org.springframework.context.support.PostProcessorRegistrationDelegate.invokeBeanFactoryPostProcessors(PostProcessorRegistrationDelegate.java:265) 
at org.springframework.context.support.PostProcessorRegistrationDelegate.invokeBeanFactoryPostProcessors(PostProcessorRegistrationDelegate.java:162) 
at org.springframework.context.support.AbstractApplicationContext.invokeBeanFactoryPostProcessors(AbstractApplicationContext.java:606) 
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:462) 
at org.springframework.web.context.ContextLoader.configureAndRefreshWebApplicationContext(ContextLoader.java:403) 
at org.springframework.web.context.ContextLoader.initWebApplicationContext(ContextLoader.java:306) 
at org.springframework.web.context.ContextLoaderListener.contextInitialized(ContextLoaderListener.java:106) 
at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:5017) 
at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5531) 
at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150) 
at org.apache.catalina.core.ContainerBase.addChildInternal(ContainerBase.java:901) 
at org.apache.catalina.core.ContainerBase.addChild(ContainerBase.java:877) 
at org.apache.catalina.core.StandardHost.addChild(StandardHost.java:652) 
at org.apache.catalina.startup.HostConfig.manageApp(HostConfig.java:1809) 
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.apache.tomcat.util.modeler.BaseModelMBean.invoke(BaseModelMBean.java:301) 
at com.sun.jmx.interceptor.DefaultMBeanServerInterceptor.invoke(DefaultMBeanServerInterceptor.java:819) 
at com.sun.jmx.mbeanserver.JmxMBeanServer.invoke(JmxMBeanServer.java:801) 
at org.apache.catalina.mbeans.MBeanFactory.createStandardContext(MBeanFactory.java:618) 
at org.apache.catalina.mbeans.MBeanFactory.createStandardContext(MBeanFactory.java:565) 
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.apache.tomcat.util.modeler.BaseModelMBean.invoke(BaseModelMBean.java:301) 
at com.sun.jmx.interceptor.DefaultMBeanServerInterceptor.invoke(DefaultMBeanServerInterceptor.java:819) 
at com.sun.jmx.mbeanserver.JmxMBeanServer.invoke(JmxMBeanServer.java:801) 
at javax.management.remote.rmi.RMIConnectionImpl.doOperation(RMIConnectionImpl.java:1487) 
at javax.management.remote.rmi.RMIConnectionImpl.access$300(RMIConnectionImpl.java:97) 
at javax.management.remote.rmi.RMIConnectionImpl$PrivilegedOperation.run(RMIConnectionImpl.java:1328) 
at javax.management.remote.rmi.RMIConnectionImpl.doPrivilegedOperation(RMIConnectionImpl.java:1420) 
at javax.management.remote.rmi.RMIConnectionImpl.invoke(RMIConnectionImpl.java:848) 
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 sun.rmi.server.UnicastServerRef.dispatch(UnicastServerRef.java:322) 
at sun.rmi.transport.Transport$2.run(Transport.java:202) 
at sun.rmi.transport.Transport$2.run(Transport.java:199) 
at java.security.AccessController.doPrivileged(Native Method) 
at sun.rmi.transport.Transport.serviceCall(Transport.java:198) 
at sun.rmi.transport.tcp.TCPTransport.handleMessages(TCPTransport.java:567) 
at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.run0(TCPTransport.java:828) 
at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.access$400(TCPTransport.java:619) 
at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler$1.run(TCPTransport.java:684) 
at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler$1.run(TCPTransport.java:681) 
at java.security.AccessController.doPrivileged(Native Method) 
at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.run(TCPTransport.java:681) 
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145) 
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615) 
at java.lang.Thread.run(Thread.java:745) 
Caused by: java.lang.IllegalArgumentException: Could not resolve placeholder 'spring.profiles.active' in string value "classpath:config/application-${spring.profiles.active}.properties" 
at org.springframework.util.PropertyPlaceholderHelper.parseStringValue(PropertyPlaceholderHelper.java:174) 
at org.springframework.util.PropertyPlaceholderHelper.replacePlaceholders(PropertyPlaceholderHelper.java:126) 
at org.springframework.beans.factory.config.PropertyPlaceholderConfigurer$PlaceholderResolvingStringValueResolver.resolveStringValue(PropertyPlaceholderConfigurer.java:258) 
at org.springframework.beans.factory.config.BeanDefinitionVisitor.resolveStringValue(BeanDefinitionVisitor.java:282) 
at org.springframework.beans.factory.config.BeanDefinitionVisitor.resolveValue(BeanDefinitionVisitor.java:204) 
at org.springframework.beans.factory.config.BeanDefinitionVisitor.visitList(BeanDefinitionVisitor.java:228) 
at org.springframework.beans.factory.config.BeanDefinitionVisitor.resolveValue(BeanDefinitionVisitor.java:192) 
at org.springframework.beans.factory.config.BeanDefinitionVisitor.visitPropertyValues(BeanDefinitionVisitor.java:141) 
at org.springframework.beans.factory.config.BeanDefinitionVisitor.visitBeanDefinition(BeanDefinitionVisitor.java:82) 
at org.springframework.beans.factory.config.PlaceholderConfigurerSupport.doProcessProperties(PlaceholderConfigurerSupport.java:208) 
... 56 more 

несмотря я вижу следующие в журналах

INFO PropertiesLoaderSupport.java 2015-09-16 20:58:47,730: Loading properties file from class path resource [config/main.properties] 


INFO PropertiesLoaderSupport.java 2015-09-16 20:58:47,731: Loading properties file from class path resource [config/application-dev.properties] 

Как я понимаю, это происходит потому, что web.xml context-param не является системной переменной. Можно ли указать файл свойств профиля, используя значение по умолчанию, а не -Dspring.profiles.active переменная?

ответ

0

В условиях, отличных от DEV, вы положительно полагаетесь на spring.profiles.active. Поэтому я бы предложил избегать конфигурации по умолчанию для DEV, потому что это создает разницу в сравнении с не-DEV-средами и вы хотите быть как можно ближе к PROD в своих конфигурациях.

Также измените имя строки-заполнителя (например, на environment.type), так как spring.profiles.active может изменить поведение вашего приложения, включив/отключив @Profile аннотированные бобы. Снова вы хотите как можно больше свести к минимуму такие различия между средами.

+0

Однако есть ли способ заставить его работать без явно определенной переменной в args? Фактически я сделал это, используя ServletContextPropertyPlaceholderConfigurer, который позволяет занять местозаполнитель из web.xml. Но, к сожалению, этот класс устарел –

+0

Правильно ли я считаю, что исключение в теме вызвано тем, что PropertyPlaceholderConfigurer инициализирует заполнители местами и системными переменными, то он находит $ {spring.profiles.active} в контексте, который не имеет соответствующего заполнителя? –

1

Я полагаю, что выяснил, почему я получаю исключение.

PropertyPlaceholderConfigurer используется для установки заполнителей из файла свойств и системных переменных. Если я явно определяю переменную system.profiles.active переменной startup, пружина регистрирует эту переменную как систему, в противном случае я имею в виду получение переменной из web.xml context-property, она не становится системной переменной, поэтому мы получаем исключение, когда PropertyPlaceholderConfigurer пытается получить местозаполнитель на system.profiles.active.

Я надеюсь, что это поможет кому-то

Смежные вопросы