A quick tutorial for middleware products

  • WeblogicArch

  • J2EEArch

  • FussionArch

Monday, April 8, 2019

On April 08, 2019 by Kamlesh   1 comment
What does spring framework do?
Spring framework lets you develop java j2ee applications in a rapid easy way,Simple right.
But the problem is setting up spring project manually as it involved lot of configurations.

What does spring boot do?
Spring boot aims to wrap all the spring components in a convenient way with no external xml configuration whatsoever. so basically spring boot lets you create a microservice that wraps the spring core in an easy way.

If you create a spring boot project using spring intializr or in any other way, you will see below maven dependency in pom.xml file which downloads all the spring core components that is required to develop an application,you don't need to add manually each component in the project as you do in manual set up of spring framework.


Components like springs core, hibernate validators, logging etc will be downloaded.


<parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.0.1.RELEASE</version>
</parent>

 
Since above dependency is a parent, all the other dependencies will be treated as child, No need to mention the version of any child dependencies you are going to add, compatible child dependencies are added based on the parent version.

This is a huge advantage of spring boot over manual set up of springs framework, Just imagine manually adding all the dependencies that needs to be compatible to all other existing dependencies, we have all suffered that pain.

Typical spring boot pom would look as shown below

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
 <modelVersion>4.0.0</modelVersion>

 <groupId>com.spring.security.demo</groupId>
 <artifactId>SpringSecurity</artifactId>
 <version>0.0.1-SNAPSHOT</version>
 <packaging>jar</packaging>

 <name>SpringSecurity</name>
 <description>Demo project for Spring Boot</description>

 <parent>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-parent</artifactId>
  <version>2.0.1.RELEASE</version>
  <relativePath/> <!-- lookup parent from repository -->
 </parent>

 <properties>
  <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
  <java.version>1.8</java.version>
 </properties>

 <dependencies>
  <dependency>
   <groupId>org.springframework.boot</groupId>
   <artifactId>spring-boot-starter-data-jpa</artifactId>
  </dependency>
  <dependency>
   <groupId>org.springframework.boot</groupId>
   <artifactId>spring-boot-starter-security</artifactId>
  </dependency>
  <dependency>
   <groupId>org.springframework.boot</groupId>
   <artifactId>spring-boot-starter-web</artifactId>
  </dependency>

  <dependency>
   <groupId>org.springframework.boot</groupId>
   <artifactId>spring-boot-starter-test</artifactId>
   <scope>test</scope>
  </dependency>
  <dependency>
   <groupId>org.springframework.security</groupId>
   <artifactId>spring-security-test</artifactId>
   <scope>test</scope>
  </dependency>
 
  <dependency>
   <groupId>javax.servlet</groupId>
   <artifactId>jstl</artifactId>
  </dependency>
  <!-- To compile JSP files -->
  <dependency>
   <groupId>org.apache.tomcat.embed</groupId>
   <artifactId>tomcat-embed-jasper</artifactId>
   <scope>provided</scope>
  </dependency>
 
 </dependencies>

 <build>
  <plugins>
   <plugin>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-maven-plugin</artifactId>
   </plugin>
  </plugins>
 </build>


</project>
In addition to that, Spring boot provides Spring Boot Data JPA which helps in quering with database in a much easy and quick way, See how it is here


Feature Spring Framework Spring Boot
Configuration XML(lot of pain) Annotations(No pain buddy)
Server Need External Server Comes with Embedded Server
ORM Hibernate Spring Boot Data JPA
Actuator Supported Works well with spring boot
Controller Only Web Web and Rest
Do not consider above table as comparison, try to consider as advantages of spring boot over spring framework.

Conclusion
Spring framework was introduced to make it easy for developers to develop an application in short period of time using spring components that will help in focusing more on business logic rather than DAO layer or Controller.

But it involved many problems like manual setup that involved a lot of configuration,persisting mechanism was not very quick and needed some kind of knowledge on hibernate etc.

And Spring boot was introduced to make it much more easy for developers by removing manual setup,removing configuration and introducing spring boot jpa for persisting mechanism.

