Skip to main content

Posts

Showing posts from November, 2015

Spring Security : Getting started with Spring Security and Spring Boot

Points To Remember Add dependency of spring security Add custom username password in application.properties A unique password is generated each time application is started if no authentication process is specified. You can configure your own authentication  providers, managers, filters, entry points, tokens etc as required. Getting started with Spring Security and Spring Boot In order to apply Spring Security to a Spring Boot application, firstly you need to add the dependency in the application as follows In Maven you can do it as follows. <dependencies> <dependency> <groupId>org.springframework.security</groupId> <artifactId>spring-security-web</artifactId> <version>4.0.3.RELEASE</version> </dependency> </dependencies> In Gradle you can do it as follows. dependencies { compile 'org.springframework.security:spring-security-web:4.0.3.RELEASE' } Your initial project may look like as s

Java : How to write a File in Java using FileWriter

Write a File in Java using FileWriter You can write a file using FileWriter as shown in the code below. package com.ekiras.demo; import java.io.File; import java.io.FileWriter; import java.io.IOException; import java.util.Date; class FileDemo { public static void main(String args[]) { readFile(); } public static void readFile() { System.out.println(" Start :: writing file"); try { File file = new File("/home/ekansh/myFile.txt"); FileWriter fileWriter = new FileWriter(file); fileWriter.write("hello, this file is created at :: " + new Date()); fileWriter.flush(); fileWriter.close(); System.out.println(" End :: writing file"); } catch (IOException e) { e.printStackTrace(); } } } You can write the data using the write() method and then call the flush() method to force the os to write to the file, finally close() method to close the file writer object, so that it can be garbage collected.

JavaScript : What is Javascript Hoisting

What is Hoisting in Javascript ?? In Javascript the variable declaration can be done at any place inside the function or inside the script. Where a variable is declared makes no difference because the javascript interpreter always move the variable declarations silently to the top of the scope where the variable is used. For example, everyone will be fine the following javascript code. <script> var num; num = 5; function foo(){ alert(num); } foo(); </script> And, all will be able to guess that the function foo() will alert value 5 . However, the following code will confuse a lot of the developers, since the variable num is used before it is declared. <script> num = 5; function foo(){ alert(num); } foo(); var num; </script> The above code will run absolutely fine and will alert 5 just as in the previous example. So what happened in example 2 ? What happened is Javascript Hoisting  and it happened automatically behind the scen

SpringBoot : How to set active profile in spring boot application

Points To Remember There are two ways to set the spring profiles Using OS environment variables Using -D arguments Must Read :: What are profiles in Spring boot. How to set active profile in spring boot application If you want to set Operating System Environment variables for your application the you can do as follows export SPRING_PROFILES_ACTIVE=development export SERVER_PORT=8090 gradle bootRun java -jar build/libs/demo-1.0.0.jar If you want to specify it each time while running jar then you can do as follows java -jar -Dspring.profiles.active=development build/libs/dem-1.0.0.jar java -jar -Dserver.port=8090 build/libs/demo-1.0.0.jar

OOJS : Create an Object in Javascript

Points To Remember You can create objects of a function in javascript. Function can have both private and public methods. Functions can inherit other functions Create an Object in Javascript Let us create a User object in javascript. var User = function() { _self = this; var _name; _self.getName = function() { return _name; } _self.setName = function(name) { _name = name; } _self.toString = function() { return "Person{name::" + _name + "}"; } } Here User is a function name is a private variable and will not be accessible outside the User function. _self is used to save the this  reference, this is done so that it does not mix with the this  of the inner functions. getName, setName and toString are the public functions that can be called from an object of User function. typeof User "function" Now let us create two Objects of the User function. var user1 = new User(); var user2 = new User(); Both user1

Hazelcast : Auto Reconnect to Hazelcast Server

Points To Remember You can call the method getLifecycleService().isRunning()  to check if the hazelcast clinet is up and running. Auto Reconnect to Hazelcast Server You can use the following code to reconnect to Hazelcast server, in case the hazelcast client looses the connection to the hazelcast server. if(!(hazelcastInstance!=null && hazelcastInstance.getLifecycleService().isRunning())){ // create a new hazelcast connection } return hazelcast instance