Spring Core Framework
Spring is a popular framework in Java Enterprise Edition.Spring can be thought of as a framework of frameworks,because it provides support to other frameworks as well such as Hibernate,JPA etc
Some of the key and core features of spring core are as belows:
Dependency Injection
Dependency Injection provides Objects which an object needs.Dependency injection generally means passing a dependent object as a parameter to a method, rather than having the method create the dependent object.Instead of making the objects tightly coupled on other objects, an initiative to make objects loosely coupled.
Conventionally,we create objects using the new
operator.
class Employee {
private int id;
private String name;
private Address address;
}
}
class Address {
private String street;
private String city;
}
}
In this case Address
class values are only set when the class Employee
is instantiated,making it a tight coupling, spring resolves this type of tight coupling with the help of Dependency Injection in two ways, Setter Injection
and Constructor Injection
.This is achieved in spring by IOC.
In spring dependency injection is of two types, XML based and annotaiton based.
Inversion of Control
Spring IoC container is the core of spring framework.It creates objects,configures and assemble their dependencies,manage entire life cycle of each object.
-
Information about the object’s configuration is done by the configuration file(XML),java annotations or java code,these objects are called
Beans
. -
Spring IoC internally uses Dependency Injection to manage object life cycle.
-
Since the control of these objects are not in the hands of developers hence the name,
Inversion of Control
. -
There are two types of IoC containers in Spring
- Bean Factory
- Application Context
-
Bean Factory is the basic version of IoC container,while ApplicationContext extends the features of BeanFactory.
-
Some of the main features of IoC containers.
- Creating Objects
- Managing dependencies
- Application Configuration
- Managing Objects
Constructor Injection & Setter Injection
In constructor injection dependencies are passed as parameter to the class’s constructor.In this way all the dependencie’s objects are created during the objection creation,say for example there is a class called Employee
with another object Address
as its dependency,along with other attributes.
The Address
class object is created whenever the Employee
class object is instantiated.
@Autowired
annotation uses constructor Injection.
In Setter Injection dependencies are injected with the help of getters and setters
Autowiring
Autowiring in spring framework is used to automatically inject dependencies.Spring IoC container detects the dependencies mentioned in the cofiguration file and the realtionship between the beans,this is known as autowiring.
@Autowired
annotation is used,autowiring in spring internally uses constructor injection.
Common Annotations
-
@Configuration
: Used to declare a class with one or more@Bean
methods.These classes are processed by the spring container to generate bean definitions. -
@Bean
: Indicates that a method produces a bean to be managed by the Spring container. -
@ComponentScan
: is used along with @Configuration annotation to specify the packages that we want to be scanned.If no arguments it scans the current package and all of its sub packages. -
@Component
: is a class level annotation,used to denote a class as component.This annotation is used to mark the beans as Spring managed components.- @Service - Annotated class is a service,specialization of
@Component
, used for business logic or services in it. - @Repository - dealing with CRUD operations,mostly deals with DAO or repositories that deal with databases.
- @Controller - front controllers,responsible to handle user requests and for returning appropriate responses to user.
- @Service - Annotated class is a service,specialization of
-
@Autowired
: is used for automatic injection of beans.
Spring MVC Annotations
-
@Controller
or@RestController
components use annotation to express request mappings,result input,exception handling and more. -
@RequestMapping
: class level or method level annotation in controller,class level annotation maps a specific request path onto a controller,used to handle web requests, -
@PathVariable
: handle template variables in request URI mapping. -
@RequestParam
: to read form data and bind it automatically to the parameter present in the provided method. -
@RequestBody
: RequestBody is mainly used with CRUD operations to read request body. -
@ResponseBody
: is used with GET methods to write the response body content.
Spring Annotations.
-
@SpringBootApplication
: Many spring boot developers like their apps to use Auto Configuration,component scan and be able define extra configuration on their application class.- @EnableAutoConfiguration: enables spring boot's auto configuration - @ComponentScan : enable component scan on the package where the application is located. - @Configuration : allow to register extra beans
-
@EnableAutoConfiguration
: attempts to automatically configure your spring application based on the dependencies.
Spring Data JPA Annotations
-
@Transactional
: class or method level annotation,Propagation Type
of the transaction,Isolation Level
of the transaction,Timeout
for the transaction,Rollback
rules for transactions. -
@Id
: marks field in model class as primary key. -
@Query
: Native SQL queries & JPQL implementation. -
@Lock
: We can configure Lock Mode when we execute a repository query method. Available lock modes are : - READ - WRITE - OPTIMISTIC - OPTIMISTIC_FORCE_INCREMENT - PESSIMISTIC_READ - PESSIMISTIC_WRITE - PESSIMISTIC_FORCE_INCREMENT - NONE