Thursday, January 11, 2024

Interview Questions for freshers on spring boot\java

Spring boot API vs goLang APISpring boot API vs goLang API? Spring Boot (Java): Well-established in the enterprise world. Extensive ecosystem with a rich set of libraries and frameworks. Strong object-oriented programming support. Good for large, complex applications. Uses traditional thread-based concurrency Generally has slightly longer startup times and higher memory usage. Uses Maven or Gradle for dependency management. Golang: Known for its simplicity, efficiency, and strong concurrency support. Fast compilation and execution speed. Explicit and minimalist syntax. Suitable for building scalable and efficient microservices. Has goroutines and channels, providing a more lightweight and efficient concurrency model. Known for fast compilation, quick startup times, and efficient resource usage. Has its own built-in dependency management tool called "go modules." How to deploy spring boot api in local windows machine? Build Spring Boot project using Maven or Gradle. Run Build Command in your project’s root Directory . Open command prompt in the directory containing your built JAR or WAR file. Use Java -jar command to run your spring boot application. Once the application is running open web browser and go to http://localhost:8080. How to deploy spring boot api in docker container? Create a `Dockerfile` in the root of your Spring Boot project. This file will contain instructions for building the Docker image. FROM openjdk:11-jre-slim WORKDIR /app COPY target/your-application-name.jar app.jar EXPOSE 8080 CMD ["java", "-jar", "app.jar"] Adjust the `your-application-name.jar` with the actual name of your JAR file. Open a terminal and navigate to the directory containing your `Dockerfile`. Build the Docker image using the following command: docker build -t your-image-name:tag . Replace `your-image-name:tag` with a suitable name and version for your Docker image. After successfully building the Docker image, you can run a container using the following command: docker run -p 8080:8080 your-image-name:tag This command maps port 8080 from your container to your local machine. Once the container is running, open a web browser and go to `http://localhost:8080` to access your Spring Boot API. Explain all annotations? 1. @ComponentScan: Scans for Spring components within a specified package or packages. 2. @Component: Indicates that a class is a Spring component, and its instances can be managed by the Spring container. 3. @Service, @Repository, @Controller: Specialized versions of `@Component` used for service classes, repository classes, and controller classes, respectively. 4. @Autowired: Marks a constructor, field, or method to be autowired by Spring's dependency injection mechanism. 5. @Qualifier: Used in conjunction with `@Autowired` to specify the exact bean to be injected, especially when multiple beans of the same type exist. 6. @Configuration: Identifies a class as a configuration class, defining beans and their relationships. 7. @Bean: Used within a `@Configuration` class to declare a bean. The method annotated with `@Bean` produces a bean instance. 8. @Value: Injects values from properties files or other sources into fields in a Spring bean. 9. @Profile: Allows the selection of beans based on the active profiles in the Spring application. 10. @RequestMapping: Maps HTTP requests to handler methods in Spring MVC controllers. 11. @PathVariable: Extracts values from the URI template and injects them into method parameters. 12. @RequestParam: Binds request parameters to method parameters in Spring MVC controllers. 13. @RequestBody: Binds the body of an HTTP request to a method parameter, typically used for handling POST requests with JSON payloads. Embedded Server? In Spring Boot, you can embed a server like Apache Tomcat or Jetty directly within your application, eliminating the need for a separate server installation. To use the embedded server, include the necessary dependency in your project's build file (e.g., Maven or Gradle).

No comments:

Post a Comment