-
Notifications
You must be signed in to change notification settings - Fork 38.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Document alternatives of using multiple PropertyPlaceholderConfigurer
s [SPR-9989]
#14623
Comments
Stevo Slavić commented As temporary workaround one can use different value separator in each PPC (e.g. : and ~ ) |
Phil Webb commented Hi Johann, Do you actually need to add multiple property placeholder resolvers? Will the following work for you? <bean class="org.springframework.context.support.PropertySourcesPlaceholderConfigurer">
<property name="ignoreUnresolvablePlaceholders" value="true"/>
<property name="propertiesArray">
<array>
<ref bean="properties1"/>
<ref bean="properties2"/>
</array>
</property>
</bean>
<util:properties id="properties1">
<prop key="prop1">value1</prop>
</util:properties>
<util:properties id="properties2">
<prop key="prop2">value2</prop>
</util:properties> |
Johann Vanackere commented Hi Phil, I've used a single Anyway, Spring supports using multiple property placeholder resolvers, so |
Paul Tomlin commented I've created a workaround for XML users which simply registers the PropertySource with the Environment, leaving the placeholder replacement as a task for an otherwise configured PropertySourcesPlaceholderConfigurer.
|
Sergei Ivanov commented We have recently run into a similar issue. In our case we have a class that extends The problem arises if some of those imported contexts (as well as possibly the master context) use default values for properties. In that case, the order of imports has a dramatic and unpredictable effect on the way how and when default values are used. Basically, the first In answer to Phil's question: we cannot aggregate all instances of I am attaching a simplified test case for the case that we have got, only that the imported contexts sit in modules of their own. I could expand it into a Maven reactor project, but for the demonstration purposes it should suffice. I took a look at the sources of Spring framework and spent some time inside them. The whole logic sits inside I think I'll exploit Paul Tomlin's idea of injecting property sources directly into the environment (need to make sure the order is set correctly so that property injection is always done up-front) and then including a singular bog-standard empty |
Oleksandr Gavenko commented Problem also discussed at http://stackoverflow.com/questions/16892752/property-not-found-with-multiple-contextproperty-placeholder : Each context:property-placeholder creates a new instance of PropertyPlaceholderConfigurer - it gets messy easily. Solution with custom PropertyPlaceholderConfigurer described at blog: http://rostislav-matl.blogspot.cz/2013/06/resolving-properties-with-spring.html |
Mauro Molinari commented This is a serious issue :-( |
Sergei Ivanov commented We have actually implemented the idea that I described in the earlier comment. I haven't got the implementation at hand now, but from the best of my recollection, we converted our custom implementation of Basically, for the whole set-up to work, you need to split the functionality into two stages:
The first stage will require a new tag, something like |
Mauro Molinari commented IMHO the impossibility to add property sources to the environment using built-in tags or beans is a limit and an inconsistency of Spring as of now. However, I think that, independently of this, if the framework allows to define more than one |
Sergei Ivanov commented Default values for properties were also a late addition: I may be wrong, but I believe they were only introduced in Spring 3.x. Likewise, the behaviour of |
Juergen Hoeller commented I'm leaning towards just documenting such scenarios. The use of multiple The only real point of multiple In general, it's definitely preferable to configure multiple source locations for a single placeholder configurer, or to register property sources with the Juergen |
Scott Feldstein commented One more scenario to consider, we overlay our spring context by importing an applicationContext.xml from another project archive which uses the PropertyPlaceholderConfigurer. I had to work around this issue by Autowiring the existing PropertyPlaceholderConfigurer into our
One issue worth noting with this approach is that the PropertyPlaceholderConfigurer doesn't accept additional locations after the initial locations have been set without overriding the existing locations. Not sure if that will have consequences or not. |
Scott Feldstein commented I apologize, my example above does not seem to be completely working, the required flag is not being respected when i do not overlay the other xml context for integration test purposes. Therefore when the bean doesn't exist a NoSuchBeanDefinitionException is thrown. I am using spring boot version 1.2.1.RELEASE (org.springframework:spring-core:4.1.4.RELEASE). Does anyone know if that is a known issue or should I file a separate bug?
|
Duane Zamrok commented There seems to be a lack of understanding as to why you would want multiple PropertyConfigurators. For me, the answer is division of interests. I have an EAR file with several WAR files using a shared context. The shared context, effectively the parent context, has a PropertyConfigurator which encompasses properties loaded and used in the top level context. My WAR files however also have their own context files. I would like the WAR context files to load their own set of properties, specific to the individual WAR. However, because of this particular bug I am unable to do so. That said, I have looked into the bug personally, tracing the code with the debugger. I understand why it's an issue, and I understand what makes the bug difficult to correct given the current framework and implementation. However, I just wanted to throw in my two cents since I'm seeing a lot of "who would do that?!" comments. |
Veit Guna commented I would also like to give an example, why multiple PSPC's can be handy. In my case, I'm using two PSPC's in one context to allow reading multiple properties - one optional for overrides, and one required base one with default configs like so: <context:property-placeholder location="classpath:custom.properties" ignore-resource-not-found="true" order="1" ignore-unresolvable="true" />
<context:property-placeholder location="classpath:application.properties" order="2" /> So, that reads defaults from db.jdbc.url=${DB_JDBC_URL:jdbc:postgresql://${db.host}:${db.port}/${db.name}} So having Are there alternatives to get this behavior with existing spring features? |
NathanXu commented After quite some experimenting (trying to limit the refactoring scope, though big refactoring seems inevitable for this ticket), I think I've found an ideal solution (see master...NathanQingyangXu:SPR-9989):
@Test
public void resolveEmbeddedDefaultValue() {
DefaultListableBeanFactory bf = new DefaultListableBeanFactory();
Properties props1 = new Properties();
Properties props2 = new Properties();
props2.put("A", "B");
bf.addEmbeddedValueResolver(buildDefaultIgnorableStringValueResolver(props1));
bf.addEmbeddedValueResolver(buildDefaultIgnorableStringValueResolver(props2));
String resolvedValue = bf.resolveEmbeddedValue("${A:defaultValue}");
assertEquals("B", resolvedValue);
} Seems the ideal solution to this tricky refactoring ticket. |
Bulk closing outdated, unresolved issues. Please, reopen if still relevant. |
Dear family, I'm new in issue-following and I don't know exactly which is the right procedure to follow an issue. But in this case, I could see that it's closed without any solution. Am I wrong? Thank you! |
@admoca60, your assessment is correct as per the comment above yours: #14623 (comment) |
so, in that case, the issue will be fixed or maybe the issue has been discarded? |
It means the latter: the issue was closed without a fix, due to lack of recent community involvement, but it can be reopened for renewed discussion if the community wishes it. |
Hello to all, Just to let you know. We recently bumped against this problem: using default values in a multi PropertySourcePlaceholderConfigurer made our 'multilevel' imported context configurations behave erratically when trying to override these defaults. The final solution we found was to remove all those defaults from the placeholder and declare them as local properties in the context declaring the PSPC locations (the root of that context 'branch'). IMHO, its not the best approach and far from being the cleanest one but at lease it lets us combine components with their own placeholder definitions. This approach still cannot solve importing 3rd party contexts 'as-is' but could help in some other scenarios. Hope this helps anyone around! Kind Regards! JP |
@jprojas-tec we recently meet the same problem. |
I too am seeing this issue. Please re-open this bug. |
/reopen |
Seeing this as well |
spring version: 5.2.12 we are facing the same problem when we are trying to use multiple context:property-placeholder labels to import different propertiess in our project. If this bug is fixed or have a good solution to avoid this problem? |
@sbrannen : can you reopen the issue? |
In the case that your project uses one single PSPC besides others that may exist in dependencies (as opposed to multiple ones in your own project), the following workaround may be applied: set the You may have better luck by actually adding your properties to the Spring Environment, for example via the |
PropertyPlaceholderConfigurer
s breaks @Value
default value behavior [SPR-9989]
PropertyPlaceholderConfigurer
s breaks @Value
default value behavior [SPR-9989]PropertyPlaceholderConfigurer
s [SPR-9989]
This thread has several use cases and most of them are about modularization. Rather than creating multiple If you want to apply the replacement against a set of well-defined keys, rather than using the full Configuring multiple configurers is still supported, but each should have a specific placeholder syntax (for example |
Johann Vanackere opened SPR-9989 and commented
When using multiple
PropertyPlaceholderConfigurer
in conjunction with@Value
annotation and default value for placeholders syntax (ie${key:defaultValue}
), only the firstPropertyPlaceholderConfigurer
is used. If this configurer does not contain the desired value, it falls back to@Value
default even if the secondPropertyPlaceholderConfigurer
contains the value.You'll find attached a JUnit test case with Spring xml context to reproduce the issue.
Affects: 3.1.3
Attachments:
Issue Links:
Referenced from: commits spring-attic/spring-framework-issues@0017731
41 votes, 43 watchers
The text was updated successfully, but these errors were encountered: