Skip to main content

What is @SpringBootApplication ?

This annotation used at start class of spring boot.

-> This annotation is equal to below 3 annotations.

1) @SpringBootConfiguration

2) @EnableAutoConfiguration

3) @ComponentScan

What is ComponentScan ?

+++++++++++++++++

-> The process of scanning packages in the application to identify spring bean classes is called as Component Scan.

-> Component Scan is built-in concept.

-> Component Scanning will start from base package.

Note: The package which contains start class is called as base package.

-> After base package scanning completed it will scan sub packages of base package.

Note: The package names which are starting with base package name are called as sub packages.

in.dshub    (base package)

in.dshub.dao

in.dshub.service

in.dshub.config

in.dshub.rest

in.dshub.util

com.wallmart.security ----> This is not sub package so scanning will not happen

-> We can specify more than one base package also in our boot start class.

Note: It is highly recommended to follow proper package naming conventions.

Ex:   companyDomainName.projectName.moduleName.layerName

com.tcs.passport   (basePkgName)

com.tcs.passport.user.dao

com.tcs.passport.user.service

What is @Bean annotation

++++++++++++++++++

-> @Bean is a method level annotation.

-> When we want to customize any object creation then we will use @Bean annotation for that.

@Configuration

public class AppConfig {

@Bean

public AppSecurity createInstance() {

AppSecurity as = new AppSecurity();

// custom logic to secure our functionality

return as;

}    }

How to represent java class as a Spring Bean?

+++++++++++++++++++++++++++++++++

@Component

@Service

@Repository

@Controller

@RestController

@Configuration

@Bean

Note: All the above annotations are class level annotations but  @Bean is method level annotation.

-> @Component is a general purpose annotation to represent java class as Spring Bean

-> @Service annotation is a specialization of @Component annotation. This is also used to represent java class as spring bean. It is allowing for implementation classes to be autodetected through classpath scanning.

Note: For business classes we will use @Service annotation.

-> @Repository annotation is a specialization of @Component annotaton. This is also used to represent java class as spring bean. It is having Data Access Exception translation

Note: For dao classes we will use @Repository

-> In Web applications to represent java class as controller we will use @Controller annotation. It is used for C2B communication.

-> In Distributed application to represent java class as distributed component we will use @RestController annotation. It is used for B2B communication.

-> If we want to perform customized configurations then we will use @Configuration annotation along with @Bean methods. Based on requirement we can have multiple @Configuration classes in the project.

Ex: Swagger Config, DB Config, RestTemplate Config, Kafka Config, Redis Config, Security Config etc...

Note: @Bean annotated method we can keep in any spring bean class but it is highly recommended to keep them in @Configuration classes. 



Comments

© 2020 The JavaDS Hub

Designed by Open Themes & Nahuatl.mx.