SpringBoot Annotations

SpringBoot Annotations

Before proceeding further let us first understand some important annotations of Spring Boot.

@SpringBootApplication: annotation is equivalent to using @Configuration, @EnableAutoConfiguration and @ComponentScan with their default attributes.  One thing to note here regarding ComponentScan is, @SpringBootApplication scans only the subpackages. i.e. for example if the main class is in com.javainfinite, then it will scan all classes from com.javainfinite.* packages.

@EnableAutoConfiguration: attempting to guess and configure beans. If we use SpringBootApplication this annotation is auto enabled and has no effect adding it again

@Controller: marks as web application controller

@RestController: works same as @Controller annotation except @ResponseBody is active by default. In case of @Controller we need to declare @ResponseBody.

@ResponseBody: returns value to the response as HTTP Response body

@RequestMapping: to map the URL for the particular method of entire class

@RequestParam: accessing query parameter values from the request

@PathVariable: pattern that is used in the URI for the incoming request

There are various other annotations too, above explained annotations are the ones most commonly used in springboot.

Read More

Basic Spring Boot Application Example

Basic Spring Boot Application Example:

We have already went through various examples using spring, To understand spring please refer here.

What is Spring Boot?
Spring Boot is a way to create stand-alone applications with less or no configurations and has defaults for annotation configuration which we will use in our application

Advantages of Spring Boot:

  • Reduces development time
  • Embedded Servers
  • No XML configuration
  • Integration with Spring JDBC, Spring ORM etc.

Now let us see an basic example to create spring boot application in Netbeans.

Structure:

pom.xml

SpringBootApp.java

Output:

We have created basic spring boot application!

 

 

Read More

Spring MVC Hibernate MySQL – Multiple Controllers

Spring MVC Hibernate MySQL – Multiple Controllers:
In our previous example we implemented Spring MVC Hibernate MySQL CRUD Operations (Here). In this article we are going to see how to use Multiple Controllers using Spring MVC.

In the below example, We are going to create 2 entities Employee and Manager and will have separate controllers for both these entities. Let us see how to add, view and delete. Edit operation is same as we discussed in previous example (Here), So i have not implemented Edit operation in this example.

Project Structure:

POM.xml

com.javainfinite.model:
Employee.java

Manager.java

com.javainfinite.dao:
EmployeeDAO.java

ManagerDAO.java

EmployeeDAOImpl.java

ManagerDAOImpl.java

com.javainfinite.service
EmployeeService.java

ManagerService.java

EmployeeServiceImpl.java

ManagerServiceImpl.java

spring-config.xml

web.xml

employee.jsp

manager.jsp

Output:

After Adding,

Deleting Employee,

On Clicking Manager Link,

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Read More

Spring MVC Hibernate MySQL CRUD Operation

Spring MVC Hibernate MySQL CRUD Operation:
The idea of MVC framework is to separate the business logic from front end and change them without affecting others.

Model: Represents data
View: User Interface of the application
Controller: Receives request and perform the operations

 

  • The request is received by the dispatcher servlet
  • The dispatcher Servlet checks with the Handler Mappings to determine the appropriate controller to pass the request
  • The dispatcher Servlet get the appropriate controller from Handler Mapping and passes the request to Controller.
  • The controller sends the request along with the object containing the name of the view and data to be displayed, but not the exact logical name
  • Dispatcher Servlet after receiving the name, checks with view resolver to resolve the actual logical name.
  • Finally the view is displayed to the user

Now let us see the example of Spring MVC, Hibernate with MySQL.

Project Structure:

pom.xml

Employee.java

EmployeeDAO

EmployeeDAOImpl.java

EmployeeService

EmployeeServiceImpl

EmployeeController.java

 

spring-config.xml

web.xml

employee.jsp

edit.jsp

Output:

On clicking Edit

 

Create Another record,

Deleting Alpha record,

 

 

 

 

Read More

Java Interview Questions – Part 2

Java Interview Questions – Part 2

String is Immutable. (String Details with Example Here)

  1. What is meant by Immutable?
    In Java, Strings are treated as Objects. Immutable means once the object has been created it cannot be changed.
  2. What are the advantages of String being Immutable?
    • It helps in Multi-threading, We can be sure that no one will change the string
    • Reduction in Memory usage
  3. Two ways of Creating Strings:
    • String s1 = “StringValue”;
    • String S2 = new String(“StringValue2”);
  4. What is the difference between creating Strings?
    Consider an example,

    • We are creating 2 strings with S1 and S2,
    • String S1 = “Java”
    • String S2 = “Java”
    • This will create a single reference in string pool and S1 and S2 will point to that Reference
    • String S1 = new String(“Java”)
    • String S2 = new String(“Java”)
    • This will create two separate objects in string pool
    • String s1 = “Java”, String S2 = “Java”
    • String S1 = new String(“Java”), String S2 = new String(“Java”)
  5. Difference Between StringBuilder and StringBuffer:
    StringBuilder StringBuffer
    StringBuilder is mutable StringBuffer is mutable
    StringBuilder is unsynchronized StringBuffer is Synchronized
    StringBuilder is faster StringBuffer is slower when compared to StringBuilder

    NOTE: StringBuffer and StringBuilder does not Override equals() and hashcode()

  6. When to use String, StringBuffer and StringBuilder?
    If our string is not going change, We use String.
    If our string can change and accessed only from single thread, We can use StringBuilder.
    If our string can change and can be accessed from multiple threads, We can use StringBuffer.
  7. Difference between == and Equals()
    • == : Will check if the address of the values are same and NOT the values itself
    • Equals(): Will check if both the values are equals
    • Example:
      • String S1 = “Java”, String S2 = “Java”
        S1==S2 will be True
      • String S1 = new String(“Java”), String S2 = new String(“Java”)
        Here S1 == S2 will be false, But S1.equals(S2) will be true
  8. Important Points:
    • Strings are Thread Safe
    • String Pool is stored in Heap Memory

(For Detailed Example of Strings, StringBuffer and StringBuilder, Click Here)

Arrays:

  1. What is an Array?
    Arrays are objects in Java that store multiple Variables of Same Type
  2. Declaring an Array?
    int[] array1;
  3. Can we declare an Array without its Size?
    Consider, We are constructing and array without mentioning the Size,

    • int[] Array1 = new int[];
      This line will not get Compiled, As the size of array is required.
  4. How does array store the values in memory?
  5. How to declare array with values in single line?
    • int array1 = {4,5,6};
  6. Arrays Holding Objects?
    • A[][] a = {{new A(“value1”), new A(“value2”)}, {new A(“value3”), new A(“value4”)}}

 

 

Read More