Showing posts with label spring. Show all posts
Showing posts with label spring. Show all posts

August 01, 2021

Spring Boot question and explanations

 Question: What is diff BeanFactory and ApplicationContextFactory?

Solution:

The Spring Framework comes with two IOC containers – BeanFactory and ApplicationContext. The BeanFactory(Lazy Loading is the most basic version of IOC containers, and the ApplicationContext ( Eager Loading) extends the features of BeanFactory.BeanFactory loads beans on-demand, while ApplicationContext loads all beans at startup. 

Ex:  

Resource res = new ClassPathResource("ioc-container-difference-example.xml"); BeanFactory factory = new XmlBeanFactory(res); 

Student student = (Student) factory.getBean("student");

Or 

ApplicationContext context = new ClassPathXmlApplicationContext("ioc-container-difference-example.xml");

Reference: https://www.baeldung.com/spring-beanfactory-vs-applicationcontext

 

Question: What Is Spring Security?

Solution:

Spring Security is a separate module of the Spring framework that focuses on providing authentication and authorization methods in Java applications. It also takes care of most of the common security vulnerabilities such as CSRF attacks.

To use Spring Security in web applications, we can get started with the simple annotation @EnableWebSecurity.

For more information, we have a whole series of articles related to security.

Question: Name Some of the Design Patterns Used in the Spring Framework?

Solution:

  • Singleton Pattern – singleton-scoped beans
  • Factory Pattern – Bean Factory classes
  • Prototype Pattern – prototype-scoped beans
  • Adapter Pattern – Spring Web and Spring MVC
  • Proxy Pattern – Spring Aspect-Oriented Programming support
  • Template Method Pattern – JdbcTemplateHibernateTemplate, etc.
  • Front Controller – Spring MVC DispatcherServlet
  • Data Access Object – Spring DAO support
  • Model View Controller – Spring MVC

Reference: https://www.baeldung.com/spring-interview-questions

 

Question: Spring WebFlux?

Solution:

The original web framework included in the Spring Framework, Spring Web MVC, was purpose-built for the Servlet API and Servlet containers. The reactive-stack web framework, Spring WebFlux, was added later in version 5.0. It is fully non-blocking, supports Reactive Streams back pressure, and runs on such servers as Netty, Undertow, and Servlet 3.1+ containers.

Reference: https://docs.spring.io/spring-framework/docs/current/reference/html/web-reactive.html#webflux



Question: Spring REST – HTTP OPTIONS Request Handler Example

Solution:

HTTP OPTIONS Method

The HTTP OPTIONS method is used to describe the communication options for the target resource. This method allows the client to determine the options and/or requirements associated with a resource, or the capabilities of a server, without implying a resource action or initiating a resource retrieval.

  • If the Request-URI is an asterisk (“*”), the OPTIONS request is intended to apply to the server in general rather than to a specific resource.
  • If the Request-URI is not an asterisk, the OPTIONS request applies only to the options that are available when communicating with that resource.
  • Responses to this method are not cacheable.


@RequestMapping(value="/buckets", method = RequestMethod.OPTIONS)

    ResponseEntity<?> collectionOptions() 

    {

         returnResponseEntity

                 .ok()

                 .allow(HttpMethod.GET, HttpMethod.POST, HttpMethod.OPTIONS)

                 .build();

    }

 

Reference: https://howtodoinjava.com/spring-rest/http-options-request-handler/


Question: Spring – Inversion of Control vs Dependency Injection

Solution:

What is Inversion of Control (IoC)

In traditional programming, the flow of the business logic is determined by objects that are statically assigned to one another. With inversion of control, the flow depends on the object graph that is instantiated by the assembler and is made possible by object interactions being defined through abstractions. The binding process is achieved through dependency injection, although some argue that the use of a service locator also provides inversion of control.

Inversion of control as a design guideline serves the following purposes:

  1. There is a decoupling of the execution of a certain task from implementation.
  2. Every module can focus on what it is designed for.
  3. Modules make no assumptions about what other systems do but rely on their contracts.
  4. Replacing modules has no side effect on other modules.

What is Dependency Injection (DI)

IoC is a design paradigm with the goal of giving more control to the targeted components of your application, the ones getting the work done. While Dependency injection is a pattern used to create instances of objects that other objects rely on without knowing at compile time which class will be used to provide that functionality. IoC relies on dependency injection because a mechanism is needed in order to activate the components providing the specific functionality.

How to implement IoC

In object-oriented programming, there are several basic techniques to implement inversion of control. These are:

  1. using a factory pattern
  2. using a service locator pattern
  3. using a dependency injection of any given below type:
    • a constructor injection
    • a setter injection
    • an interface injection

Inversion of control in Spring

The org.springframework.beans and org.springframework.context packages provide the basis for the Spring Framework’s IoC container. The BeanFactory interface provides an advanced configuration mechanism capable of managing objects of any nature. The ApplicationContext interface builds on top of the BeanFactory (it is a sub-interface) and adds other functionality such as easier integration with Spring’s AOP features, message resource handling (for use in internationalization), event propagation, and application-layer specific contexts such as the WebApplicationContext for use in we

Reference: https://stackoverflow.com/questions/3715967/when-should-we-call-system-exit-in-java


Question: What  is System.exit()?

Solution:

The java.lang.System.exit() method exits current program by terminating running Java virtual machine. This method takes a status code. A non-zero value of status code is generally used to indicate abnormal termination. This is similar exit in C/C++.

Following is the declaration for java.lang.System.exit() method:

public static void exit(int status)

exit(0) : Generally used to indicate successful termination.
exit(1) or exit(-1) or any other non-zero value – Generally indicates unsuccessful termination.


Additional info:

System.exit(0);

System.out.println("This line will never be reached");


System.exit(0) terminates the JVM. The parameter is passed back to the OS and is normally used to indicate abnormal termination (eg some kind of fatal error), so if you called java from a batch file or shell script you'd be able to get this value and get an idea if the application was successful.

System.exit(42)  == A script that called the application. Works in Windows, Unix and all other scriptable environments.


Reference: https://www.geeksforgeeks.org/system-exit-in-java/


Question: What is spring bean lifecycle?

Solution:


Bean life cycle is managed by the spring container. When we run the program then, first of all, the spring container gets started. After that, the container creates the instance of a bean as per the request, and then dependencies are injected. And finally, the bean is destroyed when the spring container is closed

  

Reference: https://www.geeksforgeeks.org/bean-life-cycle-in-java-spring/

https://reflectoring.io/spring-bean-lifecycle/


Question: Error Handling for REST with Spring  or Spring Boot @ControllerAdvice

Solution:

There are couple of ways to handle exception in springbok

Solution 1: the Controller-Level @ExceptionHandler

Solution 2: the HandlerExceptionResolver

Solution 3: @ControllerAdvice

Solution 4: ResponseStatusException (Spring 5 and Above)

Handle the Access Denied in Spring Security


Default Spring Boot Support exception messages:

In a nutshell, it serves a fallback error page for browsers (a.k.a. the Whitelabel Error Page) and a JSON response for RESTful, non-HTML requests:

{

    "timestamp": "2019-01-17T16:12:45.977+0000",

    "status": 500,

    "error": "Internal Server Error",

    "message": "Error processing the request!",

    "path": "/my-endpoint-with-exceptions"

}


Using @ControllerAdvice:

  • SpringBoot @ControllerAdvice tutorial shows how to use @ControllerAdvice annotation to manage exceptions in a Spring Boot application.
  • @ControllerAdvice is a specialization of the @Component annotation which allows to handle exceptions across the whole application in one global handling component. It can be viewed as an interceptor of exceptions thrown by methods annotated with @RequestMapping and similar.
  • ResponseEntityExceptionHandler is a convenient base class for @ControllerAdvice classes that wish to provide centralized exception handling across all @RequestMapping methods through @ExceptionHandler methods. It provides an methods for handling internal Spring MVC exceptions. It returns a ResponseEntity in contrast to DefaultHandlerExceptionResolver which returns a ModelAndView.


Using the Controller-Level @ExceptionHandler:

The first solution works at the @Controller level. We will define a method to handle exceptions and annotate that with @ExceptionHandler:

public class FooController{

    //...

    @ExceptionHandler({ CustomException1.class, CustomException2.class })

    public void handleException() {

        //

    }

}

Reference: https://zetcode.com/springboot/controlleradvice/

https://www.baeldung.com/exception-handling-for-rest-with-spring


Read more ...

March 28, 2018

Upload a file in cloud space(multipart upload operation) using HttpClient

To use below program, to upload a file easily in could space.
we will illustrate how to do a multipart upload operation using HttpClient.



import java.io.BufferedReader;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.StringReader;
import java.io.StringWriter;
import java.util.LinkedHashMap;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.transform.OutputKeys;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.ContentType;
import org.apache.http.entity.mime.HttpMultipartMode;
import org.apache.http.entity.mime.MultipartEntityBuilder;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.tomcat.util.http.fileupload.FileItem;
import org.apache.tomcat.util.http.fileupload.disk.DiskFileItem;
import org.springframework.web.multipart.MultipartFile;
import org.w3c.dom.Document;
import org.xml.sax.InputSource;

/**
* This class is used for uploading image in cloud
*/
public class UploadImage {

public static void main(String[] args) throws IOException {
httpImageTest();
//convertFileFromMultipartFile - convert multipart file to file object

     }

    private static void httpImageTest() throws ClientProtocolException, IOException {
        File file = new File(""); //c:/image/test.jpeg
        CloseableHttpClient client = HttpClients.createDefault();
        HttpPost post = new HttpPost("");
        post.addHeader("", "");
        MultipartEntityBuilder builder = MultipartEntityBuilder.create();
        builder.setMode(HttpMultipartMode.BROWSER_COMPATIBLE);
        builder.addTextBody("Content-Type", "image/jpeg");
        builder.addBinaryBody("file", file, ContentType.DEFAULT_BINARY, "fileName.jpeg");

        HttpEntity entity = builder.build();
        post.setEntity(entity);
        HttpResponse response = client.execute(post);
        System.out.println(response.getStatusLine());
        BufferedReader rd = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
        StringBuffer result = new StringBuffer();
        String line = "";
        while ((line = rd.readLine()) != null)
            result.append(line);
        System.out.println(formatXML(result.toString()));
        client.close();
    }

private static File convertFileFromMultipartFile(MultipartFile file) throws IOException {
        File convFile = new File(file.getOriginalFilename());
        convFile.createNewFile();
        FileOutputStream fos = new FileOutputStream(convFile);
        fos.write(file.getBytes());
        fos.close();
        return convFile;
    }

    private static String formatXML(String input) {
        try {
            Transformer transformer = TransformerFactory.newInstance().newTransformer();
            transformer.setOutputProperty(OutputKeys.INDENT, "yes");
            transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "3");

            StreamResult result = new StreamResult(new StringWriter());
            DOMSource source = new DOMSource(parseXml(input));
            transformer.transform(source, result);
            return result.getWriter().toString();
        } catch (Exception e) {
            e.printStackTrace();
            return input;
        }
    }

    private static Document parseXml(String in) {
        try {
            DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
            DocumentBuilder db = dbf.newDocumentBuilder();
            InputSource is = new InputSource(new StringReader(in));
            return db.parse(is);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }
}

Reference: http://www.baeldung.com/httpclient-multipart-upload

Read more ...

July 22, 2016

Flyway : Automatic database creation and updations for Web Applications (INTEGRATING FLYWAY IN A SPRING FRAMEWORK APPLICATION)

This post is for about how to integrate Flyway into a Spring/JPA application for database schema migration. To skip all the preambles and get straight to the instructions, jump to Project’s Dependencies Set-up
Flyway is a database migration tool which helps do to databases, what tools like git/svn/mercurial does for source code…which is versioning. With Flyway you can easily version your database: create, migrate and ascertain its state, structure, together with its contents. It basically allows you to take control of your database, and be able to recreate it across different environment or different versions of the application it runs with, while keeping track of the chronological changes made.
I recently worked on a project where such database schema migration was needed. And we found Flyway a good tool for the job. Prior to using Flyway, all of the schema updates that had to be done were delivered to the client in separate SQL files, with instruction on how they should be run.
The short coming of such a process soon became apparent. It was burdensome and error prone.
First of all, it calls for extra operation activities on the part of the client as they have to run whatever update scripts that are provided and ensure the database schema is in the required state before application deployment – Not an ideal process. An application should be as self-contained as possible, so when delivered, it should be deployable without much hassle on the client’s part.
Also it unnecessarily exposes the internals of the database state. Having the database modified outside of the application does not guarantee its integrity, especially when the database modification is being done by a party who is not privy to the internals of the application the database is running with.
So when the time came to upgrade the version of the application in production to a newer version, we decided to re-evaluate the process through which we provide schema upgrades. This is when Flyway entered the picture.
As mentioned, Flyway is a database migration tool. It can be used via its command line tool, via a maven plug-in or programmatic-ally from within an application, but Since the application I was working on was built with Spring framework, and uses a JPA backed database (provided by Hibernate), I then set about on how to have Flyway integrated into such an application. This post captures the typical step that would be involved.
But before getting into the steps, a little overview of how Flyway works would help have a clear understanding of the integration instructions.

How Flyway works. An Overview

Schema changes and content modification (adding, modifying or deleting contents in database tables) are done via SQL statements. What Flyway does is to provide a mechanism in which these changes can be recorded and versioned. This it does by 1) Being responsible for applying the schema changes specified in the SQL scripts and 2) By keeping an internal meta-data table named SCHEMA_VERSION through which it keeps track of various information regarding the applied scripts: when it was applied, by whom it was applied, description of the migration applied, its version number etc.
Flyway allows for the SQL scripts (which are frequently referred to as migrations) to be provided in the form of plain old SQL script files or via Java code. The important thing though, is that, whichever form the migration is written, the files need to be named following a convention, which is explainedbelow.
For more information on how Flyway works, check the How Does Flyway Work in the project website.
Now with the brief overview out of the way, we can go ahead and look at the instructions.

