1. In a servlet based Spring MVC application, which file is used to set the active profile with a context-param?
Why: In a servlet based application, the active profile can be set by adding a context-param to the web.xml file.
2. Which web.xml entry makes dev the active profile?
Why: The parameter name must be spring.profiles.active. The parameter value is the name of the profile to activate, here dev.
3. The web.xml sets spring.profiles.active to dev. What is the response of a GET request to /?
public interface Greeting {
String msg();
@Service
@Profile("default")
class Local implements Greeting {
public String msg() {
return "local";
}
}
@Service
@Profile("dev")
class Dev implements Greeting {
public String msg() {
return "dev";
}
}
}
@Controller
public class HomeController {
@Autowired
private Greeting greeting;
@GetMapping("/")
@ResponseBody
public String handle() {
return greeting.msg();
}
}
Why: Only the bean with @Profile("dev") is registered because dev is the active profile. The default bean is skipped, so the controller returns dev.
4. Which annotation should be added to this class so that it is used only when the prod profile is active?
@Service
// Add annotation here
public class ProdGreeting
implements Greeting {
public String msg() {
return "prod";
}
}
Why: @Profile marks a bean as available only for the named profile. The bean is registered only when that profile is active.
5. No active profile is set in web.xml. Which profile does Spring use?
Why: When no active profile is set, Spring uses the profile named default. Beans marked with @Profile("default") are registered.
6. The active profile is prod. What happens to a bean marked with @Profile("dev")?
Why: A bean marked with @Profile is registered only when its profile is active. Beans of other profiles are ignored.