KCA-021 UNIT 4 Spring IQ
KCA-021 UNIT 4 Spring IQ
KCA-021 UNIT 4 Spring IQ
Spring framework simplifies the complexity of enterprise applications because it uses Java
beans to implement enterprise applications that were previously possible only with enterprise
beans.
IOC container: It is responsible for instantiating, configuring, and assembling the Spring
beans by reading configuration metadata
MVC framework: that is used to create web applications or RESTful web services,
capable of returning XML/JSON responses
This process is fundamentally the inverse, hence the name Inversion of Control (IoC), of the
bean itself controlling the instantiation or location of its dependencies on its own by using
direct construction of classes, or the Service Locator pattern.
XML-based configuration
Annotation-based configuration
Java-based configuration
We can use the above configurations to implement DI in spring applications. For a better
understanding, please read the https://www.javaguides.net/2018/06/guide-to-
dependency-injection-in-spring.html article.
1. Setter-based Injection
2. Constructor-based Injection
3. Field-based Injection
The spring configuration can be done using XML files or annotations or Java configuration.
Read more about setter-based injection at Spring Dependency Injection via Setter Example.
Read more about constructor-based injection at Spring Dependency Injection via Constructor
Example.
The configuration metadata is represented in XML, Java annotations, or Java code. It lets you
express the objects that compose your application and the rich interdependencies between
those objects.
Read more about Spring IOC container at Spring IOC Container Overview.
Read more about Spring IOC container at Spring IOC Container Overview.
Note that we are supplying configuration metadata via the applicationContext.xml file(XML-
based configuration).
The most used API that implements the BeanFactory is the XmlBeanFactory.
Read more about setter-based injection at Spring Dependency Injection via Setter Example.
Read more about constructor-based injection at Spring Dependency Injection via Constructor
Example.
Which of these DI methods is better purely depends on your scenario and some requirements.
The following best practices may provide a guideline:
In a typical Spring application, you can see dependencies injected using both approaches, but
this depends on the scenario, considering the preceding guidelines.
BeanFactory container
ApplicationContext container
Read more about the BeanFactory interface at Spring BeanFactory Interface Example.
Read more about ApplicationContext at BeanFactory vs ApplicationContext in Spring.
singleton: (Default) Scopes a single bean definition to a single object instance per
Spring IoC container
request: Scopes a single bean definition to the lifecycle of a single HTTP request; that is,
each HTTP request has its own instance of a bean created off the back of a single bean
definition. Only valid in the context of a web-aware Spring ApplicationContext
session: Scopes a single bean definition to the lifecycle of an HTTP Session. Only valid in
the context of a web-aware Spring ApplicationContext
WebSocket: Scopes a single bean definition to the lifecycle of a WebSocket. Only valid
in the context of a web-aware Spring ApplicationContext.
Basically, enterprise applications have some common cross-cutting concerns that are applicable to
different types of Objects and application modules, such as logging, transaction management, data
validation, authentication, etc. In Object-Oriented Programming, the modularity of the application is
achieved by Classes whereas in AOP application modularity is achieved by Aspects and they are
configured to cut across different class methods.
Logging
Security
Transaction management
Auditing,
Caching
Internationalization
Error detection and correction
Memory management
Performance monitoring
Synchronization
21. What are Aspect, Advice, Pointcut,
JointPoint, and Advice Arguments in AOP?
Aspect: Aspect is a class that implements cross-cutting concerns, such as transaction
management. Aspects can be a normal class configured and then configured in the Spring
Bean configuration file or we can use Spring AspectJ support to declare a class as Aspect using
@Aspect annotation.
Advice: Advice is the action taken for a particular join point. In terms of programming, they are
methods that get executed when a specific join point with a matching pointcut is reached in
the application. You can think of Advice as Spring interceptors or Servlet Filters.
Pointcut: Pointcut is regular expression that are matched with join points to determine
whether advice needs to be executed or not. Pointcut uses different kinds of expressions that
are matched with the join points. Spring framework uses the AspectJ pointcut expression
language for determining the join points where advice methods will be applied.
JoinPoint: A join point is a specific point in the application such as method execution,
exception handling, changing object variable values, etc. In Spring AOP a join point is always
the execution of a method.