16. What does the @Bean annotation do?
17. What is the default bean id if you only use @Bean? How can you override this?
18. Why are you not allowed to annotate a final class with @Configuration
19. How do you configure profiles? What are possible use cases where they might be useful?
20. Can you use @Bean together with @Profile?
21. Can you use @Component together with @Profile?
22. How many profiles can you have?
23. How do you inject scalar/literal values into Spring beans?
24. What is Spring Expression Language (SpEL for short)?
25. What is the Environment abstraction in Spring?
26. Where can properties in the environment come from – there are many sources for properties – check the documentation if not sure. Spring Boot adds even more.
27. What can you reference using SpEL?
28. What is the difference between $ and # in @Value expressions?
- 16. What does the @Bean annotation do?
@Bean
public List<Student> studentList(){
return Arrays.asList(new Student("Jack", new Baseball()), new Student("Joe", new Baseball()), new Student("Jason", new Baseball()));
}
- 17. What is the default bean id if you only use @Bean? How can you override this?
@Bean(name={"name", "alias1", "alias2"}, initMethod= "init")
@Scope("prototype")
@Description("Provides a basic example of a bean")
public MyBean getABean(){
...
}
@Bean
public MyBean2 getAnotherBean(){
return new MyBean2(getABean()); //Injecting Inter-bean Dependencies
}
- 18. Why are you not allowed to annotate a final class with @Configuration
- 19. How do you configure profiles? What are possible use cases where they might be useful?
@Component
@Profile("dev", "database") //multiple profiles possible
public class DevelopmentConfig
@Configuration
@Profile("!production") //can be combined with "!" (not)
public class NotForProductionConfig {
@Bean
@Profile
public MyBean getBean(){...}
}
spring.profiles.active=dev, production, <profilesToActive>, ...
context.getEnvironment().setActiveProfiles("dev");
- 20. Can you use @Bean together with @Profile?
- 21. Can you use @Component together with @Profile?
- 22. How many profiles can you have?
- 23. How do you inject scalar/literal values into Spring beans?
public class UmsMemberController {
@Value("${jwt.tokenHeader}")
private String tokenHeader; valuesMap={key1: '1', key2: '2', key3: '3'}@Value("#{${valuesMap}}")
private Map<String, Integer> valuesMap; - 24. What is Spring Expression Language (SpEL for short)?
@Value("#{'helloworld'.toUpperCase()}")- 25. What is the Environment abstraction in Spring?
- 26. Where can properties in the environment come from – there are many sources for properties – check the documentation if not sure. Spring Boot adds even more.
@Configuration
@PropertySources({
@PropertySource("classpath:foo.properties"),
@PropertySource("classpath:bar.properties")
})
@Value("${host.mall.admin}")- Standalone Application:
- Servlet Application:
- Spring Boot Application:
- 27. What can you reference using SpEL?
- public static fields/methods from class:
public class App {
public static String MY_NAME = " XXXX";
public static getMyname(){...}
#{T(xxx.yyy.App).MY_NAME}
#{T(xxx.yyy.App).getMyname()}- Spring Bean Property/method
#{@beanName.propertyName}
#{@beanName.getProperty()}- SpEL Variables:
#{#spelVariableName}- Spring Environment Properties:
#{environment['app.file.property']}- System Properties:
#{systemProperties['app.vm.property']}- System Environment Properties
#{systemEnvironment['JAVA_HOME']}- 28. What is the difference between $ and # in @Value expressions?
#: SpEL expressions (Read Section1.1 Question 27)
No comments:
Post a Comment