Monday, December 16, 2024

Inversion of control(IOC) and Dependency Injection(DI) in Spring Framework

In this tutorial, we will learn the basic concept of Inversion of control and Dependency Injection.

The core of the spring framework is based on the IOC principle. IOC and DI are closely related concepts. They describe the different aspects of how the spring manages the objects and their interaction. Let's look into each concept.

Inversion of Control(IOC)

IOC is a design principle where the control of object creation, lifecycle and its management is transferred to a spring. Let's see the example of creating two services i.e DatabaseService and UserService

In non-IOC applications, the application code is responsible for creating and managing the DI

DatabaseService dbService = new DatabaseService();
UserService userService = new UserService(dbService);

Here, first, we create the database service object and inject it into user service as a DI resulting in a tight coupling

public class UserService {
    private final DatabaseService dbService;

    public UserService() {
        this.dbService = new DatabaseService(); // Tight coupling
    }
}

With IOC approach

@Component
public class UserService {
    private final DatabaseService dbService;

    @Autowired
    public UserService(DatabaseService dbService) {
        this.dbService = dbService;
    }
}

Here, With IoC, the spring framework itself is responsible for creating and injecting dependencies. Here our application code is not responsible for creating both the object rather spring does. We only provide the metadata @Component and @Autowired to the spring container. UserService doesn't create the DatabaseService but Spring provides it with the help of annotation provided

Dependency Injection(DI)

DI is a design pattern that implements IoC by injecting an object's dependencies at runtime. That simply means it's a mechanism used to achieve IoC. It simply provide the instances of your application classes with all the dependencies they need.

Here are some advantages of using DI

- Loose coupling

DI allows components to be loosely coupled. This makes it easier to modify or replace dependencies without affecting other components.

- Simplify application configuration

DI uses configuration files, such as XML, annotations, or Java-based configuration, to define the dependencies, making it easier to switch configurations without changing the code.

- Improve code maintenance

DI reduces the amount of code to write to wire the components of the application together. As dependencies are managed externally via a Spring's IoC container; it centralizes configuration and makes the application more modular

- Testability

With DI, it's easier to write unit tests for components. Since dependencies can be injected as mock or stub objects.

- Easier Integration with Frameworks and Libraries

Spring's IoC container can manage their dependencies alongside with application's components, enabling cleaner integration and easier management of external resources.

Share:

Thursday, November 14, 2024

How to install oracle database on Ubuntu system with Docker

In this tutorial, we are going to learn how to install the Oracle database on the Ubuntu system.

- Prerequisites

Docker needs to be installed on the system to verify it use the following command

docker --version

This will list the docker version installed

elint@elint:~$ docker --version
Docker version 27.0.2, build 912c1dd

- Download the Oracle docker image

Next, download or clone the oracle docker image from the repo oracle/docker-image Unzip it.

Navigate to the Oracle database docker files; in our case, it's inside /opt/oracle/docker-images/OracleDatabase/SingleInstance/dockerfiles. Here, you can see all the dockerfiles version. Select and navigate the desired version to install

- Download the Oracle database binary file

Download the database zip file from the Oracle website This will require the Oracle login so, create the account login and download the file. Once downloaded copy that file under the Oracle database selected dockerfile version. In our case, we are using docker file version 19.3.0

The overall folder structure looks like below

- Build the Oracle docker image

Note: You need to have at least 8-15Gb storage space on your system

Navigate to the folder

cd /opt/oracle/docker-images/OracleDatabase/SingleInstance/dockerfiles/19.3.0

Using the following command to build

docker build -t oracle/database:19.3.0-ee --build-arg DB_EDITION=ee .

Here oracle/database:19.3.0-ee is the image name, you can give any name

or you can use the following command

../buildContainerImage.sh -v 19.3.0 -e

-v specifies the docker image version and -e for the enterprise edition

Note: this will take some time to build the image

- Run the Oracle container

docker run --name oracle19.3c --network host -p 1521:1521 -p 5500:5500 -v /opt/oracle:/home/oracle oracle/database:19.3.0-ee

This will take some time to complete according to your system's RAM and CPU. Once completed the output looks similar as below

You can close the terminal after this.

- Start the container

To start the container run the below command

docker start oracle19.3c

