Spring Core and Context Flashcards

Concepts of Spring DI framework

1
Q

Which interface is used to create spring IOC container?

A

ApplicationContext. There are multiple implementations of ApplicationContext. One needs to choose based on their requirement.

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

Which are common implementations of ApplicationContext

A

ClasspathXMLApplicationContext, FileSystemXmlApplicationContext, AnnoationConfigApplicationContext, XmlWebApplicationContext, AnnotationConfigWebApplicationContext.

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

What are POJO objects called in Spring?

A

Beans

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

Which are mandatory properties for defining a bean in spring xml?

A

Bean id or name, bean class are mandatory properties.

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

Which xml tag is used to inject a dependency in spring bean?

A

is used to inject a bean.

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

Which xml tag is used to inject dependency using constructor?

A

tag is used.

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

What happens if there are two beans of same type and a class wants an instance of that bean?

A

Then spring will fail as it will get confused as to which bean needs to be injected. Then we need @Qualifier annotation.

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

Which xml configuration needs to be enabled to start autowiring using annotation?

A

enables autowiring using annotation.
@Autowired, @PostConstruct, @PreDestroy,
@PersistanceContext (if avaiable, like hibernate), @PersistanceUnit (if available)

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

Which ways are used to do some post processing once all dependencies have been injected?

A

@PostConstruct annotation or using InitializingBean interface or init-method property of tag in xml.

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

Which ways are used to do some pre processing before the context is destroyed and bean is destroyed?

A

@PreDestroy annotation, DisposableBean interface or destroy-method property of tag in xml.

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

When is pre destroy call made?

A

When the application context is closed.

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

How many types of autowiring is supported?

A

ByType, ByName or Constructor

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

Which is default autowiring type?

A

Default mode in java based autowiring is byType.

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

Which is the super class of Spring ApplicationContext?

A

BeanFactory is super of ApplicationContext.

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

What additional features of ApplicationContext?

A

It adds enterprise level features like Internationalization, etc.

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

Which is default autowiring type?

A

Default mode in java based autowiring is byType.

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

Which is the super class of Spring ApplicationContext?

A

BeanFactory is super of ApplicationContext

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

What additional features of ApplicationContext?

A

It adds enterprise level features like Internationalization, etc.

19
Q

Lifecycle of Spring Bean

A

1) Initialize Bean
2) Set dependencies
3) Set Bean factory or ApplicationContext
4) Init method or Post Construct method
5) Bean is used in application
6) Container is destroyed
7) Pre Destroyed method is called

20
Q

In what order will pre destory method of beans be called?

A

TBA

21
Q

How to pass hard coded value in bean property?

A
22
Q

How to get bean from ApplicationContext?

A
ApplicationContext applicatioContext = new ClasspathXmlApplicationContext("");
Type type 
= (Type) applicationContext.getBean("byName");
Type bean = applicationContext.getBean("byName", Type.class);
23
Q

Inner bean

A

Useful if some bean creation should not be allowed from outside directly. It only should be created inside other bean.

Now if you try to create hostel bean from outside you will not be able to create it.

24
Q

Alias for a bean

A
25
Q

How to get bean from ApplicationContext?

A
ApplicationContext applicatioContext = new ClasspathXmlApplicationContext("");
Type type 
= (Type) applicationContext.getBean("byName");
Type bean = applicationContext.getBean("byName", Type.class);
26
Q

How to inject list in bean?

A

A

B

27
Q

Alias for a bean

A
28
Q

What is use of idref?

A

Id ref is useful when you want to inject name of bean in other bean. You could do it with value=”beanName” but that would not guarantee that a bean with the same name exists in container. While id ref performs that check and will fail at runtime if no such bean exists.

29
Q

How to inject map inside a bean?

A

adfas

asdfasd

30
Q

Which method is present in InitializingBean that is used to perform processing after dependencies are set?

A

afterPropertiesSet() method

31
Q

Which method is present in DisposableBean that is used to perform processing before a bean is destoryed?

A

destroy() method

32
Q

Which scope is used to create a new bean instance everytime a dependency is injected?

A

Prototype

33
Q

What will happen if A bean is of singleton scope and its dependency B is of prototype scope?

A

Even though B bean is of prototype scope, it will not be created more than once for A. Because A is of singleton scope and only one instance of A is created.

34
Q

How to achieve A of singleton scope and its dependency B of prototype scope, and we want that every time we ask for A, a new instance of B should be injected in A?

A

Make dependency as proxy, so instead of B, a proxy of B is injected in A. So everytime we get bean A, proxy will have a new instance of B.

35
Q

How to load multiple bean files ?

A

Can achieve by using import tag and its property named as resource

36
Q

How to inherit a bean’s properties in another bean?

A

with the help of parent property of bean tag

37
Q

How to enable annotation based configuration?

A

context:annotation-config

38
Q

what is annotation?

A

Annotation is metadata information which direct compiler that is not part of program

39
Q

How to inject string value using annotation?

A

@Value

40
Q

How to enforce that a property of bean must be populated at configuration time?

A

@Required

41
Q

How to autowire bean if there is multiple same types of bean available?

A

1) @Resource(name=”nameofbean”)

2) @Autowire + @Qualifier(name=”nameofbean”)

42
Q

How to enable auto-discovery of a given bean?

A

context:component-scan.it is a superset of context:annotation-config

43
Q

what are the various ways through which we can annotating bean for autodiscovery

A

@Component,@Controller
@Repository
@Service