How to read external properties file In Spring? Announcing the arrival of Valued Associate #679: Cesar Manara Planned maintenance scheduled April 23, 2019 at 23:30 UTC (7:30pm US/Eastern) Data science time! April 2019 and salary with experience The Ask Question Wizard is Live!How to Override Spring-boot application.properties programmaticallyHow do I efficiently iterate over each entry in a Java Map?How do I read / convert an InputStream into a String in Java?How do I generate random integers within a specific range in Java?How to avoid Java code in JSP files?Reading a plain text file in JavaHow do I convert a String to an int in Java?How do I fix android.os.NetworkOnMainThreadException?What's the difference between @Component, @Repository & @Service annotations in Spring?spring boot, logback and logging.config propertySpring - @PropertySource, getting an NPE ;(

Why did Bronn offer to be Tyrion Lannister's champion in trial by combat?

Why aren't road bike wheels tiny?

Can gravitational waves pass through a black hole?

tabularx column has extra padding at right?

Is "ein Herz wie das meine" an antiquated or colloquial use of the possesive pronoun?

Can a Wizard take the Magic Initiate feat and select spells from the Wizard list?

A journey... into the MIND

How can I introduce the names of fantasy creatures to the reader?

What kind of equipment or other technology is necessary to photograph sprites (atmospheric phenomenon)

How to keep bees out of canned beverages?

Can the van der Waals coefficients be negative in the van der Waals equation for real gases?

Suing a Police Officer Instead of the Police Department

What is the difference between 准时 and 按时?

Compiling and throwing simple dynamic exceptions at runtime for JVM

false 'Security alert' from Google - every login generates mails from 'no-reply@accounts.google.com'

Why did Israel vote against lifting the American embargo on Cuba?

Will I be more secure with my own router behind my ISP's router?

Is my guitar’s action too high?

Can I ask an author to send me his ebook?

Help Recreating a Table

Trying to enter the Fox's den

Can I take recommendation from someone I met at a conference?

Should man-made satellites feature an intelligent inverted "cow catcher"?

Will the Antimagic Field spell cause elementals not summoned by magic to dissipate?



How to read external properties file In Spring?



Announcing the arrival of Valued Associate #679: Cesar Manara
Planned maintenance scheduled April 23, 2019 at 23:30 UTC (7:30pm US/Eastern)
Data science time! April 2019 and salary with experience
The Ask Question Wizard is Live!How to Override Spring-boot application.properties programmaticallyHow do I efficiently iterate over each entry in a Java Map?How do I read / convert an InputStream into a String in Java?How do I generate random integers within a specific range in Java?How to avoid Java code in JSP files?Reading a plain text file in JavaHow do I convert a String to an int in Java?How do I fix android.os.NetworkOnMainThreadException?What's the difference between @Component, @Repository & @Service annotations in Spring?spring boot, logback and logging.config propertySpring - @PropertySource, getting an NPE ;(



.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;








-1















Everybody knows if we want to read the properties file, we can do as follows:



@Configuration
@PropertySource("classpath:/application.properties")
public class AppConfig

@Value("$app.name")
public String name;


@Bean
public static PropertySourcesPlaceholderConfigurer placeholderConfigurer()
return new PropertySourcesPlaceholderConfigurer();


@Bean
public PostService postService()
return new PostServiceImpl(name);





But, now I have a framework which is similar to SpringBoot. It can integrate Spring with Mybatis.



The problem is preceding code only can read my project classpath file but I need to read the properties file project using my framework. How I do it?



Update



I'm sorry for everybody. Maybe I don't say clearly, so here is the picture: picture



  • I don't use SpringBoot


  • I want to read the project(using my framework) classpath, not my framework classpath.


Thanks.










share|improve this question
























  • what was the problem?

    – Deadpool
    Mar 9 at 3:12











  • Is this what you are trying to accomplish? stackoverflow.com/questions/29072628/…

    – neildo
    Mar 9 at 3:17











  • @Deadpool The problem is preceding code only can read my project classpath file but I need to read the properties file project using my framework. How I do it?

    – Pushy
    Mar 9 at 3:53











  • @neildo Thanks. But not because I don't use SpringBoot.

    – Pushy
    Mar 9 at 3:55











  • what is your my framework.? @Pushy

    – Deadpool
    Mar 9 at 4:14


















-1















Everybody knows if we want to read the properties file, we can do as follows:



@Configuration
@PropertySource("classpath:/application.properties")
public class AppConfig