Integrating Flyway into a JPA, Spring backed application.

When integrating Flyway into a JPA, Spring application, what you want to do is to have Flyway kick in, find the migration files (either in SQL or JAVA) and apply the necessary modifications to the database at application start-up. The following steps show how to achieve this.

1. Project’s Dependencies Set-up

The first thing to do is to add the needed project dependencies, so apart from the Spring/JPA dependencies, include Flyway as a dependency. If you are using Maven, then the following lines should be added to the dependencies section of your POM.xml:
1<dependency>
2    <groupId>org.flywaydb</groupId>
3    <artifactId>flyway-core</artifactId>
4    <version>${Flyway.version}</version>
5</dependency>
In case you are not using maven for your dependency management, the process of including Flyway should be trivial in whatever tool you are using.

2. Configuring Flyway to integrate into Spring’s Container

The next steps would be the configuration needed to have Spring pick up Flyway as a managed bean and have it play nicely with other beans it would need to work it…to be specific the entity manager factory.
If you are using XML to configure spring the necessary configuration would look thus:
1
2<bean id="flyway" class="org.Flyway.core.Flyway" init-method="migrate">
3    <property name="baselineOnMigrate" value="true" />
4    <property name="locations" value="filesystem:/path/to/migrations/" />
5    <property name="dataSource" ref="dataSource" />
6</bean>
7 
8
9<bean id="entityManagerFactory"class="o.s.orm.jpa.LocalContainerEntityManagerFactoryBean" depends-on="flyway">
10    <property name="dataSource" ref="dataSource" />
11    <property name="jpaVendorAdapter">
12        <bean class="o.s.orm.jpa.vendor.HibernateJpaVendorAdapter">
13            <property name="database" value="${jpa.database}"/>
14        </bean>
15    </property>
16</bean>
17 
18
19 <bean id="dataSource" class="o.a.commons.dbcp.BasicDataSource" lazy-init="true" destroy-method="close">
20        <property name="driverClassName" value="org.hsqldb.jdbcDriver"/>
21        <property name="url" value="jdbc:hsqldb:mem:qade"/>
22        <property name="username" value="sa"/>
23        <property name="password" value=""/>
24  </bean>
If you are using JavaConfig over XML then the configuration would look thus:
1@Configuration
2 
3public class AppConfig {
4 
5@Bean(initMethod = "migrate")
6Flyway flyway() {
7Flyway flyway = new Flyway();
8flyway.setBaselineOnMigrate(true);
9flyway.setLocations("filesystem:/path/to/migrations/");
10flyway.setDataSource(dataSource());
11return flyway;
12}
13 
14@Bean @DependsOn("flyway")
15EntityManagerFactory entityManagerFactory() {
16LocalContainerEntityManagerFactoryBean bean = newLocalContainerEntityManagerFactoryBean();
17bean.setDataSource(dataSource());
18// other configurations
19return bean.getObject();
20}
21 
22@Bean
23DataSource dataSource() {
24DataSource dataSource = new BasicDataSource();
25// data source configuration
26return dataSource;
27}
28}
Some additional things to take note of in the configurations:
Flyway Configuration.
When configuring Flyway you need to specify the init method (which was done using the init-methodproperty in xml and initMethod property of the @Bean annotation in JavaConfig)
This is used to instruct Spring to call the migrate() method once all the properties of the Flyway bean has been initialized. The migrate() method is what is responsible for performing the migration logic: that is finding the migration scripts, applying them and keeping a tab of successful migrations etc. Which is why it is specified as the init method as you would want this to be executed as soon as the Flyway bean is initialized in Spring’s container.
The baseLineOnMigrate is also another interesting part of the configuration. It comes in handy when Flyway is initially used for the first time and no SCHEMA_VERSION table exists. It instructs Flyway that before the migration scripts are applied, it should create a migration entry within the SCHEMA_VERSION table which would serve as the baseline, and thus the available migration script would only be applied if their version is higher than the baseline version.
The dataSource is used to specify the dataSource. Nothing interesting about that.
The last item of interest in the configuration is the location through which you specify where Flyway should find the migration scripts. Flyway has the ability to scan either the filesystem or classpath for these migration scripts. In the above example the value given to the location is prefixed with “filesystem:” indicating to Flyway to use its file system scanner to locate the migration scripts. If no prefix is speicified or “classpath:” is used as the prefix, then Flyway uses its classloader scanner to locate the migration scripts.
Entity Manager Factory Configuration.
The one thing to take note in the Entity Manager Factory’s configuration is the depends on property that is set to reference the Flyway bean. What this does is to ensure that the Flyway bean is always created before the Entity Manager Factory bean. Which is what you would want as you need Flyway to already have been created, do its thing before the Entity Manager Factory kicks in. In the JavaConfig this is specified using the @DependsOn annotation.