Note: Spring Boot acts as a container for spring components(spring framework) with very minimal configuration and with lots of other advantages.
On April 08, 2019 by Kamlesh   1 comment
Before diving into this implementation, I would suggest you to look at Spring Security Introduction first.
Let's Start
Most of the Web Applications uses custom login page with custom authentication,So lets go with it.

 <html>
 <body>
 <form method="post" action="/login">
 ${sessionScope["SPRING_SECURITY_LAST_EXCEPTION"].message}
                     ${logoutmsg}
 UserName <input type="text" name="username"/>
 <br/>
 Password <input type="password" name="password"/>
 <br/>
 <input type="submit">
 </form>
 </body>
 </html>



Login page is ready,Method should be post and action "/login" to invoke spring security,The EL tag ${sessionScope) will display recent exception message thrown by the springs.

Lets create a user entity now.


import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;

import org.hibernate.validator.constraints.NotBlank;

public class User {


 @Id
    @GeneratedValue(strategy = GenerationType.AUTO)   
    private Long userId;

    public Long getUserId() {
  return userId;
 }

 public void setUserId(Long userId) {
  this.userId = userId;
 }

 public String getUsername() {
  return username;
 }

 public void setUsername(String username) {
  this.username = username;
 }

 public String getPassword() {
  return password;
 }

 public void setPassword(String password) {
  this.password = password;
 }

 public String getRole() {
  return role;
 }

 public void setRole(String role) {
  this.role = role;
 }

 @NotBlank
 private String username; 

    @NotBlank
    private String password; 

    @NotBlank
 private String role;
}

When the username and password is submitted from login page,the security config file which extends WebSecurityConfigurerAdapter will be invoked.
Custom Authenticator is used in below example

@Configuration
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {

 @Autowired
 private CustomUserDetailsService userDetailsService;


@Autowired
CustomAuthentication customauthentication;

 @Autowired
 public void configure(AuthenticationManagerBuilder auth) throws Exception {   
  auth.authenticationProvider(customauthentication); //The Custom Authenticator is used here.
 }


 @Override
 protected void configure(HttpSecurity http) throws Exception {

 //CSFR is disabled,if you dont know what csrf is,Spring has a beautiful documentaion about it ,Check it out.
 http.csrf().disable();

 //Login,logout page and resources are permitted for all users
 http.authorizeRequests().antMatchers("/","/login","/logout","/resources/**").permitAll();


  //userInfo page requires login as ROLE_USER or ROLE_ADMIN.
     // If no login, it will redirect to /login page.
        http.authorizeRequests().antMatchers("/userInfo").access("hasAnyRole('ROLE_USER', 'ROLE_ADMIN')");

     // For ADMIN only.
        http.authorizeRequests().antMatchers("/admin").access("hasRole('ROLE_ADMIN')");

 //Login and logout configurations
 //username and password parameter must match the login form username and password parameter
 //When the user logs out,it will be redirected to login page as specified,it is always good practice to display a logout message whwn the user logs
 out,To display a logout message,follow the last snippet.
 //On Successful login user will be redirected to "/index" page as specified below else back to login page.
 http.authorizeRequests().and().
 formLogin().loginProcessingUrl("/login").loginPage("/login").defaultSuccessUrl("/index")
 .failureUrl("/login?error=true").usernameParameter("username").passwordParameter("password").
 and().
 logout().logoutSuccessUrl("/login?logout");


  // If no login, it will redirect to /login page.
 http.authorizeRequests().antMatchers("/**").authenticated();

 //Handling Access Denied Request
 http.authorizeRequests().and().exceptionHandling().accessDeniedPage("/accessdenied");

 }

}


Simple Custom Authentication would look like below one,logic might change based on your requirements.

public class CustomAuthentication implements AuthenticationProvider {

@Autowired
private UserRepository userrepository;

 @Override
 public Authentication authenticate(Authentication auth)
   throws AuthenticationException {
 
   String username = auth.getName();
   String password = auth.getCredentials().toString();

  User user = userrepository.findByUsername(username);
  if(user==null){
   throw new BadCredentialsException("Username Not Found");
  }
 
  if(!password.equals(user.getPassword)){
   throw new BadCredentialsException("Username Or Password Is invalid");
  }
 
 
  return new UsernamePasswordAuthenticationToken(username,password,
    Arrays.asList(new SimpleGrantedAuthority(user.getRole())));
 }

