Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8949059
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T13:09:03+00:00 2026-06-15T13:09:03+00:00

In spring JavaConfig, I can define property source and inject into Environment @PropertySource(classpath:application.properties) @Inject

  • 0

In spring JavaConfig, I can define property source and inject into Environment

@PropertySource("classpath:application.properties")

@Inject private Environment environment;

How do I do that if in xml?
I am using context:property-placeholder, and on the JavaConfig class @ImportResource to import the xml. But I cannot retrieve property defined in the properties file using environment.getProperty(“xx”)

<context:property-placeholder location="classpath:application.properties" />
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T13:09:04+00:00Added an answer on June 15, 2026 at 1:09 pm

    AFAIK, there is no way of doing this by pure XML. Anyways, here is a little code I did this morning:

    First, the test:

    public class EnvironmentTests {
    
        @Test
        public void addPropertiesToEnvironmentTest() {
    
            ApplicationContext context = new ClassPathXmlApplicationContext(
                    "testContext.xml");
    
            Environment environment = context.getEnvironment();
    
            String world = environment.getProperty("hello");
    
            assertNotNull(world);
    
            assertEquals("world", world);
    
            System.out.println("Hello " + world);
    
        }
    
    }
    

    Then the class:

    public class PropertySourcesAdderBean implements InitializingBean,
            ApplicationContextAware {
    
        private Properties properties;
    
        private ApplicationContext applicationContext;
    
        public PropertySourcesAdderBean() {
    
        }
    
        public void afterPropertiesSet() throws Exception {
    
        PropertiesPropertySource propertySource = new PropertiesPropertySource(
                "helloWorldProps", this.properties);
    
        ConfigurableEnvironment environment = (ConfigurableEnvironment) this.applicationContext
                .getEnvironment();
    
        environment.getPropertySources().addFirst(propertySource);
    
        }
    
        public Properties getProperties() {
            return properties;
        }
    
        public void setProperties(Properties properties) {
            this.properties = properties;
        }
    
        public void setApplicationContext(ApplicationContext applicationContext)
                throws BeansException {
    
            this.applicationContext = applicationContext;
    
        }
    
    }
    

    And the testContext.xml:

    <?xml version="1.0" encoding="UTF-8"?>
    <beans ...>
    
        <util:properties id="props" location="classpath:props.properties" />
    
        <bean id="propertySources" class="org.mael.stackoverflow.testing.PropertySourcesAdderBean">
            <property name="properties" ref="props" />
        </bean>
    
    
    </beans>
    

    And the props.properties file:

    hello=world
    

    It is pretty simple, just use a ApplicationContextAware bean and get the ConfigurableEnvironment from the (Web)ApplicationContext. Then just add a PropertiesPropertySource to the MutablePropertySources

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Spring.NET is an open source application framework that makes building enterprise .NET applications easier.
The old docs for Spring Javaconfig say that I can use @Bean(scope=DefaultScopes.PROTOTYPE) to get
In Spring MVC Hibernate application , when i am trying to use properties file
Spring ROO have built-in email support So you can setup email sending properties with
From Spring 3.1, we can use JavaConfig more easily thanks to @Enable* annotations. So
Where can I find Spring 3 IoC JavaConfig (annotations) tutorial? If possible, please provide
[spring 3.0.5] MVC I have on class like that: public class Address { private
Spring Roo can be use in existing projects that follow standard maven layout .
I have spring Java config object that loads my properties but does not override
Spring has PropertyPlaceholderConfigurer . Is there something similar that uses beans instead of property

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.