3. Writing Migration Scripts

Now that you have the necessary beans configured, the next thing to do is to create the migration scripts which contains the necessary schema updates that needs to be applied to the database during a migration, As specified earlier, Flyway supports migration scripts written in plain old SQL or in Java.
Whichever method you use, all you have to do is to have the migration file named appropriately, following the convention required by Flyway and have them in the location specified in the configuration.
Why a naming convention? The naming convention is required, as this is the default way Flyway keeps track of its versioning (I say default because when using Java, there is the possibility to override this naming mechanism. This is explained below). It uses it to determine the order in which migration scripts needs to be applied and to keep track of the scripts applied and ones pending.
The naming convention is as follows:
V__.
Where  is a numeric value that can contain a point (“.”) or an underscore (“_”) for example 3, 3.1, 3_1 are all valid for the version. (Not that if an underscore is used, it would be turned into a dot “.” at runtime by Flyway)
The double underscore, __ is what is used to separate the version number from the description.
 is a short description of what the scripts contained in the file is about. It would be extracted and added to the description column of the schema_version table.
For the  you can have either sql or java depending on the method being used to supply the migrations.
So the following are all valid names
V3__description_of_migration.sql
V3.1__description_of_migration .java
V3_1__description_of_migration .sql
If the migrations are specified in SQL there is little else that can be done apart from specifying the schema updates and naming them appropriately. On the other hand, with Java we have a little more flexibility. I would quickly take a look at how to use Java to write the migration and some of its available features not present when using SQL.

