0

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.

Alexander Sorkin
  • 634
  • 7
  • 20
Flyer
  • 11
  • 1
  • 1
  • 2
  • what was the problem? – Ryuzaki L Mar 09 '19 at 03:12
  • Is this what you are trying to accomplish? https://stackoverflow.com/questions/29072628/how-to-override-spring-boot-application-properties-programmatically – neildo Mar 09 '19 at 03: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? – Flyer Mar 09 '19 at 03:53
  • @neildo Thanks. But not because I don't use SpringBoot. – Flyer Mar 09 '19 at 03:55
  • what is your `my framework.`? @Pushy – Ryuzaki L Mar 09 '19 at 04:14
  • Sorry, I don't say clearly. The "my framework" means a project which can integrate Spring with Mybatis automatically. It named JobnessWebmvc So I need to get the configuration properties that use my framework such database name and password, server port and so on. @Deadpool – Flyer Mar 09 '19 at 04:22
  • 1
    If the property file that you want to read (regardless of which framework it is coming from) is on the classpath your code should work. If it is not on the classpath, you can use `@PropertySource` with a file location as well: `@PropertySource("file:/application.properties")` – Dan_Maff Mar 09 '19 at 04:43
  • You can check https://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-external-config.html to see different locations where spring can read properties file from. – Olantobi Mar 09 '19 at 06:42
  • `@PropertySource("classpath:/application.properties")` that is not external at all. – Antoniossss Mar 09 '19 at 06:57
  • The question asked seems valid to me and should not be downvoted. The ask is that if the properties file is not in classpath and situated elsewhere, in a different location external to the classpath, how in that case can Spring be used to read and inject such properties. Note: The asker doesn't use BOOT – Ed Bighands Jul 29 '20 at 04:23

5 Answers5

1

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
Roshini
  • 85
  • 1
  • 8
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.

Olantobi
  • 869
  • 1
  • 8
  • 16
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"));
neildo
  • 2,206
  • 15
  • 12
0

For non boot users who want to scan properties external to application classpath:

@PropertySource("file:/path/to/application.properties") The "file" can be replaced with "http" for webhosted remote properties

Ed Bighands
  • 159
  • 8
0

I using next option to load properties file from anywhere, and put it into environment to access it via Environment#getProperty or @Value("name"):

@Configuration
public class MVCConfig {

    @Autowired
    private ConfigurableEnvironment env;

    @PostConstruct
    public void setup() {
       Properties config = new Properties();  
       try (InputStream stream = this.getClass().getResourceAsStream("/custom.properties")) {
           config.load(stream);
       }
       env.getPropertySources().addLast(new PropertiesPropertySource("mvc", config));
    }
}
  • 1
    Hi @TheDarkDnKTv, welcome to Stack Overflow and thanks for your contribution. I think you may have misunderstood the question, they were looking for how to load the property from an outside source such as a file. So they wanted this part which you commented: /* Property loading code here. You can use default Java */. – CausingUnderflowsEverywhere Jul 28 '21 at 04:34