14-MoreOnConfiguration Flashcards

1
Q

Ways to read a property from the application.properties

A
  1. Using @Value(“${propertyName}”)
  2. @Autowired Environment env;
    then: env.getProperty(“propertyName”);
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

How to use @Value to set a default value if property not found?

A

@Value(“${propertyName: default Value}”)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What properties are loaded into Spring Environment?

A
  1. Environment variables i.e. system environment variables
  2. Java system properties
  3. Additional resources added by:
    @Configuration
    @PropertySource(“classpath:/com/app.properties”)
    @PropertySource(“file:/config/local.properties”)
    class Config {}
  4. ## Properties configured with @TestPropertySource will have higher precedence than those loaded from the OS’s env or Java system propertiesNote that if the same property is defined in both files, the value in the last one seen wins
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

What class does evaluate @Value expression?

A

PropertySourcesPlaceHolderConfigurer

It extends BFPP (BeanFactoryPostProcessor)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

How to declare a BFPP bean method?

A

BFPP bean method must be static so that it can load before all other beans e.g.
@Configuration
class Config {
@Bean
public static PropertySourcesPlaceHolderConfigurer method() {}
}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

What happens if using @Profile annotation on configuration class?

A

Everything (e.g. bean) in configuration belongs to the profile

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

How to enable a bean excepts 1 profile?

A

@Profile(“!profileName”)
@Bean
BeanType method() {}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

Ways to activate some profiles?

A
  1. -Dspring.profiles.active=p1,p2
  2. System.setProperty(“spring.profiles.active”, “p1,p2”)
  3. In application.properties
    spring.profiles.active=p1,p2
  4. Create applicationContext manually then set:
    applicationContext.getEnvironment().setActiveProfiles(“p1,p2”);
  5. In test
    @ActiveProfiles(“p1,p2”)
    class TestClass {}
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

Ways to create an ApplicationContext manually

A
  1. new ClassPathXmlApplicationContext(“config.xml”)
  2. ## new AnnotationConfigApplicationContext(ConfigClass.class)Note: WebApplicationContext is an interface
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

How to control property files loaded using @Profile?

A

@Configuration
@Profile(“local”)
@PropertyResouce(“file.properties”)
class Config {}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

Expressions language implemented in Spring

A
  1. SpEL (start with #)

2. Unified Expression Language used by JSP and JSF (start with $)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

How to set (daily.limit * 2) using @Value?

A
Must use SpEL, not work for Unified EL
e.g. @Value("#{new Integer(environment["daily.limit"] * 2}")
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

Ways to set fall-back value using @Value?

A
  1. SpEL @Value(“#{environment[‘daily.limit’] ?: 100000}”)

2. Unified EL @Value(“${daily.limit : 100000}”)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

List out default implicit references in SpEL?

A
  1. environment
  2. systemProperties
  3. systemEnvironment
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

What do SpEL expressions starting with $ reference?

A

Properties in the application environment

–> seem they mean syntax of Unified Expression Language is also supported in SpEL

How well did you know this?
1
Not at all
2
3
4
5
Perfectly