3. Writing Flyway Migration in Java.

As with migration written in SQL you need to have the Java migration in the location specified in the configuration. You then also need to have the class in which the sql migration are written implement the  JdbcMigration or SpringJdbcMigration interface.
The difference between these two interfaces is that jdbcMigration makes a Connection object available for interacting with the database while SpringJdbcMigration makes a JdbcTemplate object available for database interaction.
So a migration script written in Java may look like this:
1import org.flywaydb.core.api.migration.spring.SpringJdbcMigration;
2import org.springframework.jdbc.core.JdbcTemplate;
3 
4public class V2__moveDBStateToV2 implements SpringJdbcMigration {
5 
6@Override
7public void migrate(JdbcTemplate jdbcTemplate) throws Exception {
8// programmatically change the database state using JdbCTemplate
9}
10 
11}
And that is it. This way you can perform complex checks and operations needed to move the database state into a required state.
Apart from that, the Java option also provides some additional functionality like allowing you to disregard the naming convention and instead have the version and description specified in the Java class. This is done by implementing the MigrationInfoProvider Interface. For example:
1public class WhateverName implements SpringJdbcMigration, MigrationInfoProvider {
2 
3@Override
4public void migrate(JdbcTemplate jdbcTemplate) throws Exception {
5// programmatically change the database state using JdbCTemplate
6}
7 
8@Override
9public MigrationVersion getVersion() {
10return MigrationVersion.fromVersion("2"); //return 2 as version
11 
12}
13 
14@Override
15public String getDescription() {
16return "Moves DB to State Two";
17}
18 
19}

