# Review No. 6

Published 2024-01-14

This page contains Java concepts (but not only) briefly explained.

# Application Context vs container in Spring

The Spring container is a place where the Bean are created and lives (concept). Application Context is the Spring implementation of IoC container (ApplicationContext is also an interface).

# How can we shut down the Spring Application Context ?

Methode #1

private ApplicationContext context;
... 
((ConfigurableApplicationContext) context).close();

Methode #2 Using Actuator /shutdown endpoint (if Actuator is used).

Methode #3 Stop the application using System.exit(0); command.

# Are the Beans instantiated eagerly or lazily ?

eagerly - for Singleton

lazily - for Prototype

# BeanFactoryPostProcessor vs BeanPostProcessor

BeanFactoryPostProcessor (interface) - used when we want to modify a class definition just before the Bean is created.

BeanPostProcessor (interface) - used when we want to modify a Bean after the Bean is created.

# What is a proxy object ?

A proxy object is an objects which control the access to the real object it represents.

# What is Environment interface used for ?

An object of this type keeps information about profiles and properties.

# How can we configure a Data Source in Spring ?

  • Java bean configuration : a method must return a DataSource reference type.
@Configuration
public class JpaConfig {

    @Bean
    public DataSource dataSource()
    {
        DataSourceBuilder dataSourceBuilder = DataSourceBuilder.create();
        dataSourceBuilder.driverClassName("org.h2.Driver");
        dataSourceBuilder.url("jdbc:h2:file:C:/temp/test");
        dataSourceBuilder.username("sa");
        dataSourceBuilder.password("");
        return dataSourceBuilder.build();
    }

    //...
}
  • Properties configuration
application.properties
spring.datasource.url=jdbc:postgresql://localhost:5432/my_db
spring.datasource.username=user1
spring.datasource.password=user1_pass
spring.datasource.driver-class-name=org.postgresql.Driver
  • JNDI configuration - the configuration is on the Application Server

# What is a callback ?

A callback is a code or reference to the code that can be passed as an argument to the method. This method will execute passed callback during execution.

# Global vs Local transaction

  • Global transaction -> spans multiple transactional resources (databases, queues)
  • Local transaction -> spans 1 transactional resource (database, queue)

# Is the JDBC template able to participate in an existing transaction ?

JDBC Template is able to participate in existing transaction, in a transaction defined programmatically, or defined with @Transactional annotation.

# How can we access application.properties values ?

We can access it using @Value annotation, using @ConfigurationProperties (@EnableConfigurationProperties enable this feature) or using an Environment object.

# What is a fat jar ?

A fat jar (or uber jar) is an "executable jar". It contains the compiled code for your application and also all the dependencies. The "original" jar is not executable and has no dependencies (= a thin jar).

# What is the MANIFEST.MF file ?

MANIFEST.MF file describes the Java Archive (JAR). It is located in META-INF directory. For an executable jar it contains "Main-Class" and "Class-Path". MANIFEST.MF file is created when the jar file is created.

# Which are the embedded web servers in Spring Boot ?

When we use spring-boot-starter-web starter we can have:

  • Tomcat (the default): the standards, used for small applications.
  • Jetty : better performance for large application.
  • Undertow : consumes less memory and CPU resources.

When we use spring-boot-starter-webflux we can use Reactor Netty (created more for real-time applications).

# What value does Spring Boot Actuator provide?

Spring Boot Actuator provides the following features:

  • Monitoring
  • Health-checks
  • Metrics
  • Audit Events

Spring Boot Actuator supports two protocols: HTTP & JMX.

# Is RESTful API always HTTP?

REST (Representational State Transfer) isn't always linked to HTTP. You can use other transfer protocols, such as FTP, SMTP, etc. But, typically REST APIs employs HTTP.

# What are 3 main characteristics of RESTful APIs?

  • Stateless : The server does not retain any session information about the client between requests => each request must contain all the information required for the server to process the request.

  • uniform interface : The set of interfaces is well-defined (HTTP methods like GET, POST, PUT, DELETE,etc).

  • resource-based architecture : Each call is done on a resource (identified by a unique URI (Uniform Resource Identifier))

# What does CRUD mean ?

CRUD stands for: Create, Read, Update, Delete.

# Which are the main Http response code ?

  • Informational (1xx): Indicates that the request was received and the process is continuing.
  • Successful (2xx): Indicates that the request was successfully received, understood, and accepted.
  • Redirection (3xx): Indicates that further action must be taken to complete the request.
  • Client Errors (4xx): Indicates that an error occurred during the request processing and it is the client who caused the error.
  • Server Errors (5xx): Indicates that an error occurred during request processing but that it was by the server.

# Which libraries could we use in Spring for mocking ?

Mockito, EasyMock

# @RunWith vs @ExtendWith

If you are using JUnit version < 5, you have to use @RunWith(SpringRunner.class) or @RunWith(MockitoJUnitRunner.class) etc.

If you are using JUnit version = 5, you have to use @ExtendWith(SpringExtension.class) or @ExtendWith(MockitoExtension.class) etc.

# What are Inner beans in Spring?

Inner beans in Spring are beans that are defined within the scope of another bean.

# How can you determine if your program has a deadlock?

We can use a thread dump tool to inspect the state of the threads in your program.

# Facade vs Proxy Design Pattern

Proxy wrap an object, Facade hide more objects. Proxy generally add functionality (logging, security, etc), Facade generally will not add functionality.

# ApplicationContext vs BeanFactory

BeanFactory provides basic IoC and Dependency Injection (DI) features, while ApplicationContext provides advanced features like:

  • Internationalization (i18n) -> you can create an application using multiple languages

  • Event publishing -> for instance, you can publish a ContextStartedEvent when the context is started

# What is the use of Interceptor design pattern ?

The use of the Interceptor design pattern is to add actions before or/and after a request/call to an object without modifying that object.

# Which design pattern can be used when to decouple/separate abstraction from the implementation ?

Bridge design pattern

# What is DAO Design Pattern ?

DAO (Data Access Object) Design Pattern is used to separate the data persistence logic from the service which need the persistence.

# Interpreter design pattern in Java

Example
int result = expression.interpret(context);

where,

"expression" is an implementation of an interface (we can have many implementations).

"context" is a context which keeps information used for interpreting.

# What are the different modes of Autowiring supported by Spring ?

by type, by name

# How can we change the heap size of a JVM ?

The HEAP size of a JVM can be changed by setting the -Xms and -Xmx parameters on the command line when we start the JVM.

# Is it allowed to declare main method as final ?

It is allowed, but the main method is not overridden anyway.