@Value("$app.name")
public String name;


@Bean
public static PropertySourcesPlaceholderConfigurer placeholderConfigurer()
return new PropertySourcesPlaceholderConfigurer();


@Bean
public PostService postService()
return new PostServiceImpl(name);





But, now I have a framework which is similar to SpringBoot. It can integrate Spring with Mybatis.



The problem is preceding code only can read my project classpath file but I need to read the properties file project using my framework. How I do it?



Update



I'm sorry for everybody. Maybe I don't say clearly, so here is the picture: picture



  • I don't use SpringBoot


  • I want to read the project(using my framework) classpath, not my framework classpath.


Thanks.










share|improve this question
























  • what was the problem?

    – Deadpool
    Mar 9 at 3:12











  • Is this what you are trying to accomplish? stackoverflow.com/questions/29072628/…

    – neildo
    Mar 9 at 3:17











  • @Deadpool The problem is preceding code only can read my project classpath file but I need to read the properties file project using my framework. How I do it?

    – Pushy
    Mar 9 at 3:53











  • @neildo Thanks. But not because I don't use SpringBoot.

    – Pushy
    Mar 9 at 3:55











  • what is your my framework.? @Pushy

    – Deadpool
    Mar 9 at 4:14














-1












-1








-1








Everybody knows if we want to read the properties file, we can do as follows:



@Configuration
@PropertySource("classpath:/application.properties")
public class AppConfig

@Value("$app.name")
public String name;


@Bean
public static PropertySourcesPlaceholderConfigurer placeholderConfigurer()
return new PropertySourcesPlaceholderConfigurer();


@Bean
public PostService postService()
return new PostServiceImpl(name);





But, now I have a framework which is similar to SpringBoot. It can integrate Spring with Mybatis.



The problem is preceding code only can read my project classpath file but I need to read the properties file project using my framework. How I do it?



Update



I'm sorry for everybody. Maybe I don't say clearly, so here is the picture: picture



  • I don't use SpringBoot


  • I want to read the project(using my framework) classpath, not my framework classpath.


Thanks.










share|improve this question
















Everybody knows if we want to read the properties file, we can do as follows:



@Configuration
@PropertySource("classpath:/application.properties")
public class AppConfig

@Value("$app.name")
public String name;


@Bean
public static PropertySourcesPlaceholderConfigurer placeholderConfigurer()
return new PropertySourcesPlaceholderConfigurer();


@Bean
public PostService postService()
return new PostServiceImpl(name);





But, now I have a framework which is similar to SpringBoot. It can integrate Spring with Mybatis.



The problem is preceding code only can read my project classpath file but I need to read the properties file project using my framework. How I do it?



Update



I'm sorry for everybody. Maybe I don't say clearly, so here is the picture: picture



  • I don't use SpringBoot


  • I want to read the project(using my framework) classpath, not my framework classpath.


Thanks.







java spring






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Mar 9 at 15:35









Alexander Sorkin

5419




5419










asked Mar 9 at 3:03









PushyPushy

11




11












  • what was the problem?

    – Deadpool
    Mar 9 at 3:12











  • Is this what you are trying to accomplish? stackoverflow.com/questions/29072628/…

    – neildo
    Mar 9 at 3:17











  • @Deadpool The problem is preceding code only can read my project classpath file but I need to read the properties file project using my framework. How I do it?

    – Pushy
    Mar 9 at 3:53











  • @neildo Thanks. But not because I don't use SpringBoot.

    – Pushy
    Mar 9 at 3:55











  • what is your my framework.? @Pushy

    – Deadpool
    Mar 9 at 4:14


















  • what was the problem?

    – Deadpool
    Mar 9 at 3:12











  • Is this what you are trying to accomplish? stackoverflow.com/questions/29072628/…

    – neildo
    Mar 9 at 3:17











  • @Deadpool The problem is preceding code only can read my project classpath file but I need to read the properties file project using my framework. How I do it?

    – Pushy
    Mar 9 at 3:53











  • @neildo Thanks. But not because I don't use SpringBoot.

    – Pushy
    Mar 9 at 3:55











  • what is your my framework.? @Pushy

    – Deadpool
    Mar 9 at 4:14

















what was the problem?

– Deadpool
Mar 9 at 3:12





what was the problem?

– Deadpool
Mar 9 at 3:12













Is this what you are trying to accomplish? stackoverflow.com/questions/29072628/…

