Api-Ms Unit 1
Api-Ms Unit 1
IoC container:
Refers to the core container that uses the DI or IoC pattern to implicitly
provide an object reference in a class during runtime. This pattern acts
as an alternative to the service locator pattern. The IoC container
contains assembler code that handles the configuration management of
application objects.
The Spring framework provides two packages, namely
org.springframework.beans and org.springframework.context which
helps in providing the functionality of the IoC container.
Transaction management:
Helps in handling transaction management of an application without
affecting its code. This framework provides Java Transaction API (JTA)
for global transactions managed by an application server and local
transactions managed by using the JDBC Hibernate, Java Data Objects
(JDO), or other data access APIs. It enables the developer to model a
wide range of transactions on the basis of Spring’s declarative and
programmatic transaction management.
import com.companyname.projectname.customer.CustomerService;
import com.companyname.projectname.order.OrderService;
@Configuration
public class Application {
@Bean
public CustomerService customerService() {
return new CustomerService();
}
@Bean
public OrderService orderService() {
return new OrderService();
}
}
Bean Configuration:
<beans
xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
</beans>
package com.geeksforgeeks.org;
import com.geeksforgeeks.org.IGeek;
import org.springframework.beans.factory.annotation.Autowired;
This injects the CsvGFG bean into the GFG object using the setter
method (setGeek).
2. Constructor Dependency Injection (CDI):
Constructor DI involves injecting dependencies through constructors. To
configure CDI, the <constructor-arg> tag is used in the bean
configuration file.
package com.geeksforgeeks.org;
import com.geeksforgeeks.org.IGeek;
Bean Configuration:
<beans
xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
</beans>
This injects the CsvGFG bean into the GFG object via the constructor.
Setter DI Constructor DI
Good readability as it
Poor readability as it adds a lot of
is separately present in
boiler plate codes in the application.
the code.
5) Auto Scanning
Spring Component Scanning
Spring 5 auto scanning using @Component annotation and XML
configuration
xsi:schemaLocation="http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-
context.xsd">
</beans>
Whenever Spring container will load the configuration file, it will create
the object of annotated beans.
Note: It is application only for secondary type dependency injection. For
primitive type we have to inject it manually.
@ComponentScan
While developing an application, we need to tell the Spring framework to
look for Spring-managed components. @ComponentScan enables
Spring to scan for things like configurations, controllers, services, and
other components we define.
In particular, the @ComponentScan annotation is used
with @Configuration annotation to specify the package for Spring to
scan for components:
@Configuration
@ComponentScan
public class EmployeeApplication {
public static void main(String[] args) {
ApplicationContext context =
SpringApplication.run(EmployeeApplication.class, args);
// ...
}
}Copy
Alternatively, Spring can also start scanning from the specified package,
which we can define using basePackageClasses() or basePackages(). If
no package is specified, then it considers the package of the class
declaring the @ComponentScan annotation as the starting package:
@Component("employee")
public class Employee {
@EnableAutoConfiguration
The @EnableAutoConfiguration annotation enables Spring Boot to
auto-configure the application context. Therefore, it automatically
creates and registers beans based on both the included jar files in the
classpath and the beans defined by us.
For example, when we define the spring-boot-starter-web dependency in
our classpath, Spring boot auto-configures Tomcat and Spring MVC.
However, this auto-configuration has less precedence in case we define
our own configurations.
The package of the class declaring
the @EnableAutoConfiguration annotation is considered as the default.
Therefore, we should always apply
the @EnableAutoConfiguration annotation in the root package so that
every sub-packages and class can be examined.