Some closing thoughts.

When I first approached Flyway as the tool to be used for database migration, one of the first thoughts that came to mind was, why use an tool when I already have hibernate hbm2ddl which can also be used to modify the database schema. I already have hibernate being the JPA provider, why an external tool?
The answer is simple. Hibernates hbm2ddl is in no way as flexible or as powerful compared to a database migration tool like Flyway. At its core, hbm2ddl could be seen as just a tool for extending your database to keep it in line with new additions to the backing entity. It does not have the ability to modify field names, transfer data from one column to another etc. Tasks which always pop up in any real life database migration situation.
So if you need to do proper database migration and versioning, a tool like Flyway is quite handy.
A thing to also note is that Flyway can be used as a standalone migration tool. This is possible via Flyway’s robust command line tool, whose instruction can be found here. Flyway also provides callbacks which can be used as extension points to hook into Flyway’s lifecycle to perform advance operations. The instruction on the callback can be found her

e.
All in all, Flyway really shines in that it gets the job done and it gets it done well. It is relatively simple with little learning curve and flexible. The documentation and JavaDoc is also decent, which makes working with it painless.

Reference: https://flywaydb.org/documentation/callbacks.html,
http://blog.trifork.com/2014/12/09/integrating-flywaydb-in-a-spring-framework-application/
Read more ...

My Favorite Site's List

#update below script more than 500 posts