Posts

Advanced wiring in Spring Part-2

Image
In the last article we have seen that how we can configure beans based on certain environments.This is quite useful features spring have provided.Sometimes we need more granular control over the bean autowiring in spring, such as we need certain beans that needs to be autowired or injected on some complex condition (which is something more than certain environment). for e.g. What if we need certain bean needs to be injected if some other bean have specific property value. or inject an bean only if some other class is found on class-path. Here Spring will help you to make such a configuration using @Conditional annotation. Suppose we want the bean OperationUser to be created if the property " department= Operations " is set in your properties file. @Bean @Conditional(OpsDepartmentCondition.class) public OperationsUser operationsUser(){ OpeartionsUser opsUser = new OperationsUser(); return opsUser; } We will take a look at how OpsDepartmentCondition will

Advanced wiring in Spring

Image
Many times we face situation in which we want certain things configured as per the environment or on certain complex condition. Spring provides great features in easing the developer job for this use-case. We will look at such a scenario along with solution spring provides in this case. Environments and Profiles: One of the most challenging part of any application is to transitioning of the software into different environments (DEV, QA, STG, PROD). Certain choices that we make in DEV environments are not suitable when we run the same setup on PROD environment. Consider basic use of using different database as per the deployment environment. for DEV we might setup a datasource something like below: @Bean(destroyMethod = "shutdown") public DataSource dataSource() { return new EmbeddedDatabaseBuilder() .addScript("classpath:devCreate.sql") .addScript("classpath:devInsert.sql") .build(); } but when we will go on PROD environment, this c

Docker fundamentals

Image
Nowadays everybody is talking about Docker. Some people are well versed with docker concepts and have hands on it and some have started learning docker. So what is Docker?. As per the official docs from docker, Docker is a platform for developers and sysadmins to develop , deploy , and run applications with containers. So what does make it different. We were already doing that since long . When we are deploying application on servers we have to deal with setting up new VM , configuring dependencies / libraries, environment variables. Docker makes this thing easy for you, by providing OS level virtualization (aka. containerization). So basically you can create Docker images that will have all your resources ( env. variables / libs / running on what port). Then you can deploy your application inside docker container anywhere without even worrying about the dependencies. In traditional way you have to setup separate VM and then install all the required software and have

Spring RestTemplate : Follow Redirect Automatically

Follow Redirect Automatically in Spring RestTemplate.... Now since the boost of micro-service based architecture we use web-services a lot. Spring boot is one of the popular framework to work on Micro-service based architecture. Soap web-services are legacy swords and REST( Representational State transfer ) is now the modern time weapon in everybody's arsenal. When we use REST with Spring we have lot of inbuilt functionalities (Converting Response and request to Domain objects, Error handling, HttpStatus code and many more...) offered by Spring. Today we are going to check about RestTemplate. Spring's central class for synchronous client-side HTTP access. This is what Spring says about RestTemplate. RestTemplate provides support to all HTTP Methods. Most popular are GET and POST. So when we are calling any webservice endpoint using RestTemplate GET or POST there are chances that in response you get Redirect response (302 HTTP Status code). Which indicates that this

JShell : Test your logic right away

Image
You might have heard about Java version 9 release and lot of buzzwords related to the new features that are going to unleashed from Oracle. New Module system(jigsaw) , Jlink and more tools are introduced. Today we will talk about one of the tool(aka. feature) which provides functionality that you might have used when you have used javascript and other languages... So, What it is .... REPL (Read-Evaluate-Print-Loop). So as you know, when you are writing software all these years, and at any point of time if you want to just execute some random code, some logic that you think it can work, you go to the command line shell (any shell that helps you to execute that language commands) and just right it there and execute..that's it.. Just check out the below features from your browser tools console executing javascript code (REPL feature). Python, Ruby, Haskell, Tcl also have  REPL features. Unfortunately java was lacking REPL feature from long time..and with the release o

Solve java.security.InvalidKeyException: Illegal key size or default parameters

Image
Solve java.security.InvalidKeyException: Illegal key size or default parameters When first time I was working on AES 256 bit encyrption, I faced above exception. I tried everything and was frustrated what is the main cause. Then I reasearhed little bit on this, and found this has to do with " Java Cryptography Extension (JCE) Unlimited Strength Policy Files" . So, What is  JCE Unlimited Strength Policy ? So when you install JDK / JRE on your machine a normal version of JCE policy jars is included which doesn't support higher bit (256 in my case) encryption / decryption. The reason behind this is, US has restrictions on the export of cryptographic technology: https://en.wikipedia.org/wiki/Export_of_cryptography_from_the_United_States They used to be very strict -- cryptography was classified as munitions, and you can only download the full strength products from the US and other white-listed countries. Restrictions have eased up a lot since then,

Why Spring

Today we will talk about Spring framework. Though you will find numerous websites and blogging available on internet regarding spring, I am going to put some more insight on why spring was needed and how the spring framework works. The basic motive of spring development was dependency injection and simplified programming model using java-beans (simple POJO). So what is mean by dependency injection.?     Every class will define there dependency. And the spring container will provide you the same.     for eg. You went to restaurant , and asked for a coffee, burgers. Now there are two types how the order will be delivered,         1. Self service. (Service Locator Pattern)         2. Service by the Restaurant staff. (Dependency injection)         1. The Service Locator Pattern:          Now in the above example the Restaurant is the Spring application container who provide all types of services. You ask the Spring application container for one service and look for it yourself.