Posts

Showing posts from April, 2024

Constructor

  Constructors are used to initialize the object. It has the same name as the class and returns void. When a new keyword is called, it invokes the constructor and the run time creates the object and returns the reference. If the class is inherited from another class, the superclass constructor is called before the current class constructor. We can explicitly call the superclass constructor by calling super(). Copy constructors are special constructors where a new object is created by copying values from the existing one. class HelloWorld extends First { String name = "" ; HelloWorld () { System. out .println( "HelloWorld Called" ) ; } HelloWorld (String name) { super (name) ; this . name = name ; System. out .println( "HelloWorld Called " + name) ; } HelloWorld (HelloWorld obj) { this . name = obj. name ; System. out .println( "HelloWorld obj Called " ) ; } public sta...

Internal working of Spring

Spring is popular for its auto-configuration feature. It means it automatically configures our application based on the added dependencies during the project creation. Beans are stored in IoC container. Spring will look for main class with SpringApplication.run method and annotated with @SpringBootApplication. @SpringBootApplication is the combination of three annotations. @ComponentScan: Also called user configuration which is first step or phase. It detects and registers spring managed components (@Controller, @Service, @Repository, @RestController etc) with Application Context. @ComponentScan without arguments tells the Spring to scan the current package and the sub-packages. @EnableAutoConfiguration: Auto-configuration phase. It enables the Spring to auto-configure the application context. Therefore it automatically creates and registers beans based on jars in the classpath and the beans defined by the user. @Configure: Indicates the annotated class declares one or more beans d...

Call a third party API from Spring Web Application

  To call a third-party API from a Spring web application, you can use various methods depending on your requirements and preferences. Here are a few common approaches: Using RestTemplate: RestTemplate is a synchronous HTTP client provided by Spring, which simplifies consuming RESTful services. You can use RestTemplate to make HTTP requests to the third-party API and handle the responses in your Spring application.              Example:                RestTemplate restTemplate = new RestTemplate();                ResponseEntity<String> response = restTemplate.getForEntity("https://api.example.com/data",                          String.class);                String responseBody = response.getBody();        ...

Java Persistence API

  JPA, or Java Persistence API, is a Java specification for accessing, persisting, and managing data between Java objects and relational databases. It provides a set of interfaces and annotations that define a standard way to map Java objects to database tables and perform CRUD (Create, Read, Update, Delete) operations on those objects. Here are some key concepts and features of JPA: Entity : An entity is a Java class that represents a database table. It is typically annotated with @Entity to mark it as an entity class. Each entity instance corresponds to a row in the database table. EntityManager : EntityManager is an interface that provides methods for interacting with the persistence context, which represents the collection of managed entities. It is responsible for persisting, retrieving, updating, and deleting entities from the database. Persistence Unit : A persistence unit is a set of entity classes that are managed together as a unit. It is defined in a persistence.xml fil...

Spring Data JPA

  Spring Data JPA is a part of the larger Spring Data project, which aims to simplify database access in Spring applications. Specifically, Spring Data JPA provides a powerful abstraction on top of the Java Persistence API (JPA) to facilitate working with relational databases in Spring-based applications. Here's an overview of Spring Data JPA: Repository Abstraction : Spring Data JPA introduces the concept of repositories, which are interfaces that define methods for performing CRUD (Create, Read, Update, Delete) operations on entities. Developers create interfaces that extend the JpaRepository interface provided by Spring Data JPA, and Spring automatically generates the implementation at runtime. Entity Management : Spring Data JPA simplifies entity management by providing support for persisting, retrieving, updating, and deleting JPA entities. Entities are Java objects that represent database records, and they are typically annotated with JPA annotations to define their mapping ...

Hibernate

Hibernate is a powerful, high-performance object-relational mapping (ORM) framework for Java, enabling developers to interact with relational databases in a more object-oriented way. It abstracts the database interactions, allowing developers to focus more on the business logic of their applications rather than dealing with tedious SQL and database connectivity details. Hibernate facilitates the mapping of Java objects to database tables and automates the data persistence process, making it easier to work with relational data in an object-oriented language like Java. Key Features of Hibernate Object-Relational Mapping (ORM): Hibernate automates the mapping between Java classes and database tables. Each class corresponds to a table, and each instance of a class corresponds to a row in the table. This allows developers to work with database data using Java objects, rather than having to write complex SQL queries. Hibernate Query Language (HQL): HQL is an object-oriented query language, s...

Normalization

Normalization is a process used in relational database design to organize data and minimize redundancy, dependency, and inconsistency within a database schema. It involves breaking down large tables into smaller, more manageable tables and establishing relationships between them to reduce data duplication and improve data integrity. The main objectives of normalization are to eliminate data anomalies, improve data integrity, and simplify database maintenance. There are several normal forms in database normalization, each representing a different level of normalization. The most commonly used normal forms are: First Normal Form (1NF): In 1NF, each column in a table contains atomic values, and there are no repeating groups or arrays within the table. Second Normal Form (2NF): In 2NF, the table is in 1NF, and all non-key attributes are fully functionally dependent on the primary key. This eliminates partial dependencies. Third Normal Form (3NF): In 3NF, the table is in 2NF, and all non-ke...

JAXB

  JAXB (Java Architecture for XML Binding) is a Java API that allows Java developers to map Java classes to XML representations and vice versa. It provides a convenient way to serialize Java objects into XML and deserialize XML documents into Java objects. Here are some key features of JAXB: Automatic Mapping : JAXB eliminates the need for manual parsing and generation of XML documents by automatically mapping Java classes to XML schema (XSD) definitions and vice versa. It simplifies the process of converting between Java objects and XML documents. Annotation-Based Mapping : JAXB uses annotations like @XmlRootElement , @XmlElement , @XmlAttribute , and @XmlType to define the mapping between Java classes and XML elements/attributes. These annotations provide fine-grained control over the XML representation of Java objects. Marshalling and Unmarshalling : JAXB provides APIs for marshalling (converting Java objects to XML) and unmarshalling (converting XML to Java objects). Developer...