In this tutorial, we will learn the usage of @PropertySource and @PropertySources annotations in Spring or Spring boot applications.
@PropertySource and @PropertySources Annotations Overview
Spring @PropertySource annotation is used to provide properties files to Spring Environment.
Spring @PropertySources annotation is used to provide multiple properties files to Spring Environment.
These annotations are used with @Configuration classes.
Spring @PropertySource annotation is repeatable, which means you can have multiple @PropertySource annotations on a Configuration class.
We can use the @Value annotation and Environment class to read the Property file.
@PropertySource and @PropertySources Annotations Example
Create a mail.properties file
Create a mail.properties file under /resources folder in the Spring boot project and add the following content to it:
gmail.host=gmail.com
gmail.email=ramesh@gmail.com
gmail.password=secret
Using @PropertySource annotation
Next, let's use @PropertySource annotation to provide mail.properties file to Spring Environment:
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
@Configuration
@PropertySource("classpath:mail.properties")
public class SpringConfig {
}
Note that @PropertySource annotation is used with @Configuration class.
Next, in order to test this, let's use the @Value annotation and Environment class to read the mail.properties file content from Spring Environment.
Read Property File Using @Value Annotation
Let's use @Value annotation to read the mail.properties file content from Spring Environment:
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
@Component
public class PropertySourceDemo {
@Value("${gmail.host}")
private String host;
@Value("${gmail.email}")
private String email;
@Value("${gmail.password}")
private String password;
public String getHost() {
return host;
}
public String getEmail() {
return email;
}
public String getPassword() {
return password;
}
}
Testing
Let's write the code to test:
import net.javaguides.springannotations.lazy.propertysource.PropertySourceDemo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class SpringAnnotationsApplication {
public static void main(String[] args) {
var context = SpringApplication.run(SpringAnnotationsApplication.class, args);
PropertySourceDemo propertySourceDemo = context.getBean(PropertySourceDemo.class);
System.out.println(propertySourceDemo.getHost());
System.out.println(propertySourceDemo.getEmail());
System.out.println(propertySourceDemo.getPassword());
}
}
Output:
gmail.com
ramesh@gmail.com
secret
Read Property File Using Environment class
Let's use the Environment class to read the mail.properties file content from Spring Environment:import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.env.Environment;
import org.springframework.stereotype.Component;
@Component
public class PropertySourceDemo {
@Autowired
private Environment environment;
//@Value("${gmail.host}")
private String host;
// @Value("${gmail.email}")
private String email;
// @Value("${gmail.password}")
private String password;
public String getHost() {
return environment.getProperty("gmail.host");
}
public String getEmail() {
return environment.getProperty("gmail.email");
}
public String getPassword() {
return environment.getProperty("gmail.password");
}
}
Testing
Let's write the code to test:import net.javaguides.springannotations.lazy.propertysource.PropertySourceDemo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class SpringAnnotationsApplication {
public static void main(String[] args) {
var context = SpringApplication.run(SpringAnnotationsApplication.class, args);
PropertySourceDemo propertySourceDemo = context.getBean(PropertySourceDemo.class);
System.out.println(propertySourceDemo.getHost());
System.out.println(propertySourceDemo.getEmail());
System.out.println(propertySourceDemo.getPassword());
}
}
import net.javaguides.springannotations.lazy.propertysource.PropertySourceDemo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class SpringAnnotationsApplication {
public static void main(String[] args) {
var context = SpringApplication.run(SpringAnnotationsApplication.class, args);
PropertySourceDemo propertySourceDemo = context.getBean(PropertySourceDemo.class);
System.out.println(propertySourceDemo.getHost());
System.out.println(propertySourceDemo.getEmail());
System.out.println(propertySourceDemo.getPassword());
}
}
Output:
gmail.com
ramesh@gmail.com
secret
gmail.com
ramesh@gmail.com
secret
Using Multiple @PropertySource Annotations
Spring @PropertySource annotation is repeatable, which means you can have multiple @PropertySource annotations on a Configuration class.
For example:
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
@Configuration
@PropertySource("classpath:mail.properties")
@PropertySource("classpath:messages.properties")
public class SpringConfig {
}
@PropertySources Annotation
Spring @PropertySources annotation is used to provide multiple properties files to Spring Environment.
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.context.annotation.PropertySources;
@Configuration
@PropertySources({
@PropertySource("classpath:mail.properties"),
@PropertySource("classpath:messages.properties")
})
public class SpringConfig { }
Conclusion
In this tutorial, we discussed the usage of @PropertySource and @PropertySources annotations in Spring or Spring boot applications.
Related Spring and Spring Boot Annotations
- Spring Boot @Bean Annotation Example
- Spring @Qualifier Annotation Example
- Spring @Autowired Annotation with Example
- Spring @Bean Annotation with Example
- Spring @Configuration Annotation with Example
- Spring @PropertySource Annotation with Example
- Spring @Import Annotation with Example
- Spring @ImportResource Annotation Example
- Spring - @Lazy Annotation Example
- Spring - @Primary Annotation Example
- Spring @PostConstruct and @PreDestroy Example
- Spring @Repository Annotation
- Spring @Service Annotation
- The Spring @Controller and @RestController Annotations
- Spring Boot @Component, @Controller, @Repository and @Service
- Spring @Scope annotation with Prototype Scope Example
- Spring @Scope annotation with Singleton Scope Example
- Spring Boot @PathVariable
- Spring Boot @ResponseBody
- Spring @RequestBody - Binding Method Parameters to Request Body
- Spring Boot @ResponseStatus Annotation
- Spring Boot - Creating Asynchronous Methods using @Async Annotation
- @SpringBootTest Spring Boot Example
- @SpringBootTest vs @WebMvcTest
- @DataJpaTest Spring Boot Example
- Spring @PostConstruct and @PreDestroy Example
- Spring @GetMapping, @PostMapping, @PutMapping, @DeleteMapping and @PatchMapping
- Spring Boot @EnableAutoConfiguration Annotation with Example
- Spring Boot @SpringBootApplication Annotation with Example
Free Spring Boot Tutorial | Full In-depth Course | Learn Spring Boot in 10 Hours
Watch this course on YouTube at Spring Boot Tutorial | Fee 10 Hours Full Course
Firstly l am so thankful to you. You are doing such great work for us. you are really rendering service to those who are really needed it .by giving a lot of important information in less time. Thanks a lot for sharing your extravagant precious knowledge with us.
ReplyDeleteEnt clinic in Gurdaspur
You are well equipped with your intelligence and know how to impart knowledge to others. Your way of explanation is very easy to understand. Keep doing like this and thanks for sharing your efforts in the form of our benefits.
ReplyDeleteBest ENT Hospital in Faizabad