Uploaded image for project: 'Spring Framework'
  1. Spring Framework
  2. SPR-10409

Properties from @Import-ed Configuration override properties in @Import-ing configuration

    Details

      Description

      I have a property `test=default` in class DefaultConfig, and I'm making them available using @PropertySource annotation.

      @Configuration
      @PropertySource("classpath:default.properties")
      public class DefaultConfig {}
      

      I then want to be able to override to `test=override`, which is in a different properties file in class OverrideConfig, so I again use @PropertySource.

      @Configuration
      @Import(DefaultConfig.class)
      @PropertySource("classpath:override.properties")
      public class OverrideConfig {}
      

      I configure a test to prove that it works.

      @RunWith(SpringJUnit4ClassRunner.class)
      @ContextConfiguration(classes={OverrideConfig.class})
      public class TestPropertyOverride {
          
          @Autowired
          private Environment env;
          
          @Test
          public void propertyIsOverridden() {
          	assertEquals("override", env.getProperty("test"));
          }
          
      }
      

      Except of course it does not.

      org.junit.ComparisonFailure: expected:<[override]> but was:<[default]>

      Maxing out debug, I can see what's happening:

      StandardEnvironment:107 - Adding [class path resource [default.properties]] PropertySource with lowest search precedence
      StandardEnvironment:107 - Adding [class path resource [override.properties]] PropertySource with lowest search precedence

      I can't provide default property values in base configurations and then override them in others.

      Am I making a simple mistake or misthinking this, or would you expect the properties defined by an @PropertySource in an @Import-ed configuration class to be overridden by properties defined in am @PropertySource in the @Import-ing class?

        Attachments

          Activity

            People

            • Assignee:
              Unassigned
              Reporter:
              brabster Paul Brabban
              Last updater:
              Chris Beams
            • Votes:
              4 Vote for this issue
              Watchers:
              7 Start watching this issue

              Dates

              • Created:
                Updated:
                Days since last comment:
                4 years, 6 weeks, 6 days ago