0% found this document useful (0 votes)
0 views

Spring Core

Spring is a lightweight framework for building loosely coupled enterprise applications, providing APIs for various operations such as database access and web services. It utilizes dependency injection (DI) and inversion of control (IOC) to manage application components, with various bean scopes including singleton and prototype. Key features include predefined templates, loose coupling, and powerful abstractions, making development faster and easier.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
0 views

Spring Core

Spring is a lightweight framework for building loosely coupled enterprise applications, providing APIs for various operations such as database access and web services. It utilizes dependency injection (DI) and inversion of control (IOC) to manage application components, with various bean scopes including singleton and prototype. Key features include predefined templates, loose coupling, and powerful abstractions, making development faster and easier.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

1: What is spring?

 Spring is a framework which is used to making light weight, loosing coupled


Enterprise Application.
 Spring provides pre-defined API for Database Operations, Restful web services,
microservices and security.
 We can develop distributed backend Application very Fastly. Mainly we can develop
backend Application using Java.
 There are multiple modules of spring like Spring Core, Spring MVC, Spring AOP,
Spring Security, Spring ORM, Spring JDBC etc.

2: What are the features of spring Framework?


Spring provides:
1-Predefined Template
2-Loose Coupling
3-Light Weight
4-Easy to test
5-Powerful Abstraction
6-Fast development

3: What is DI?

 DI is a mechanism which is used to inject the dependency class into dependent class.
 Basically, when we write any program sometimes it's needed to create the object of
some class, so dependency injection helped us to create object creation process, it
will do that work automatically by own.
 For achieving this functionality, we have only configured that particular dependency
class to dependent class in XML file or we can use Annotation also.

4: What are the types of DI?


Mainly there are 5 types of DI

 Constructor Injection
 Setter Injection
 Field Injection(@Autowired)
 Circular Injection
 Lookup method injection

5: What is IOC in spring?

 The Full of IOC is Inversion of Control.


 Basically, the spring container uses DI for managing the application components by
creating objects wiring them together along with configuring and managing their
overall life cycles.
 The instruction for the spring container to do the tasks will be provided either by
XML configuration, Annotation of the code, this whole process called as IOC.
6: What is spring Configuration file?
 It is XML file which is contains the information of classes and describes how those classes
configured and linked with each other.
 It is called as XML Configuration file.
Sample Code looks like:
<bean id="on1" class="com.app.Empse"?
<p name ="empType">
<Value>export</value>

7: What is Spring Bean?

 It is the object forming the Backbones of the users Application and it instantiated,
configured, wired, Managed by Spring IOC Container.
 Basically, it will create by the help of Configuration code which is supply by user to
IOC Container.

8: Diff b/w CI and SI?

 By the CI, partial injection is not possible but we can achieve Partial injection by