 @Override
 public boolean supports(Class<?> arg0) {
  return true;
 }

}

In the above code exception is thrown with a relevant message if the condition fails,these messages are displayed on login page by EL tag ${sessionScope["SPRING_SECURITY_LAST_EXCEPTION"].message}.

If everything checks out,user will be forwarded to welcome page as specified in the security config file.

On Successfully logging out , /login is called which hits the controller,You can set the logout message there as shown below

@RequestMapping(value="/login")
public String showLogin(String error,String logout,Model model) {

 if(logout!=null)
  model.addAttribute("logoutmsg", "You've been logged out Successfully");

    return "login";
}
On April 08, 2019 by Kamlesh   1 comment
Spring Security provides comprehensive security services for J2EE-based enterprise software applications.

As you probably know two major areas of application security are "authentication" and "authorization" (or "access-control").
These are the two main areas that Spring Security targets.

"Authentication" is the process of establishing a principal is who they claim to be i.e authenticating the user during signing in.

"Authorization" refers to the process of deciding whether a principal is allowed to perform an action within your application.

Spring Security Maven

<dependency>
    <groupId>org.springframework.security</groupId>
    <artifactId>spring-security-core</artifactId>
    <version>4.0.3.RELEASE</version>
</dependency>
Spring Security provides even its own basic login page,processes the login on its own using the jpa,We will see how the set up is done in next chapter
On April 08, 2019 by Kamlesh   No comments
There might be a situation where you need to use your own method based on the requirement of a application.

Spring Boot JPA provides not only CRUD Operations,if you extend JpaRepository, a whole lot of implementation will be available

Some of them are shown below

findByLastnameAndFirstname
findByLastnameOrFirstname
findByStartDateBetween
findByAgeLessThan
findByAgeLessThanEqual
findByAgeGreaterThan
findByAgeGreaterThanEqual
findByStartDateAfter
findByStartDateBefore
findByAgeIsNull
findByAge(Is)NotNull
findByFirstnameLike
findByFirstnameNotLike
findByFirstnameStartingWith

Before adding your own custom implementation,I would suggest you to look at all the implementations that are already available here.

Your own implementation can be added as shown below

public interface PersonDAO extends JpaRepository<Person, Long> {

    @Query("update User u set u.username = :username where u.userid=:userid")
    Integer updateUsernameByUserid(@Param("username") String username,@Param("userid") int userid);
}
On April 08, 2019 by Kamlesh   No comments
Crud Operations using Spring Data JPA
import org.springframework.data.repository.CrudRepository;

public interface PersonDAO extends CrudRepository<Person, Long> {
}



Here PersonDAO Interface extends CrudRepository which contains crud operation methods and it accepts two generics,
Person - bean which you want to persist(Bean name)
Long - Type of primary key of the bean Person.(primary key is mandatory to persist)
Implementation
We dont really implement PersonDAO interface at all,Hibernate implements for use,we just use those method as shown below.

@Service
public class ServiceLayer {

@Autowired
private PersonDAO dao;

 public Person addPerson(Person person) {
 
  return dao.save(person);

 }
 public List getAllPerson() {
 
  return (List) dao.findAll();
 }

 public void deletePerson(int id) {
 
  dao.delete(id);
 }
}

If you observe closely we have just declared PersonDAO object, not intialized,Since it is annotated with @Autowired, Spring looks for the implementation available for that Interface and intializes for us,i.e DEPENDENCY INJECTION 
On April 08, 2019 by Kamlesh   No comments
Java Persistance API is a specification that provides an easy way of mapping from java object to relational mapping
Why JPA?
The java bean what we try to insert into database needs to be converted into single relational row that can fit into a table, This was all taken care by ORM's (Object - relational mapping), still the problem persists as the conversion had too much of boiler plate code,JDBC Connections, Creating statements,prepared statements,hard coding the queries,looping over results sets etc, it was not really sophisticated and simple, Hence JPA.

How is it simple using JPA now??
Spring Data JPA made it more easy to map the object to relational values, usually DAO layer contains all the information about contacting with the databases, All the boilerplate code that we were writing removed completely by just an interface(not even implementation).

YES,Just an interface, Dont get confused, How it is implemented is in coming chapters!!

To use a Spring Data JPA,add the following dependency

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
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.