A quick tutorial for middleware products

  • WeblogicArch

  • J2EEArch

  • FussionArch

Monday, April 8, 2019

On April 08, 2019 by Kamlesh   No comments
One of the main reason of Spring boot huge success is AutoConfiguration, Developers were really exhausted by this xml configuration.

In traditional spring mvc architecture if you wanted just to configure component scan and views, you had to write below shown snippet.

<context:component-scan base-package="com.example.spring.dao"></context:component-scan>
<context:component-scan base-package="com.example.spring.controllers"></context:component-scan>
<context:component-scan base-package="com.example.spring.services"></context:component-scan>
<mvc:annotation-driven/>
 <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
      <property name="prefix" value="/WEB-INF/jsps/" />
      <property name="suffix" value=".jsp" />
 </bean>

Now,In spring boot it is just two lines of code that needs to be placed in application.properties.


spring.mvc.view.prefix=/WEB-INF/views/
spring.mvc.view.suffix=.jsp
 
So What is application.properties?
It is a YAML configuration file placed under src/main/resources folder,it is a human readable key:value format where you place all the configuration like server port,database information,logging systems etc.


Example : For logging properties

logging.level.org.springframework.web=DEBUG
logging.level.org.hibernate=ERROR

For more examples refer spring boot documentation
On April 08, 2019 by Kamlesh   No comments
For traditional spring mvc architecture refer here, We were annotating spring mvc Controller Class with @Controller as shown below


@Controller
public class ViewsController {

@RequestMapping("/view1")
public String showView1(){
 return "register";
}

In the above code as we know,when the request is made to ApplicationContext/view1 the resource register file will be returned.

In addition to that,Spring boot supports rest controller as shown below


@RestController
@Produces("application/json")
public class BaseController {

@RequestMapping("/list")
private List getList(){
 return Arrays.asList("one","two","three");
}
}

As mentioned above the Annotation @RestController identifies that it needs to produce the content that is mentioned, Here the method getList() returns json as mentioned with annotaion @Produces("application/json").
On April 08, 2019 by Kamlesh   No comments
Since most of the spring boot configurations are done by annotations,it is better if we go through what annotations are first.

Annotation : provides information about the program that is annotated with and it is not part of the program itself.

These annotations can be run time or compile time annotations

Compile Time Annotations
Compile time annotations are checked by the compiler at compile time.

public interface Test {

 public void add(int a,int b);
}

class Tester implements Test{
 @Override
 public void add(int a, int b) { 
 }

}
In the above code Annotation @Override is compile time,if you try to add or remove any parameter it will show an error in IDE itself.

Run Time Annotations
As the Compile time annotation,run time annotation checks for run time information about the program that is annotated with.
On April 08, 2019 by Kamlesh   No comments
Setting up Spring Boot

Best way to set up a spring boot application is by Spring Intializer,Where you can add all the dependencies required ,download  and import it in IDE.
OR
Create a new maven or gradle project and all the dependencies manually.
OR
Start a spring starter project from spring tools suite IDE

To start an application,just run Generated MainApplication.java file which starts the embedded server.

import org.springframework.boot.SpringApplication;
@SpringBootApplication
public class MainApplication {
 public static void main(String[] args) {
 
  SpringApplication.run(MainApplication.class, args);
 }
 
}

Just run the MainApplication.java as classic java application, Once the server is up,you can request for resources.
Let's create some resources in coming chapters.
On April 08, 2019 by Kamlesh   No comments
Why spring boot was introduced when we had all the comforts of frameworks spring like mvc,hibernate etc


 One main reason to remove all the configurations that would take plenty of time to just setup the framework.

Setting up the traditional Spring MVC framework includes
Adding Internal View Resolvers
Setting up Hibernate Session Factory
Connection to Database etc

Sample Code of Component scan and internal view resolver in tradition mvc approach is as shown below

<context:component-scan base-package="com.example.spring.dao"></context:component-scan>
<context:component-scan base-package="com.example.spring.controllers"></context:component-scan>
<context:component-scan base-package="com.example.spring.services"></context:component-scan>
<mvc:annotation-driven/>
 <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
      <property name="prefix" value="/WEB-INF/jsps/" />
      <property name="suffix" value=".jsp" />
 </bean>

Above Code is replaced in just two lines in spring boot using Annotations.

Annotations are widely used in spring boot,All the configurations are replaced by single line annotations, so that the developer can concentrate on business logic rather than spending more time on configuring.

We will see how single line annotations can replace the traditional xml  in chapter "Annotations".
On April 08, 2019 by Kamlesh   No comments
Before staring with Spring Boot,You need to have basic knowledge of spring core to get better understanding of spring boot.


Lets start ...

What is spring boot?
Spring boot acts as a tool to build microservice.

What is microservice?
Microservice is a software architecture style that structures an application with collection of loosely coupled components.

Definition looks little complex?
Lets break it down!!

1.  Microservice is a software architecture
Architecture can be defined as set of certain rules to develop an application.

2.  It structures an application with collection of loosely coupled components
The Architecture structures an application  which may contain many components,It can be ORM's,Data Providers,JPA etc that are loosely coupled (does not depend on one other).

What exactly is microservice,In general way??
It is very difficult to explain what microservice is in just one shot,I will try my best here,

Just think microservice as a container which comes with no precise definition of architectural style,You can plug in whichever server you like to deploy the application, add your favorite ORM's, etc,it is a production ready system,it is as simple as running classic "java main class".

Here Spring boot basically aims in bootstrapping and deploying the spring packed project in a easy and quick way.

On April 08, 2019 by Kamlesh   No comments
This tutorials contains full end to end discussion on spring boot for beginners.The topics are well constructed, source code is available for each topic which can be downloaded and deployed in any IDE.

This "Spring Boot Tutorials" Contains
Spring Boot Introduction
                     What is Spring Boot?
                     Why Spring Boot?
                     Creating a Spring Boot Project
                     Annotations
                     Spring Boot Controllers
                     Spring Boot Configurations using YAML

 Spring Boot JPA
                    Introduction to Spring Boot JPA
                    CRUD Operations using Spring Boot JPA
                    Implementing own method using Spring Boot JPA

Spring Security
                  Introduction to Spring Security
                  Spring Security Implementation

Use navigation to go through the tutorials, I would suggest to go through all the topics without skipping any. If you have any doubts or any examples are not working,please let us know in the comment section or contact us.