Posts

JavaEE CDI - Contexts and Dependency Injection

CDI (Contexts and Dependency Injection) is a standard dependency injection framework included in Java EE 6 onwards. CDI is also part of the Eclipse MicroProfile project, a framework use for development of Microservice application. ### CDI Releases | CDI Version | Release Year| |:-----------:|:-----------:| | CDI 1.0 | 2009 | | CDI 1.1 | 2013 | | CDI 1.2 | 2014 | | CDI 2.0 | 2017 | | CDI 3.0 | 2020 | | CDI 4.0 | 2022 | ### New Features - Removal of deprecated code Ex - **@New** qualifier which was deprecated in version 1.1 - Change in Bean discovery Mode CDI 4.0 bean discovery mode is by default Annotated - Support for Java Module system ( introduced in Java v9) - Introducing CDI Lite #### The “beans.xml” File First, we must place a **beans.xml** file in the - **src/main/resources/META-INF/** folder with **bean-discovery-mode** set to ```all``` or ``annotated`` Even if this file doesn't contain any sp...

Microservices using Eclipse Microprofile

Image
## What is MicroProfile ? MicroProfile is enterprose Java for Microservices development. It is an Open source framework. Initial version released in year 2016 with: - CDI - JAX-RS - JSON-P Over the years MicroProfile is evolving to solve the challenges in Microservice development. Given below are few features added to MicroProfile in recent release. - Configuration - Fault Tolerance - JWT Propogation - Health Check - Metrics - Open Tracing - Open API - Rest Client ## Why we need MicroProfile? MicroProfile evolved due to slowdown in JavaEE innovation. JavaEE was not prepared for Microservices development. ## MicroProfile Implementation - RedHat WildFly (https://www.wildfly.org/) - IBM WebSphere Liberty (https://www.ibm.com/products/websphere-liberty) - Open Liberty ( https://openliberty.io) - TomEE (https://tomee.apache.org/) - Payara Micro (https://www.payara.fish/) - ThornTail - Quarkus ( https://quarkus.io/) ### CDI - Contexts and Dependency Injection - Bean...

Executing Store procedure without out parameters and input parameters in Spring

Sometime, we have store procedures which do not have any input and output parameters. We can execute such store procedures using Spring *[SimpleJdbcCall](https://docs.spring.io/spring-framework/docs/current/javadoc-api/org/springframework/jdbc/core/simple/SimpleJdbcCall.html)* class You might get the following exception message: ``` Caused by: org.springframework.dao.InvalidDataAccessApiUsageException: Unable to determine the correct call signature - no procedure/function/signature for 'MY_STORE_PROCEDURE' ``` In such situation, We can bypass checking the signature of store procedure in metadata using method *withoutProcedureColumnMetaDataAccess* ``` SimpleJdbcCall simpleJdbcCall = new SimpleJdbcCall(jdbcTemplate) .withProcedureName("MY_STORE_PROCEDURE") .withoutProcedureColumnMetaDataAccess(); simpleJdbcCall.execute(); ``` By calling the method withoutProcedureColumnMetaDataAccess we are bypassing any processing of the metadata lookups for potential param...

Java Stream Examples

### Problem #1: Given an integer array sorted in increasing order, return an array of the squares of each number sorted in increasing order. ### Solution: ``` public static int[] sortedSquaresArray(int[] numbers) { return Arrays.stream(numbers).boxed() .map(val -> val * val) .sorted() .mapToInt(value -> value.intValue()) .toArray(); } ``` If you want to return the squares of each number sorted in descending order then you can use *.sorted(Collections.reverseOrder())* ### Problem #2: Given ArrayList of Employee (name, age) object. Print the names of employees having age greater than 20 saperated with comma. ### Solution: ``` String str = employeeList.stream() .filter(e -> e.age >20) .map(e -> e.name) .collect(Collectors.joining(",")); System.out.println(str); ``` ### Problem#3: Create a reverse map...

Tricky Java Questions

Image
Java is most popular programming language. It is widely used programming languange and has many job opportunities. Interviewers often ask tricky questions to check the candidate knowldege about Java programming language. Sometimes experienced programmers also fail to answer tricky questions. In this post, I have picked few conceptual questions that will helped you to have better understanding of Java language. #### Q-1: what will be output of following code ? ``` public static void main(String[] args) { Integer number1 = 100; Integer number2 = 100; if (number1 == number2) { System.out.println("number1 == number2"); } else { System.out.println("number1 != number2"); } ``` #### Answer: ``` number1 == number2" ``` We have compared the references of two Integers. The result is surprising !!!. This is because of the autoboxing. Java uses IntegerCache to suppor...