-
Notifications
You must be signed in to change notification settings - Fork 60
Configuration File Auto loading
Spring has a functionality of loading the parameters from the configuration file to your Java class automatically, whatever the configuration file is an application configuration file or your customized configuration file, or the format is properties (.properties) or YAML (.yml)
In your application configuration properties file, you can define your parameters like this: application.properties
redis.server=127.0.0.1
redis.port=6379
In the Java class, you can use @Component
annotation on class level and use @Value
annotation on field level with the name of the parameter in the properties file.
@Component
public class ServiceConfig{
@Value("${redis.server}")
private String redisServer="";
@Value("${redis.port}")
private String redisPort="";
/* omit other methods */
}
In this case, you don't have to implement the functionality of reading properties files and don't have to write the setters methods of the fields in this class too.
(Need to add)
The only differece is you need to add @PropertySource
annotation to your config class for specifying the location and name of your customized config file. The path can be either an absolute path or relative path with classpath parameter, like:
@Component
@PropertySource("classpath:your_customize_config.properties")
public class ServiceConfig{
@Value("${redis.server}")
private String redisServer="";
@Value("${redis.port}")
private String redisPort="";
/* omit other methods */
}
- Overview
- Getting Started
-
Technical Essentials
- Autowired
- SpringData JPA
- Configuration File Auto-loading
- Configuration Encryption
- Service Discovery with Eureka
- Resiliency Patterns with Hystrix
- Configure Hystrix
- Service Gateway with Zuul
- Zuul Filters
- Protect Service with Spring Security and OAuth2
- Use JWT as Access Token
- Store Clients and Users' Credentials to DB
- Integrate with Message Queue (Kafka)
- Integrate with Redis
- Tune Logging
- Log Aggregation
- Send Trace to Zipkin
- Build Runnable Jar
- Core Application Logic
- Components