Skip to main content

Posts

Spring : Difference between @Autowired, @Inject and @Resource

Points To Remember @Inject is not a spring feature you need to include  javax inject dependency  in order to use @Inject. @Autowired is Spring annotation used to inject dependency just like @Inject. It use @Qualifier annotation to differentiate between the beans. @Resource is also Spring annotation, but it uses bean name to inject dependencies and differentiate between beans. Problem Statement :: Structure In order to show how @Autowired @Inject and @Resource annotations work  we will create 3 services Interface PersonService , this is the interface for all person related operations. Class EngineerService , this is the service that will do operations for engineer. Class ManagerService , this is the service that will do operations for manager. Here, both EngineerService and ManagerService implements PersonService.  Now we will try to add the services to a controller by different  ways to test how @Autowire , @Inject and @Resource will work. So the ...

SpringBoot : What are Profiles in Spring Boot Application

What are Profiles in Spring Boot Profiles can be seen as different environments in spring boot application. Suppose you are working on an application where you have different staging environments like Dev, QA, UAT, Production etc. So you will have different configurations for each environment, for this kind of applications what you need is having different values that can be switched depending upon some flags. In early days, people used to write configurations for all environments and comment the unused environments. But with Spring Boot we can do this without commenting any code by use of Profiles . Suppose we have database name configurations for our application as follows Dev     - ekiras_dev QA    - ekiras_qa UAT  - ekiras_uat Prod  - ekiras Now, we can use profiling in this case. How we will do it ? lets see Creating Profiles in Spring Boot  We will create different files for different environments and call then as profile in rest of the blog...

SpringBoot : How to display static html file in Spring boot MVC application

Points To Remember In order to serve static files like js, css, images etc ,all your files should be under the resources/static folder. Spring application can serve all the static files inside folders resources/static/ resources/public/ In order to serve html files from spring boot application  your html files should be under static/public folder you need to add view controller to serve html file How to display static html file in Spring boot MVC application Step 1 : Extend Class WebMvcConfigurerAdapter You should create a class that extends WebMvcConfigurerAdapter Your class should have  @Configuration annotation. You class should not have  @EnableMvc annotation. Override addViewControllers method and add your mapping. Override configurePathMatch method and update suffix path matching. view plain copy to clipboard print ? @Configuration    public   class  MvcConfigurer  extends  WebMvcConfigurerAdapter {      ...

ShellScript : How to write to the beginning of a file in Linux

How to write to the beginning of a file using shell script in Linux Suppose, we have a file release.txt  and we want to write to first line of the of the file whenever a new release is made. sed -i '1i\'"text to write " filename Using the above command as sed -i '1i\'"newVersion-RELEASE" release.txt This will append the text to the first line of the file release.txt In the example shown above , we have added a new version 2.0.0-RELEASE to version.txt file . If you want to do this using a shell script then you can do it as follows #!/bin/bash function writeAtBeginning(){ sed -i '1i\'"$1" $2 } writeAtBeginning "$@" In the above example, we have added the new version through a shell function by passing version and file name as parameters

SpringBoot : How to run Spring boot application to custom port

Points To Remember Default port for spring boot application is 8080. You can change the port number in the following ways from  application.properties command line arguments How to run Spring boot application to custom port In order to run a spring boot application on a custom port you can specify the port in application.properties. server.port=8090 If you are using environment specific properties file then you can define server.port  property in each of your properties file. You can alternatively set the port from command line arguments like below gradle -Dserver.port=8090 bootRun This will override any configuration of port given inside the properties file or application.yml file.

SpringSecurity : How to disable Session Creation for Stateless Authentication

How to disable Session Creation for Stateless Authentication We need to disable session creation for authenticating requests based on token based authentication. This can be easily configured by the following configurations. view plain copy to clipboard print ? package  com.ekiras.ss.security.config;      import  com.ekiras.ss.security.filter.TokenAuthenticationFilter;   import  org.springframework.context.annotation.Bean;   import  org.springframework.core.Ordered;   import  org.springframework.core.annotation.Order;   import  org.springframework.security.config.annotation.web.builders.HttpSecurity;   import  org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;   import  org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;   import  org.springframework.security.config.http.SessionC...

SpringSecurity : Authenticate User with Custom UserDetailsService

Points To Remember Create class that implement UserDetailsService and override loadUserByUsername() method. Throw UsernameNotFoundException if no user was found by username. Register this class as a bean by overriding WebSecurityConfigurerAdapter . Authenticate User with Custom UserDetailsService Step 1 : Create Entities for User and Role Create Entity User package com.ekiras.ss.domain; import javax.persistence.*; import java.util.Set; /** * @author ekiras */ @Entity public class User { @Id @GeneratedValue (strategy = GenerationType.AUTO) private long id; private String username; private String password; private boolean enabled; @ManyToMany (fetch = FetchType.EAGER,cascade = CascadeType.ALL) @JoinTable (joinColumns = @JoinColumn (name = "user_id" ),inverseJoinColumns = @JoinColumn (name = "role_id" )) private Set<role> roles; // GETTERS and SETTERS } Create Entity Role package com.ekiras.ss.domain; ...