Unit-5 Oopj PDF
Unit-5 Oopj PDF
Spring F ramework
1) Predefined Templates
2) Loose Coupling
3) Easy to test
The Dependency Injection makes easier to test the application. The EJB
or Struts application require server to run the application but Spring
framework doesn't require server.
4) Lightweight
5) Fast Development
6) Powerful abstraction
7) Declarative support
formatting.
Basics-Spring Dependency Injection concepts
o tight coupling The dependency lookup approach makes the code tightly
coupled. If resource is changed, we need to perform a lot of modification in
the code.
o Not easy for testing This approach creates a lot of problems while testing
the application especially in black box testing.
1. class Employee{
2. Address address;
3.
4. Employee(Address address){
5. this.address=address;
6. }
7. public void setAddress(Address address){
8. this.address=address;
9. }
10.
11.}
Spring AOP
AOP breaks the program logic into distinct parts (called concerns). It is
used to increase modularity by cross-cutting concerns.
Problem without AOP We can call methods (that maintains log and
sends notification) from the methods starting with m. In such scenario,
we need to write the code in all the 5 methods.
But, if client says in future, I don't have to send notification, you need to
change all the methods. It leads to the maintenance problem.
Solution with AOP We don't have to call methods from the method.
Now we can define the additional concern like maintaining log, sending
notification etc. in the method of a class. Its entry is given in the xml
file.
o Join point
o Advice
o Pointcut
o Introduction
o Target Object
o Aspect
o Interceptor
o AOP Proxy
o Weaving
Join point
Pointcut
Introduction
Interceptor
AOP Proxy
Weaving
AOP Implementations
AOP implementations are provided by:
1. AspectJ
2. Spring AOP
3. JBoss AOP
Singleton and Prototype Bean Scopes in Java Spring
Bean Scopes refers to the lifecycle of Bean that means when the object
of Bean will be instantiated, how long does that object live, and how
many objects will be created for that bean throughout. Basically, it
controls the instance creation of the bean and it is managed by the
spring container.
Bean Scopes in Spring
The spring framework provides five scopes for a bean. We can use
three of them only in the context of web-aware Spring
ApplicationContext and the rest of the two is available for both IoC
container and Spring-MVC container. The following are the
different scopes provided for a bean:
Chat applications
Sending any notification to the user
And many more.
In HTTP client has to request only the response or the payload from
the server will be given and then the connection is terminated. Now if
you require further payload or data then you have to repeat the whole
process.
WebSocket client establishes the socket connection with the server
and receives the message or data without any refresh
The connection won’t be terminated which means that we can
again receive the message.
Also, WebSocket is a bi-directional protocol which means that the
server and client can exchange the message in parallel and it decreases
the round trip and the time for response. An application like trading,
monitoring, or creating functionality like notifications WebSocket is
very useful.
Creating a Chat Application using WebSocket in Spring Boot
Autowiring in Spring
Example of Autowiring
Let's see the simple code to use autowiring in spring. You need to use
autowire attribute of bean element to apply the autowire modes.
1. B.java
2. A.java
3. applicationContext.xml
4. Test.java
Bean life cycle in Java Spring
The lifecycle of any object means when & how it is born, how it
behaves throughout its life, and when & how it dies. Similarly, the
bean life cycle refers to when & how the bean is instantiated, what
action it performs until it lives, and when & how it is destroyed. In this
article, we will discuss the life cycle of the bean.
Bean life cycle is managed by the spring container. When we run the
program then, first of all, the spring container gets started. After that,
the container creates the instance of a bean as per the request, and then
dependencies are injected. And finally, the bean is destroyed when the
spring container is closed. Therefore, if we want to execute some code
on the bean instantiation and just after closing the spring container,
then we can write that code inside the custom init() method and
the destroy() method.
Spring Boot is a project that is built on the top of the Spring Framework.
It provides an easier and faster way to set up, configure, and run both
simple and web-based applications.
o Web Development
o SpringApplication
o Application events and listeners
o Admin features
o Externalized Configuration
o Properties Files
o YAML Support
o Type-safe Configuration
o Logging
o Security
Application Runner
It does not define the standard message exchange format. We can build
REST services with both XML and JSON. JSON is more popular format
with REST. The key abstraction is a resource in REST. A resource can
be anything. It can be accessed through a Uniform Resource Identifier
(URI).
RESTful Service Constraints
o There must be a service producer and service consumer.
o The service is stateless.
o The service result must be cacheable.
o The interface is uniform and exposing resources.
o The service should assume a layered architecture.
Request Parameter
Developer productivity
Battle-tested security
Data access made easy