Sunday, 5 November 2017

Introducing Spring Boot - How to create your first Spring Boot Application

Today I am going to talk about how to create your first Spring Boot application. But before that someone would like to question what is this and why should I use it. So first I will tell you why you should start using Spring Boot for your coming applications and also try migrating old spring applications. But there is a mandatory requirement for spring boot. You must know either Maven or Gradle to start using SApring Boot.
Why should I use Spring Boot?
It is almost 15 years about the first beta release of the Spring framework. We had every configuration xml based. As it developed further, it moved on to the Java based configuration. But whether it is XML based or Java based you have to do a lot of configs to set up your application. If its a web application with hibernate and other spring features configs just get added up and u cant forget about that famous DispatcherServlet in spring.
Here Spring Boot comes to your rescue. It requires the minimal setup. So, how does everything work without those config in Boot. Yes its very smart. It detects the controllers services components entities security config classes automatically. Depending on the dependencies mentioned in Maven or Gradle it auto detects configuration. You don't even have to map location of your resources i.e. css js images themes. If you need some special config and sometimes you will need it as all those can't be auto-detected like your datasource url password etc which can be easily done using a single properties file named application.properties. Since this is just the start so we will build a Hello Worl Spring Boot.
Let's start and see how to build your first Hello World Spring Boot app
We will be using Intellij IDEA to create the project. We prefere first creating an empty project  and then adding a Spring Boot application module. This way you can have a root project with multiple modules.

1. Open Intellij IDEA and create an empty project.
2. Go to New > Module
3. Choose Spring Initializer from the option

4. Enter Maven details like groupid, artifactid, packaging etc.

5.In the next window select the dependencies. We chose only web

6. Next enter Module name and location and click on Finish. That's it. We can now write the codes

Lets start writing codes
pom.xml

 4.0.0

 com.innova
 spring-boot-intro
 0.0.1-SNAPSHOT
 war

 spring-boot-intro
 Demo project for Spring Boot

 
  org.springframework.boot
  spring-boot-starter-parent
  1.5.6.RELEASE
   
 

 
  UTF-8
  UTF-8
  1.8
 

 
  
   org.springframework.boot
   spring-boot-starter-web
  

  
   org.springframework.boot
   spring-boot-starter-tomcat
   provided
  
  
   org.springframework.boot
   spring-boot-starter-test
   test
  
 

 
  
   
    org.springframework.boot
    spring-boot-maven-plugin
   
  
 




Create folder src/main/webapp/WEB-INF/jsp to put in your jsp file

application.properties
spring.mvc.view.prefix=/WEB-INF/jsp/
spring.mvc.view.suffix=.jsp

PageController.java
package com.innova.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
@RequestMapping("/")
public class PageController {

    @RequestMapping("/")
    public String home() {
        return "index";
    }
}

index.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>


    Spring Boot Intro


Hello World


Watch our entire video

Get the entire code from GitHub

Cheers. Happy Coding :)


Saturday, 18 February 2017

Getting started with Arduino - LED Blink Digital Output

Today we will start our Arduino tutorial with a very basic thing. There are many coders around the world who write codes to develop some cool softwares to run on computers, smartphones and tsablets. But there are very few who writes code to control hardware using their program. Writing a general basic program won't do that. You need some special devices called microcontrollers and microcomputers with GPIO pins which can control digital devices, read sensor data and perform different actions.
Arduino is most popular open-source board microcontroller. It has a number of digital, analog PWM pins which will serve our purpose. Here we will use Arduino to make LED blink every second.

Hardware Required

  • Arduino UNO
  • Breadboard
  • LED - We are using a red LED here
  • 220ohm resistor
  • Some Jumper wires
Build the circuit
  • Connect a M-M jumper from GND to breadboard
  • Connect GND of breadboard to -ve terminal of LED via resistorto avoid burning it.
  • Connect +ve of LED to pin3 of Arduino

Write the code/sketch
  • Open Arduino IDE and then go to Tools > Board > Arduino Uno. 
  • And also select Tools > Port > Arduino COM Port (COM3/COM4 like)
const int LED = 3;  //Pin to which LED is connected to Arduino

void setup() {
  pinMode(LED, OUTPUT);  //We would see output from this pin
}

void loop() {
  digitalWrite(LED, HIGH);  //Set HIGH voltage
  delay(1000);             
  digitalWrite(LED, LOW);  //Set 0 voltage
  delay(1000);
}
Upload sketch to Arduino and see.


Cheers. Have Fun