CDI Dependency Injection @PostConstruct and @PreDestroy Example

We have seen how to inject cdi beans by using qualifiers and producers so far. Additionally, every cdi bean has a lifecycle, and we can initialize and prepare to destroy any managed bean by using two common annotations; @PostConstruct and @PreDestroy. Methods annotated with these annotations are called bean lifecycle callbacks. @PostConstruct Due to the […]

Java Enums Tutorial

Enums (Enumeration) are introduced as a new reference type in Java 1.5 with a bunch of beneficial features. They consist of fix set of constants (each constant is implicitly labeled as public static final) which each constant can have a data-set. Lets see the simplest example of an Enum;

Enums are classes that return […]

CDI Dependency Injection Producer Method Example

In this tutorial we will see how to use producer methods for injecting CDI beans by using @Produces annotation. We will use Weld CDI implementation. Using producer methods provides a programmatic way to solve disambiguation by using injection points. We have already seen how to solve disambiguation by using qualifiers in this tutorial. As a […]

Adapter Design Pattern in Java

Adapter design pattern is a structural design pattern that provides two unrelated interfaces to work together. The most common illustration about adapter design pattern is sockets from different countries. Well most of us faced with laptop adapter issues when traveling, and looked for appropriate adapter for our laptops. So, by using an adapter we are […]

Create Qualifiers for CDI Beans

As I described in my previous post, we can define and inject cdi beans by @Named annotation. Well according to the CDI specification (JSR-299) injecting beans by their names is legacy and tend to cause issues (if a bean is tried to be injected by an undefined/wrong name then we will get errors which are […]

Convert objects to/from JSON by Jackson example

We will see a simple example about converting objects to/from JSON objects in Java by Jackson. First, add maven dependency for jackson.

And two typical Person and Address classes. Person.java

Address.java

Notes @JsonCreator annotation is used for constructors or static factory methods to construct instances from Json. This is called property base […]

Arquillian Example for CDI Dependency Injection

Arquillian is a platform which provides integration tests by deploying, running containers so that we can easily use cdi beans in tests. In this tutorial we will see how to inject and use cdi beans in test classes by running Arquillian. As a first step we have to add Arquillian core library in our pom.xml […]

Java CDI Dependency Injection Example

CDI (Context and Dependency Injection) is a specification defined in JSR-299. Major aim is loose coupling by dependency injection.In this tutorial we will see how to use CDI Dependency Injection in java with three different ways; Field injection Constructor injection Setter method injection We are going to use @Inject alongside @Named annotations from CDI of […]

Graphs

Graphs are data structures to represent road networks, the web, social networks etc. Moreover, hundreds of computational problems are related to graphs.They have two main ingredient which are; as known as nodes. : pair of vertices can be undirected. or directed. When we are talking about of a graph, notation refers to a graph which […]

Quicksort

Quicksort is a sorting algorithm which applies divide and conquer paradigm. Quicksort has a worst case running time of , however, it has running time of on average which makes quicksort very efficient. Moreover, it works in-place but not stable. The performance of quicksort depends on selecting the pivot, and starting to partition around it. […]