setter injection. (The class has 4 variables in the time of object creation we need pass
all variable details in Ci but if some of are not required you can use SI?
 In the construction Injection new instance will create if any modification but it is not
possible in the setter injection, we can’t achieve any new instance.
 If the bean has many properties, then we preferred constructor injection and if bean
have some few properties, then we preferred for setter injection.
 CI doesn’t override the setter properties but SI override the Constructor properties.

09:What is the default scope of bean in spring?


Ans-There are multiple scope of bean in spring. These are Singleton, Request, Session,
Prototype.
 Singleton: Only one instance will be created for a single bean definition per Spring
IoC container and the same object will be shared for each request made for that
bean.
 Prototype: A new instance will be created for a single bean definition every time a
request is made for that bean.
 Request: A new instance will be created for a single bean definition every time an
HTTP request is made for that bean. But only valid in the context of a web-aware
Spring Application Context.
 Session: Scopes a single bean definition to the lifecycle of an HTTP Session. But only
valid in the context of a web-aware Spring Application Context.
 Singleton is default type scope of bean.

10-What is the diff b/w singleton and prototype?


Ans- These are the Scope of the bean.
 Singleton: When you used singleton only one instance will create.
 Prototype: When you used prototype multiple instances will create.

11-Can you tell me any hypothetical scenario where you need prototype
scope?
Ans: As a rule of thumb, you should use the prototype scope for all beans that are
stateful, while the singleton scope should be used for stateless beans.

12-What is the diff between setter injection and constructor injection?


 Partial dependency: can be injected using setter injection but it is not possible by
constructor. Suppose there are 3 properties in a class, having 3 Arg constructor and
setters’ methods. In such case, if you want to pass information for only one property,
it is possible by setter method only.
 Overriding: Setter injection overrides the constructor injection. If we use both
constructor and setter injection, IOC container will use the setter injection.
 Changes: We can easily change the value by setter injection. It doesn't create a new
bean instance always like constructor. So, setter injection is flexible than constructor
injection.

13-What is the diff b/w @Autowired and @Qulifier?


Both @Autowired and @Qualifier are two different methods of Auto-wire annotations that
are used to achieve dependency injection in Spring.
- You can use @Qualifier along with @Autowire to help Spring Framework find the
right bean to Auto-wire. Spring Framework will ask you explicitly select the bean if
ambiguous bean types are found, in which case you should provide the qualifier.
The @Qualifier annotation can be used on any class annotated with @Component or on any
methods annotated with @Bean annotations. It can also be applied to constructor
arguments and method parameters.

@Autowired and @Qualifier Annotation Example in Spring Framework


Now, let's see an example of how to use @Autowired and @Qualifier annotation
in Spring Framework. Assuming, there Employee interface has two methods
with calculateSalary () and calculateDeductions ().

public interface Employee {

public void calculateSalary ();


public void calculateDeductions();
}
There are two beans, Software Engineer and Quality Assurance Engineer, implement the Employee
interface.

@Component (value = "softwareengineer")


public class SoftwareEngineer implements Employee {
@Override public void calculateSalary() {
System.out.println("Calculate Software Engineer Salary");
}
@Override public void calculateeDeductions() {
System.out.println("Calculate total salary deduction of software
Engineer");
}}

@Component (value = " qaengineer ")


public class QAEngineer implements Employee {

@Override
public void calculateSalary() {
System.out.println("Calculate Quality Assurance Engineer Salary");
}
@Override
public void calculateDeductions() {
System.out.println("Calculate total salary deduction of
Quality Assurance Engineer");
}
}

So as from the injecting bean in EmployeeService using @Autowired with @Qualifier annotation. If
you don't use @Qualifier annotations then it will throw the
NoUniqueBeanDefinitionException because Spring doesn't know which bean should autowire.

To avoid this confusion or exception, we should use @Qualifier


annotation.

To uniquely identify the different beans, we should use the @Qualifier annotation along
with @Autowired.

@Component
public class EmployeeService {

@Autowired
@Qualifier("softwareengineer")
private Employee employee;

public void service () {


employee.calculateSalary();
employee.calculateDeductions();
}
}

Now, let's compare different features of @Autowired and @Qualifier in Spring Framework.
The @Autowired annotation can be used alone. If it is used alone, it will be wired by type.
So, problems arise if more than one bean of the same type is declared in the container
as @Autowired does not know which beans to use to inject.
As a result, use @Qualifier together with @Autowired to clarify which beans to be wired by
specifying the bean name
14: What are the Bean Scope Spring provided?
 Singleton: (The container will create one object for our conf in singleton)
 Prototype: (Every time new object will create)
 Request: (it is basically used in web application, when any request come from any
client that time object will be create once response is done object will be destroy)
 Session (When browser is login that time this object will create once logout that
object will destroy)

15: What is bean wiring?

 When the beans are combined together inside the spring Container, that is called
Wired on Bean wiring.
 The spring container should know what beans are needed and how the beans are
dependent each other while wiring beans.
 Ex: Combine EmpService object with EmpRepo object

16: What is Autowiring in spring?


 In spring by using @Autowire annotation we are doing Autowire.

17: What is the difference b/w @Autowired and @inject?


 Both are used for same purpose but @Autowired is given by spring framework and @inject
given by java CDI and it is used for non-spring framework.

18: What is the diff b/w @componet and @Bean


 @Componet creates object inside spring container. it can be used only if we have source
code of a class.
 @Bean is used for pre-defined java class which we can’t modify.

19: How many types of IOC in spring?


There are two types of containers these are-
 Bean factory
 Application Context

20: What is the Use of @Required Annotation in spring framework?

 @Required is applied to bean property setter method.


 If you are using @Required annotation top of any variable, we most need to pass values of
that annotation.

21: What is the use of @Autowired Annotation in spring?


 It is used to link tow beans inside spring container.
 Mostly recommended using Field injection.
22: What is the use of @Qualifer annotation in spring Framework?

 When we create more than one bean of the same type and want to wire only one of
them with a property, we can use the @Qualifer annotation along with @Autowired
to remove the ambiguity by specify which exact bean should be wired.

23: What are the lifecycle methods in spring?

o There are two lifecycle methos in spring init and destroy method.
o They are executed by default by spring container while creating object:

 The order is like creating the object, providing the data by using setter injection,
calling init() method in the middle destroy() method will called to destroy the object.

24: What are the Stereo Type annotations?


 @component
 @Service
 @Repository
 @Controller
 @RestController

20: What are profiles in spring?

In real time we are using multiple env like dev, qa, UAT, prod

Every time when env will change we must need to change the code also to avoid extra time
and code we need to use profile

Example: pofile= QA/UAT

21: What is Spring Framework?

 Spring framework is a dependency injection framework to make java application


loosely coupled.
 Spring framework makes the easy development of JavaEE application.

You might also like