Spring Core
Spring Core
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.
Constructor Injection
Setter Injection
Field Injection(@Autowired)
Circular Injection
Lookup method injection
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.
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.
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.
@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 uniquely identify the different beans, we should use the @Qualifier annotation along
with @Autowired.
@Component
public class EmployeeService {
@Autowired
@Qualifier("softwareengineer")
private Employee employee;
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)
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
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.
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.
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