– neildo
Mar 9 at 3:17





Is this what you are trying to accomplish? stackoverflow.com/questions/29072628/…

– neildo
Mar 9 at 3:17













@Deadpool The problem is preceding code only can read my project classpath file but I need to read the properties file project using my framework. How I do it?

– Pushy
Mar 9 at 3:53





@Deadpool The problem is preceding code only can read my project classpath file but I need to read the properties file project using my framework. How I do it?

– Pushy
Mar 9 at 3:53













@neildo Thanks. But not because I don't use SpringBoot.

– Pushy
Mar 9 at 3:55





@neildo Thanks. But not because I don't use SpringBoot.

– Pushy
Mar 9 at 3:55













what is your my framework.? @Pushy

– Deadpool
Mar 9 at 4:14






what is your my framework.? @Pushy

– Deadpool
Mar 9 at 4:14













3 Answers
3






active

oldest

votes


















0














Spring framework can read external configuration files from different locations.
It can read the configuration file from your project directory but you would need to remove this line:



@PropertySource("classpath:/application.properties")


that limits it to your application class path.
You can check here to see the different locations spring read configuration files from.






share|improve this answer























  • Maybe you misunderstand my problem. Could you see my question try again? I edited it

    – Pushy
    Mar 9 at 8:42


















0














Spring provides external configuration. By this you can run your application in different environment.



refer link :
https://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-external-config.html



If you do not like application.properties as the configuration file name, you can switch to another file name by specifying a spring.config.name environment property.



import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.core.env.Environment;

@Configuration
@PropertySource("classpath:db.properties")
@PropertySource("classpath:project.properties")
public class DBConfiguration

@Autowired
Environment env;

