Spring Flashcards

You may prefer our related Brainscape-certified flashcards:
1
Q

What is a Spring Bean?

A

a Spring bean is an object which Spring framework manages at runtime. It is the basic building block of any Spring application.

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

What is involved in Spring managing a Spring Bean?

A

Creating an object,
Providing dependencies (e.g. other beans, configuration properties)
Intercepting object method calls to provide additional framework features
Destroying an object

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

How does Spring know what beans to create?

A

Spring has to create beans, you provide the recipe in BEAN DEFINITIONS (which classes to use as beans)

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

How do you define a Spring Bean?

A

1 - @Component on the class (usu. if you have access to the source code)
2 - @Bean in a custom config class (if you don’t own the code)
3 - declare a bean in an XML config file (not common anymore)

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

What are 3 common derivatives of @Component?

A

@Service
@Controller
@Repository
These are all @Component, but with info about what jobs they do

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

You don’t want your class to be dependent on Spring, but you still want Spring to create a bean. What do you do?

A

Use @Bean in a custom config class.

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

What is @Configuration?

A

Also @Component, but it marks the class as the holder of bean definitions

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

What are Spring Bean properties?

A

Properties give details about HOW Spring should create objects. Spring creates default properties, but you can customize.

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

Name some Spring Bean properties.

A
class
name
dependencies
scope
initialization mode
initialization callback
destruction callback
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

Explain Bean class.

A

When you define a bean, you connect to a single concrete class in your application. The class is the main property of the bean.

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

Explain Bean name

A
A custom string that Spring uses to identify beans. Unlike Bean class, bean names are unique across the application. (Spring makes up its own names at runtime by default, so this is optional)
WHY? Mostly for when you have several beans for the same class defined with @Bean. (still pretty rare)
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

Define 2 beans of the same class type

A
@Configuration
class MyConfigurationClass {
   @Bean(name = "myBeanClass")
   MyBeanClass myBeanClass() {
       return new MyBeanClass();
   }
   @Bean(name = "anotherMyBeanClass")
   MyBeanClass anotherMyBeanClass() {
       return new MyBeanClass();
   }
}
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

Explain Bean dependencies

A

Sometimes beans need other beans to do their jobs. Spring needs to create them in the right order (dependencies first), but you don’t have to worry about it as long as you specify the dependencies.

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

How do you define Bean dependencies?

A

If you have a class marked @Component and there is a constructor with parameters, Spring will use the parameters as dependencies.

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

@Autowired on the constructor. When is it not necessary?

A

When there is only one constructor in the class marked with @Component. If you have multiple constructors, you need to mark one with @Autowired so Spring can find the right dependencies.

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

Explain bean scope

A

The scope of a Spring bean defines HOW MANY instances of a particular class the framework creates at runtime (singleton? prototype?). Also, it defines when new instances are created.

17
Q

What is the default bean scope?

A

Singleton. Spring creates only one instance and shares it across the whole application

18
Q

How do you set the scope of a spring bean?

A
Use the @Scope annotation and its string attribute. e.g.
@Component
@Scope("prototype")
class MyPrototypeClass {
    //...
}
19
Q

Spring has scopes just for web applications. What are some examples and how do you define them?

A

request (@RequestScope)
session (@SessionScope)
global session
application (@ApplicationScope)

20
Q

What is bean initialization mode?

A

How Spring creates the beans. Eager = when application starts, Spring eagerly creates all singleton beans at startup (slow).
Or LAZY waits until the bean is needed. Ex:
@Component
@Lazy
class MyLazyClass {
//…
}

21
Q

What is Bean initialization?

A

Spring creates a new instance based on the bean definition, then needs to get it into a usable state. To make sure that the logic happens AFTER initialization, use initialization callback.

22
Q

How do you set bean initialization callback?

A

Make the bean class (marked with @Component) implement InitializingBean. For factory methods (@Bean) use initMethod:

@Bean(initMethod = "someInitMethodName")
MySpringBeanClass meBeanClass() {
   return new MySpringBeanClass();
}
23
Q

What is a bean destruction callback?

A

a method that Spring should call when a bean object is destroyed (not common)
Implement DisposableBean interface, or:

@Bean(name = "myBeanClass", destroyMethod = "cleanUpMethod")
MySpringBeanClass meBeanClass() {
   return new MySpringBeanClass();
}
24
Q

What is the application context?

A

The special object that Spring creates at startup, AKA IoC container. It’s the container where your beans exist. It sniffs out and creates your bean objects for you.

25
Q

How does @Value work?

A
Define a property in application.properties, then inject it into beans by adding @Value to the contructor or bean fields. You can define: 
a property placeholder (${…})
an expression (#{…}).
26
Q

How do you add a default value with @Value (in case the first one is missing?)

A

Define as usual, then colon, default value:

@Value(“${sbpg.init.welcome-message:Hello world}”)

27
Q
Why does this code print null?
@Service
class DontDoItService {
   @Value("${sbpg.init.welcome-message:Hello world}")
   private String message;
   // ...

InitService() {
log.info(message); // prints: null
}
}

A

Because Spring creates the bean from the constructor first, THEN injects values. The value is defined in the field, not the constructor, so it doesn’t exist at the time the bean is created.

Code in the constructor is executed first. The injection happens next.

28
Q

What is Spring Boot?

A

An extension of the Spring Framework with sensible defaults and configurations, but you can also define your own custom configurations.