Flyer
Flyer

Reputation: 11

How to read external properties file In Spring?

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

Thanks.

Upvotes: 0

Views: 11302

Answers (5)

TheDarkDnKTv
TheDarkDnKTv

Reputation: 23

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));
    }
}

Upvotes: 0

Ed Bighands
Ed Bighands

Reputation: 169

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

Upvotes: 0

neildo
neildo

Reputation: 2386

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"));

Upvotes: 0

Roshini
Roshini

Reputation: 85

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

Upvotes: 1

Olantobi
Olantobi

Reputation: 879

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.

Upvotes: 0

Related Questions