oracle19.3c is the container name; you can find it using the command: docker ps -a

elint@elint:/opt/oracle/docker-images/OracleDatabase/SingleInstance/dockerfiles/19.3.0$ docker ps -a 
CONTAINER ID   IMAGE                       COMMAND                  CREATED       STATUS                 PORTS     NAMES
767c0a92e087   oracle/database:19.3.0-ee   "/bin/bash -c 'exec …"   11 days ago   Up 11 days (healthy)             oracle19.3c

This process will set the default username and password for the system user to change it, use the command

docker exec oracle19.3c ./setPassword.sh yourPassword

Where oracle19.3c is the container name

Now, login into the container

docker exec -it oracle19.3c bash

To connect to the database use the below command

sqlplus system/yourPassword@orclpdb1

Here orclpdb1 is the default database SID created

If you are having trouble running queries with sqlplus might be due to the export path issue that can resolved by exporting the environment variable. Note: enter into the container first with the command: docker exec -it oracle19.3c bash

export ORACLE_HOME=/opt/oracle/product/19c/dbhome_1
export PATH=$ORACLE_HOME/bin:$PATH

Verify the path for sqlplus

bash-4.2$ cd /opt/oracle/product/19c/dbhome_1 
bash-4.2$ ls
OPatch	 addnode     cfgtoollogs  css  data    deinstall    drdaas   has      instantclient  jdbc  ldap  md	  nls	opmn	     ord  owm	 precomp  relnotes     root.sh.old.1  runInstaller   slax      sqlplus	utl
QOpatch  assistants  clone	  ctx  dbjava  demo	    dv	     hs       inventory      jdk   lib	 mgw	  odbc	oraInst.loc  oss  perl	 racg	  root.sh      root.sh.old.2  schagent.conf  sqlj      srvm	wwg
R	 bin	     crs	  cv   dbs     diagnostics  env.ora  install  javavm	     jlib  log	 network  olap	oracore      oui  plsql  rdbms	  root.sh.old  root.sh.old.3  sdk	     sqlpatch  usm	xdk

Now you can create a new sid/database and connect to the users.

With suitable database connector; you can connect the database using the url: jdbc:oracle:thin:@127.0.0.1:1521/orclpdb1

Share:

Wednesday, November 13, 2024

How to remove docker containers and images

In this tutorial, we are going to learn how to remove the docker containers and images

Initially, list all the containers that use the docker images. To see all the running containers on your machine use the following command

docker ps -a

This will show all the running containers on your machine. The sample output looks like as below:

Here, we do have one running container with container ID: 767c0a92e087 and container name: oracle19.3c

Stop a running container

docker stop <container_id>

Use your own container ID in our case it is 767c0a92e087 to stop it

Now, let's delete the container

docker rm <container_id>

To force to remove the running container in one command use the following command

docker rm -f <container_id>

If you want to remove all the containers(not recommended)

docker container prune

Let's look into deleting the docker images

List all the available docker images

docker image ls

The sample output looks like

elint@elint:~$ docker image ls
REPOSITORY        TAG         IMAGE ID       CREATED       SIZE
oracle/database   19.3.0-ee   dd6c130762a3   11 days ago   6.54GB

To delete the specific image

docker rmi <image_id>

Use your own docker image ID in our case it is dd6c130762a3

To remove the docker image forcefully

docker rmi -f <image_id>

To remove unused images. The unused or dangling images are those images which are not associated with docker images

docker image prune

Note: the deleted images need to re-download if we need it later

These are the overall commands to clean up the docker containers and images.

Share:

Tuesday, November 12, 2024

How to install and start Redis server for Ubuntu system

If the Redis server is not installed on the system install it. For the Ubuntu system use the following command

sudo apt-get update
sudo apt-get install redis-server

Once the Redis server is installed, open the terminal and start it using the following command

redis-server

This will start the Redis server. Note: with this redis server will start on default port 6379.

To customize the configuration create a custom file redis.conf add the proper configuration and start the server

redis-server /etc/redis/redis.conf

The above command to run the server will require the terminal to open so to run in the background use the following command

redis-server --daemonize yes

To test whether the Redis is running successfully ping using the Redis cli command

redis-cli ping

This will return the response PONG if it's running successfully

Share: