Q1. What is a property placeholder in Spring?
Property placeholder allows you to externalize configuration properties from bean definitions.
You define properties in a
Spring resolves them using a
You define properties in a
.properties file and use placeholders like ${db.url} in XML or @Value annotations.Spring resolves them using a
PropertySourcesPlaceholderConfigurer.Q2. How do you use @Value to inject properties?
Example:
@Value can inject property values into fields, constructor parameters, or methods.Example:
@Value("${app.name}")
private String appName;
You need to register a PropertySourcesPlaceholderConfigurer (or use <context:property-placeholder> in XML).Q3. What are Spring profiles?
Profiles allow you to register beans conditionally based on the active environment.
For example, you can have different datasource configurations for development, testing, and production.
Use
For example, you can have different datasource configurations for development, testing, and production.
Use
@Profile on @Bean or @Component to specify which profile the bean belongs to.Q4. How do you activate a profile?
You can set the active profile programmatically (via
In Spring Boot, you can also use
ConfigurableEnvironment) or by setting the spring.profiles.active system property (e.g., -Dspring.profiles.active=dev).In Spring Boot, you can also use
application.properties: spring.profiles.active=dev.Q5. What is the use of @PropertySource?
Example:
@PropertySource is used to specify the location of property files to be loaded into the Spring Environment.Example:
@Configuration
@PropertySource("classpath:app.properties")
public class AppConfig { ... }
This makes properties available for injection via @Value.