Posts

Showing posts from January, 2023

Controller class in spring

 To initialize the controller in the spring we need the below annotations   @Controller : this is a specialization of the @Componet annotation.  the controller annotation indicates that the class is a controller type to spring while component scanning. by default, @Controller handler methods only return the view.  if we want to return JSON or XML response then need to add @ResponseBody annotation on every method in the class. @Controller public class HelloWordController { @RequestMapping( "/showform" ) public String showForm() { return "helloword-form" ; } @RequestMapping( "/showform2" ) @RequestBody public User showForm() { return new User("Nilesh","Kadam") ; } } @RestController : @RestController is a specialized version of @Controller annotation. this is a combination of @Controller and @ResponseBody annotation. RestController is useful for RESTfull web service because in RESTfull web service we on...

How to configure spring MVC using xml configuration

Image
The web browser sends the request to the front controller. the front controller is known as a dispatcher servlet. dispatcher servlet will redirect the request to the appropriate controller. the Controller will perform some operation to add or get the data into the model to fulfill requirements and return the view to display on the web browser. Spring MVC configuration steps Create web.xml and servlet-context.xml file in WebContent/WEB-INF folder. the Web.xml file contains the dispatcher servlet, and URL mapping configuration. the servlet-context.xml file containing component scanning, annotation-driven support, and view resolver configuration. Configure Spring MVC dispatcher servlet. (web.xml) <servlet> <servlet-name> dispatcher </servlet-name> <servlet-class> org.springframework.web.servlet.DispatcherServlet </servlet-class> <init-param> <param-name> contextConfigLocation </param-name> <param-value> /WEB-I...

Future interface in java

Future interface introduces in java 1.5 in java. util.concurrent package. It is used to hold the result of an asynchronous computation. The asynchronous result will automatically appear in the Future after the asynchronous process is completed. Mostly Future will use with Callable interface and ExecutorService to capture the asynchronous result.  Implementation of Future with thread public class FutureWithThread { private ExecutorService executor = Executors. newFixedThreadPool ( 20 ) ; public Future<Integer> makeDouble (Integer num){ return executor .submit(()->{ Thread. sleep ( 100 ) ; return num * num ; }) ; } } The interface provides the following four methods: get(): the method waits for the computation to complete the async operation and waits to retrieve its result. cancel() : suppose in case we want to stop the execution and we can use the future.cancel(true) to tell the executor service to stop the cur...

Microservice Architecture Advantages and Disadvantages

Microservices architecture is a method of developing software systems as a collection of small, independent services that communicate with each other over a network. Each microservice is responsible for a specific business function and runs its own process, which can be deployed and scaled independently of the other services. This approach to software development has several benefits: It allows for faster development and deployment of new features, as each microservice can be developed, tested and deployed independently. It allows for more flexible scaling, as different microservices can be scaled up or down depending on their usage. It promotes a culture of autonomy and ownership among development teams, as each team is responsible for the development and maintenance of their own microservice. It allows for the use of different technologies and programming languages for different microservices. It helps to reduce the complexity of the overall system, as each microservice can be unders...

How to do @Async operation in spring boot

Using @Async annotation we can create the asynchronous method in spring. If we add @Async annotation on the method then spring Will execute the method in a separate thread in the background thread pool.  Enabling @Async support in spring boot To enable asynchronous processing, you need to add @EnableAsync in the spring configuration class. @EnableAsync annotation will look for a method marked with @Async annotation and run them in the background thread pool.  @Configuration @EnableAsync public class AsyncConfig { ... }   @Async has two rules It must be applied to the public method only. Self-invocation is not allowed. That means calling the async method from the same class is not work. The reason for the above rule is very simple The method needs to be public so that can be provided and self-innovation doesn't work because it passes the proxy and call the method directly. The method with void return type The method with a void return type works like a runnable thre...