Thursday 3 November 2022

SpringBoot

SpringBoot

  • SpringBoot Application : 

    • Pros & Cons : 

  • SpringBoot Application creation using spring.io : 

  • SpringBoot Application Annotations : 

                    @SpringBootApplication : 
                            @SpringBootConfiguration
                            @EnableAutoConfiguration
                            @ComponentScan
                        
  • SpringBoot Application RestApi representation : 

  • SpringBoot Application RestApi with SpringJpa with CRUD operations :

  • Create

  • Insert

  • Update

  • Delete

  • SpringBoot Application RestApi with JPA Repository with Relationships : 

  • One to many

  • Many to one 

  • Many to many

  • One to one



Spring boot Interview Based Questions

Annotations

@SpringBootApplication : will take care of everything

@SpringBootConfiguration : will take about the configurations

@EnableAutoConfiguration : it configures the classpath beans

@ComponentScan : will scan all the classes 

ex : adding mongo dependency in the pom but beans no need to specify, enable auto configuration will take care.


SpringBoot Console Application : 

    


Diff between SpringBootConfiguration vs EnableAutoConfiguration


@Sterotype :

@Service :

@Controller :

@Componet :


@ResponseBody :

@ResponseEntity :


@Lazy :

IOC vs Dependency Injection :

    Earlier we are provind the dependency injection in xml but after spring boot comes its easy to inject the bean using @Autowired.


Diff mvc and spring boot ::
                Springboot                                            SpringMVC
    ==> embeded server                                        ==> need to configure the server
    ==> embeded db                                                ==> need to configure the db
    ==> faster development                                     ==> takes time to develop
    ==> avoid boilerplate code                                

SpringProfiling : using Profile we can instantiate the bean to the particular env.

For dev env we can instantiate the few beans for production we can instantiation using spring profiling.


Retry mechanism : 

@EnableRetry is used to retry the springboot application. It will retry the calls when exception occurs.

@Retryable(value=RuntimeException.class)


What is the starter dependency of the Spring boot module : web starter, Test starter , Data JPA starter.

How can you configure the RestTemplate in Spring not springBoot : 
        ==> ApplicationContext ctx = new AnnotationConfigApplicationContext(RestTemplate.class)
                RestTemplate rs = ctx.getBean(RestTemplate.class);

How can you secure your api in Spring not SpringBoot(SpringWebFilter)
        @EnableWebSecurity and overrride the configure(HttpSecurity http) method

When the beans will destroy in SpringBoot : 

When the beans will initialize : By Starting the springboot tomcat server or applicaiton starting time.

Bean Life Cycle :    
            ==> ContainerStarted --> Bean instantiated --> Dependcies injected --> Init() -> destroy()
    
Spring Actuator Apis : 
            ==> Health check purpose 
            ==>  /health
            ==> / beans
            ==> /caches
            ==> /configProps
            ==> /env
What is thymeleaf?
   
Spring cloud config server : 

GraphQL : Graphql is a query language to retrive the data from servers. Its a alternative to SOAP and REST and GRPC. 
The main steps of creating a GraphQL Java server are:
  1. Defining a GraphQL Schema.
  2. Deciding on how the actual data for a query is fetched
GraphQL offers a solution to both of these problems. It allows the client to specify exactly what data it desires, including from navigating child resources in a single request, and allows for multiple queries in a single request.
It also works in a much more RPC manner, using named queries and mutations, instead of a standard mandatory set of actions. This works to put the control where it belongs, with the API developer specifying what's possible, and the API consumer specifying what's desired.

2 Piller of GraphQL : 
  1. GraphQL Schema
  2. GraphQL Query Language

Thursday 27 October 2022

Email Sending Configuration


Email Configuration


Step 1 : Need Spring Email Dependency 
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-mail</artifactId>
</dependency>
Step 2 : Required Email properties 
Properties props = new Properties ();
props.put ("mail.smtp.auth", Boolean.TRUE);
props.put ("mail.smtp.starttls.enable", Boolean.TRUE);
props.put ("mail.smtp.host", "smtp-mail.outlook.com"); // gmail : smtp-mail.gmail.com
props.put ("mail.smtp.port", "587");

Step 3 : Use below code to send email using springboot application 
try
{
Session session = Session.getInstance(
props, new javax.mail.Authenticator()
{
protected PasswordAuthentication getPasswordAuthentication()
{
return new PasswordAuthentication("user-email", "user-password");
}
})
;
Message msg = new MimeMessage(session);
msg.setFrom(new InternetAddress("user-email", false));

msg.setRecipients(Message.RecipientType.CC,
InternetAddress.parse("user-mails"));

msg.setRecipients(Message.RecipientType.TO,
InternetAddress.parse("user-email"));
msg.setSubject("Email Configuration");
msg.setSentDate(new Date());

MimeBodyPart messageBodyPart = new MimeBodyPart();
messageBodyPart.setContent("MESSAGE_BODY",
MediaType.TEXT_HTML_VALUE);

Multipart multipart = new MimeMultipart();
multipart.addBodyPart(messageBodyPart);

MimeBodyPart attachPart = new MimeBodyPart();
attachPart.attachFile(file);
multipart.addBodyPart(attachPart);
msg.setContent(multipart);
Transport.send(msg);
}
catch (Exception exe)
{
log.error ("error @ Email sending {} ", exe);
}

SpringBoot

SpringBoot SpringBoot Application :  Pros & Cons :  SpringBoot Application creation using spring.io :  SpringBoot Application Annotation...