@Bean
public DBConnection getDBConnection()
System.out.println("Getting DBConnection Bean for
App:"+env.getProperty("APP_NAME"));
DBConnection dbConnection = new DBConnection(env.getProperty("DB_DRIVER_CLASS"),
env.getProperty("DB_URL"), env.getProperty("DB_USERNAME"),
env.getProperty("DB_PASSWORD").toCharArray());
return dbConnection;




DB.properties:
#Database configuration
DB_DRIVER_CLASS=com.mysql.jdbc.Driver
DB_URL=jdbc:mysql://localhost:3306/Test
DB_USERNAME=root
DB_PASSWORD=root

project.properties:
APP_NAME=TEST APP





share|improve this answer























  • Maybe you misunderstand my problem. Could you see my question try again?

    – Pushy
    Mar 9 at 8:41


















0














If you are just wanting to read properties yourself from the classpath, you can use



Properties prop = new Properties();
InputStream input = this.getClass().getResourceAsStream("/application.properties")
prop.load(input);

// get the property value and print it out
System.out.println(prop.getProperty("foo"));





share|improve this answer

























    Your Answer






    StackExchange.ifUsing("editor", function ()
    StackExchange.using("externalEditor", function ()
    StackExchange.using("snippets", function ()
    StackExchange.snippets.init();
    );
    );
    , "code-snippets");

    StackExchange.ready(function()
    var channelOptions =
    tags: "".split(" "),
    id: "1"
    ;
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function()
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled)
    StackExchange.using("snippets", function()
    createEditor();
    );

    else
    createEditor();

    );

    function createEditor()
    StackExchange.prepareEditor(
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader:
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    ,
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    );



    );













    draft saved

    draft discarded


















    StackExchange.ready(
    function ()
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55073597%2fhow-to-read-external-properties-file-in-spring%23new-answer', 'question_page');

    );

    Post as a guest















    Required, but never shown

























    3 Answers
    3






    active

    oldest

    votes








    3 Answers
    3






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    0














    Spring framework can read external configuration files from different locations.
    It can read the configuration file from your project directory but you would need to remove this line:



    @PropertySource("classpath:/application.properties")


    that limits it to your application class path.
    You can check here to see the different locations spring read configuration files from.






    share|improve this answer























    • Maybe you misunderstand my problem. Could you see my question try again? I edited it

      – Pushy
      Mar 9 at 8:42















    0














    Spring framework can read external configuration files from different locations.
    It can read the configuration file from your project directory but you would need to remove this line:



    @PropertySource("classpath:/application.properties")


    that limits it to your application class path.
    You can check here to see the different locations spring read configuration files from.






    share|improve this answer























    • Maybe you misunderstand my problem. Could you see my question try again? I edited it

      – Pushy
      Mar 9 at 8:42













    0












    0








    0







    Spring framework can read external configuration files from different locations.
    It can read the configuration file from your project directory but you would need to remove this line:



    @PropertySource("classpath:/application.properties")


    that limits it to your application class path.
    You can check here to see the different locations spring read configuration files from.






    share|improve this answer













    Spring framework can read external configuration files from different locations.
    It can read the configuration file from your project directory but you would need to remove this line:



    @PropertySource("classpath:/application.properties")


    that limits it to your application class path.
    You can check here to see the different locations spring read configuration files from.







    share|improve this answer












    share|improve this answer



    share|improve this answer










    answered Mar 9 at 6:54









    OlantobiOlantobi

    6141714




    6141714












    • Maybe you misunderstand my problem. Could you see my question try again? I edited it

      – Pushy
      Mar 9 at 8:42

















    • Maybe you misunderstand my problem. Could you see my question try again? I edited it

      – Pushy
      Mar 9 at 8:42
















    Maybe you misunderstand my problem. Could you see my question try again? I edited it

    – Pushy
    Mar 9 at 8:42





    Maybe you misunderstand my problem. Could you see my question try again? I edited it

    – Pushy
    Mar 9 at 8:42













    0














    Spring provides external configuration. By this you can run your application in different environment.



    refer link :
    https://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-external-config.html



    If you do not like application.properties as the configuration file name, you can switch to another file name by specifying a spring.config.name environment property.



    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.context.annotation.Bean;
    import org.springframework.context.annotation.Configuration;
    import org.springframework.context.annotation.PropertySource;
    import org.springframework.core.env.Environment;

    @Configuration
    @PropertySource("classpath:db.properties")
    @PropertySource("classpath:project.properties")
    public class DBConfiguration

    @Autowired
    Environment env;

    @Bean
    public DBConnection getDBConnection()
    System.out.println("Getting DBConnection Bean for
    App:"+env.getProperty("APP_NAME"));
    DBConnection dbConnection = new DBConnection(env.getProperty("DB_DRIVER_CLASS"),
    env.getProperty("DB_URL"), env.getProperty("DB_USERNAME"),
    env.getProperty("DB_PASSWORD").toCharArray());
    return dbConnection;




    DB.properties:
    #Database configuration
    DB_DRIVER_CLASS=com.mysql.jdbc.Driver
    DB_URL=jdbc:mysql://localhost:3306/Test
    DB_USERNAME=root
    DB_PASSWORD=root

    project.properties:
    APP_NAME=TEST APP





    share|improve this answer























    • Maybe you misunderstand my problem. Could you see my question try again?

      – Pushy
      Mar 9 at 8:41















    0














    Spring provides external configuration. By this you can run your application in different environment.



    refer link :
    https://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-external-config.html



    If you do not like application.properties as the configuration file name, you can switch to another file name by specifying a spring.config.name environment property.



    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.context.annotation.Bean;
    import org.springframework.context.annotation.Configuration;
    import org.springframework.context.annotation.PropertySource;
    import org.springframework.core.env.Environment;

    @Configuration
    @PropertySource("classpath:db.properties")
    @PropertySource("classpath:project.properties")
    public class DBConfiguration

    @Autowired
    Environment env;

    @Bean
    public DBConnection getDBConnection()
    System.out.println("Getting DBConnection Bean for
    App:"+env.getProperty("APP_NAME"));
    DBConnection dbConnection = new DBConnection(env.getProperty("DB_DRIVER_CLASS"),
    env.getProperty("DB_URL"), env.getProperty("DB_USERNAME"),
    env.getProperty("DB_PASSWORD").toCharArray());
    return dbConnection;




    DB.properties:
    #Database configuration
    DB_DRIVER_CLASS=com.mysql.jdbc.Driver
    DB_URL=jdbc:mysql://localhost:3306/Test
    DB_USERNAME=root
    DB_PASSWORD=root

    project.properties:
    APP_NAME=TEST APP





    share|improve this answer























    • Maybe you misunderstand my problem. Could you see my question try again?

      – Pushy
      Mar 9 at 8:41













    0












    0








    0







    Spring provides external configuration. By this you can run your application in different environment.



    refer link :
    https://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-external-config.html



    If you do not like application.properties as the configuration file name, you can switch to another file name by specifying a spring.config.name environment property.



    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.context.annotation.Bean;
    import org.springframework.context.annotation.Configuration;
    import org.springframework.context.annotation.PropertySource;
    import org.springframework.core.env.Environment;

    @Configuration
    @PropertySource("classpath:db.properties")
    @PropertySource("classpath:project.properties")
    public class DBConfiguration

    @Autowired
    Environment env;

    @Bean
    public DBConnection getDBConnection()
    System.out.println("Getting DBConnection Bean for
    App:"+env.getProperty("APP_NAME"));
    DBConnection dbConnection = new DBConnection(env.getProperty("DB_DRIVER_CLASS"),
    env.getProperty("DB_URL"), env.getProperty("DB_USERNAME"),
    env.getProperty("DB_PASSWORD").toCharArray());
    return dbConnection;




    DB.properties:
    #Database configuration
    DB_DRIVER_CLASS=com.mysql.jdbc.Driver
    DB_URL=jdbc:mysql://localhost:3306/Test
    DB_USERNAME=root
    DB_PASSWORD=root

    project.properties:
    APP_NAME=TEST APP





    share|improve this answer













    Spring provides external configuration. By this you can run your application in different environment.



    refer link :
    https://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-external-config.html



    If you do not like application.properties as the configuration file name, you can switch to another file name by specifying a spring.config.name environment property.



    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.context.annotation.Bean;
    import org.springframework.context.annotation.Configuration;
    import org.springframework.context.annotation.PropertySource;
    import org.springframework.core.env.Environment;

    @Configuration
    @PropertySource("classpath:db.properties")
    @PropertySource("classpath:project.properties")
    public class DBConfiguration

    @Autowired
    Environment env;

    @Bean
    public DBConnection getDBConnection()
    System.out.println("Getting DBConnection Bean for
    App:"+env.getProperty("APP_NAME"));
    DBConnection dbConnection = new DBConnection(env.getProperty("DB_DRIVER_CLASS"),
    env.getProperty("DB_URL"), env.getProperty("DB_USERNAME"),
    env.getProperty("DB_PASSWORD").toCharArray());
    return dbConnection;




    DB.properties:
    #Database configuration
    DB_DRIVER_CLASS=com.mysql.jdbc.Driver
    DB_URL=jdbc:mysql://localhost:3306/Test
    DB_USERNAME=root
    DB_PASSWORD=root

    project.properties:
    APP_NAME=TEST APP






    share|improve this answer












    share|improve this answer



    share|improve this answer










    answered Mar 9 at 8:09









    RoshiniRoshini

    396




    396












    • Maybe you misunderstand my problem. Could you see my question try again?

      – Pushy
      Mar 9 at 8:41

















    • Maybe you misunderstand my problem. Could you see my question try again?

      – Pushy
      Mar 9 at 8:41
















    Maybe you misunderstand my problem. Could you see my question try again?

    – Pushy
    Mar 9 at 8:41





    Maybe you misunderstand my problem. Could you see my question try again?

    – Pushy
    Mar 9 at 8:41











    0














    If you are just wanting to read properties yourself from the classpath, you can use



    Properties prop = new Properties();
    InputStream input = this.getClass().getResourceAsStream("/application.properties")
    prop.load(input);

    // get the property value and print it out
    System.out.println(prop.getProperty("foo"));





    share|improve this answer





























      0














      If you are just wanting to read properties yourself from the classpath, you can use



      Properties prop = new Properties();
      InputStream input = this.getClass().getResourceAsStream("/application.properties")
      prop.load(input);

      // get the property value and print it out
      System.out.println(prop.getProperty("foo"));





      share|improve this answer



























        0












        0








        0







        If you are just wanting to read properties yourself from the classpath, you can use



        Properties prop = new Properties();
        InputStream input = this.getClass().getResourceAsStream("/application.properties")
        prop.load(input);

        // get the property value and print it out
        System.out.println(prop.getProperty("foo"));





        share|improve this answer















        If you are just wanting to read properties yourself from the classpath, you can use



        Properties prop = new Properties();
        InputStream input = this.getClass().getResourceAsStream("/application.properties")
        prop.load(input);

        // get the property value and print it out
        System.out.println(prop.getProperty("foo"));






        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Mar 9 at 15:42

























        answered Mar 9 at 15:08









        neildoneildo

        1,01777




        1,01777



























            draft saved

            draft discarded
















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid


            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.

            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55073597%2fhow-to-read-external-properties-file-in-spring%23new-answer', 'question_page');

            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            1928 у кіно

            Захаров Федір Захарович

            Ель Греко