Q8 How To Tell An Auto
To instruct an auto-configuration class to back off when a bean already exists, we can use the @ConditionalOnMissingBean annotation.
The most noticeable attributes of this annotation are:
- value the types of beans to be checked
- name the names of beans to be checked
When placed on a method adorned with @Bean, the target type defaults to the method’s return type:
@Configurationpublic class CustomConfiguration }
Q18 How To Change The Default Port In Spring Boot
- Using a properties file We can define this in an application.properties file using the property server.port.
- Programmatically In our main @SpringBootApplication class, we can set the server.port on the SpringApplication instance.
- Using the command line When running the application as a jar file, we can set the server.port as a java command argument:
java -jar -Dserver.port=8081 myspringproject.jar
What Is The Spring Beans Life Cycle
Spring Beans are initialized by Spring Container and all the dependencies are also injected. When the context is destroyed, it also destroys all the initialized beans. This works well in most cases but sometimes we want to initialize other resources or do some validation before making our beans ready to use. Spring framework provides support for post-initialization and pre-destroy methods in spring beans.
We can do this in two ways â by implementing InitializingBean and DisposableBean interfaces or using init-method and destroy-method attribute in spring bean configurations. For more details, please read Spring Bean Life Cycle Methods.
You May Like: An Interview With God Explained
What Is The Configuration File Name Which Is Used By Spring Boot
The configuration file name, which is utilized as a part of spring boot projects is application.properties. This document is very important where we would overwrite all the default setups. Ordinarily, we need to hold this document under the assets folder of the project.
There is no force to go containerless
- The embedded container is only one component of Spring Boot
- Traditional WAR additionally benefits a considerable measure from Spring Boot
- Automatic Spring MVC setup, including Dispatcher Servlet
- Sensible defaults in light of the class-path content
- The embedded container can be utilized during improvement.
Can You List Advantages Of Spring Boot
Advantages of Spring boot are:
- It provides a lot of default configurations which help you to create Spring application faster.
- It comes with embedded tomcat or jetty server, so you dont have to deploy jar.
- It reduces development code by avoiding a lot of boilerplate code.
- It increases productivity as you can create Spring application quickly.
- It provides a lot of starter project for easy maven integration.You dont have to worry about version mismatch.
- You can quickly create using sample project using spring boot initializer
Read Also: Sign Up For Interview Slots
Spring Boot Interview Questions And Answers
5) What is JavaConfig?
A) Spring JavaConfig is a product of the Spring community that provides pure Java methods for configuring Spring IoC containers. So it helps to avoid using XML configuration. The advantages of using JavaConfig are:
- Object-oriented configuration. Since the configuration is defined as a class in JavaConfig, users can take advantage of the object-oriented features in Java. A configuration class can inherit from another, override its @Bean method, and so on.
- Reduce or eliminate XML configuration. The benefits of externalization configuration based on the principle of dependency injection have been proven. However, many developers dont want to switch back and forth between XML and Java. JavaConfig provides developers with a pure Java way to configure Spring containers similar to the XML configuration concept. From a technical point of view, it is possible to configure the container using only the JavaConfig configuration class, but in fact many people think that it is ideal to mix JavaConfig and XML.
- Type safety and refactoring friendly. JavaConfig provides a type-safe way to configure the Spring container. Thanks to Java 5.0 support for generics, it is now possible to retrieve beans by type rather than by name, without any cast or string-based lookups.
6) What are the formats of Spring Boot configuration files? What is the difference between them?
A) .properties and .yml, the main difference between them is the writing format.
What’s Your Experience With Spring Boot
If you’re going on a Spring Boot interview, a general question might be to describe your experience with Spring Boot and with microservices. Successfully answering this question means coming prepared to discuss a time you developed and implemented microservices, especially if you did so using Spring Boot.
Example:”I used Spring Boot to create a microservices infrastructure in my current role. It helped us resolve bandwidth issues as we scaled to more users on our proprietary software platform.”
How To Check The Environment Properties In Your Spring Boot Application
If we need to set the different target environments, Spring Boot has a built-in mechanism.
One can simply define an application environment.properties file in the src/main/resources directory and then set a Spring profile with the same environment name.
For example, if we define a production environment, that means well have to define a production profile and then application-production.properties.
This environment file will be loaded and will take precedence over the default property file. You should note that the default file will still be loaded. Its just that when there is a property collision, the environment-specific property file takes precedence.
How To Configure The Logger In Spring Boot How To Change Default Logging Level
Spring Boot uses Commons Logging for all internal logging but leaves the underlying log implementation open. Default configurations are provided for Java Util Logging, Log4J2, and Logback. Also By default, if we use the Starters, Logback is used for logging.
- Enable a debug mode by starting your application with a –debug flag.
$ java -jar myapp.jar debug
- From application.properties
- Changing the log level
Read Also: How To Conduct Virtual Interviews
What Are The Different Scopes Of Spring Bean
There are five scopes defined for Spring Beans.
Spring Framework is extendable and we can create our own scopes too. But most of the time we are good with the scopes provided by the framework. To set Spring bean scopes we can use the âscopeâ attribute in bean element or @Scope annotation for annotation-based configurations.
Explain @restcontroller Annotation In Spring Boot
Spring restcontroller annotation is an annotation that is itself annotated within two annotations.
@Restcontroller is annotated within @controller and @Responsebody. This annotation is applied to mark the respective class as a request handler in your application.
Spring Rest controller annotation is used to create restful web services using Spring MVC.
Also Check: How To Do A Follow Up Email After An Interview
How To Insert Data In Mysql Using Spring Boot
First configure mysql in your spring boot application.
Then you can map your entities with your db tables using JPA.
And with the help of save method in JPA, you can directly insert your data into your database.
@RestController@RequestMappingpublic class Controller
In the above case, your data which may be in JSON format can be inserted successfully into the database.
@RequestMappingResponseEntity< ?> insert }
What Is The Advantage Of Using Spring Framework
Some of the advantages of using Spring Framework are:
- Reducing direct dependencies between different components of the application. Spring IoC container is responsible for initializing resources or beans and inject them as dependencies.
- Writing unit test cases are easy in the Spring framework because our business logic doesnât have direct dependencies with actual resource implementation classes. We can easily write a test configuration and inject our mock beans for testing purposes.
- Reduces the amount of boiler-plate code, such as initializing objects, open/close resources. I like JdbcTemplate class a lot because it helps us in removing all the boiler-plate code that comes with JDBC programming.
- Spring framework is divided into several modules, it helps us in keeping our application lightweight. For example, if we donât need Spring transaction management features, we donât need to add that dependency to our project.
- Spring framework supports most of the Java EE features and even much more. Itâs always on top of the new technologies, for example, there is a Spring project for Android to help us write better code for native Android applications. This makes the spring framework a complete package and we donât need to look after the different frameworks for different requirements.
You May Like: What Questions To Ask An Employer In A Job Interview
What Are The Spring Boot Starters
Starters are a set of convenient dependency descriptors which we can include in our application.
Spring Boot provides built-in starters which makes development easier and rapid. For example, if we want to get started using Spring and JPA for database access, just include the spring-boot-starter-data-jpa dependency in your project.
For more information
What Is The Easiest Way To Deploy A Spring Boot Application In Production What Other Options Are There
The simplest way to deploy your Spring Boot application is as a .jar file with an embedded servlet container, to any server or platform that has a JRE installed. For organizational and historical reasons, you can also deploy your Spring Boot application as a .war file, into an existing servlet container or application server.
Last but not least, you can, of course, also put your .jar file into a Docker image and even deploy those with Kubernetes.
Spring Boot Interview Question Answer For Java Developers
1. What is Spring Boot? Why should you use it?you don’t need to setup a Tomcat server2. What are some important features of using Spring Boot?1. Starter dependencyRESTful services2. Auto-Configuration3. Spring Initializer4. Spring Actuator5. Spring CLILearn Spring Boot – Rapid Spring Application Development3. What is auto-configuration in Spring boot? how does it help? Why Spring Boot is called opinionated?Spring Security MasterclassLearn Spring 5 and Spring Boot 2 courses4. What is starter dependency in Spring Boot? how does it help?Spring MVCJackson5. What is the difference between @SpringBootApplication and @EnableAutoConfiguration annotation?@EnableAutoConfiguration@SpringBootApplication6. What is Spring Initializer? why should you use it?Spring Boot Masterclass.7. What is Spring Actuator? What are its advantages?beansCPU and memory usageSpring Security Certification Class8. What is Spring Boot CLI? What are its benefits?Spring Boot in Action9. Where do you define properties in Spring Boot application?10. Can you change the port of the Embedded Tomcat server in Spring boot? If Yes, How?Learn Spring Boot in 100 Steps 11. What is the difference between an embedded container and a WAR?web server12. What embedded containers does Spring Boot support?13. What are some common Spring Boot annotations?Main class14. Can you name some common Spring Boot Starter POMs?15. Can you control logging with Spring Boot? How?classpath
How To Create Jar File In Spring Boot
To create a jar file in spring boot you need to define your packaging file as jar in your pom.xml.
Then just do maven build with specifying goals as package so that your application will start building.
Once the build is successful, just go into your Target folder and you can see .jar file generated for you application.
You May Like: Sample Case Manager Interview Questions
What Are Microservices In Spring Boot
Microservices is an architectural style wherein the key business capabilities of an application are exposed as loosely coupled services.
Sprint boot is a framework that has evolved to be formidable for Java microservice development.
Spring Boot enables building production-ready applications faster and provides embedded servers that are easy to deploy with containers.
Spring Cloud which builds on top of Spring Boot, provides features to quickly build production-ready microservices. Its possible to quickly set up services with minimal configurations Eg. Service Registration and discovery, circuit breakers, proxies, logging, log tracking, monitoring, etc.
Where Do You Need @enablewebmvc
The @EnableWebMvc annotation is required to enable Spring MVC when Java configuration is used to configure Spring MVC instead of XML. It is equivalent to < mvc: annotation-driven> in an XML configuration.
It enables support for the @Controller-annotated classes that use @RequestMapping to map incoming requests to handler methods that are not already familiar with Spring’s support for Java configuration. The Spring Master Class on Udemy is a good place to start.
Also Check: What To Ask A Graphic Designer In An Interview
What Is Bootstrapping In Spring Boot
One of the way to bootstrap your spring boot application is using Spring Initializer.you can go to the official website of spring and select your version, and add you groupID, artifactId and all the required dependencies.
And then you can create your restEndpoints and build and run your project.There you go, you have bootstrapped your spring boot application.
What Is The Difference Between @controller And @restcontroller In Spring Mvc
There are many differences between them @Controller and @RestController annotations, as discussed in my earlier article , but the most important one is that with the @RestController you get the @ResponseBody annotation automatically, which means you don’t need to separately annotate your handler methods with the @ResponseBody annotation.
This makes the development of RESTful web services easier using Spring. You can see here to learn more about Spring Boot and how it can help you to create Spring MVC-based web applications.
Don’t Miss: How To Interview A Nanny
Spring Boot Interview Questions That Make You Think
Here are 11 Spring Boot interview questions that don’t focus on trivial details, but rather make you think and thus learn a lot along the way.
Most lists of Spring Boot Interview questions make you memorize random details from the Spring Boot documentation. But memorization is a poor substitute for truly understanding and feeling confident about Spring Boot.
So today, weâre going to take a different approach. Rather than listing 50 questions on Spring Boot minutiae, weâre going to focus on 11 that make you think and thus learn a lot along the way.
Here they are, in random order.
What Do You Understand By Aspect
Enterprise applications have some common cross-cutting concerns that are applicable to different types of Objects and application modules, such as logging, transaction management, data validation, authentication, etc. The modularity of application is achieved by classes in Object-oriented programming. In AOP, application modularity is achieved by Aspects and they are configured to cut across different class methods.
AOP takes out the direct dependency of cross-cutting tasks from classes that are not possible in normal object-oriented programming. For example, we can have a separate class for logging but the other classes will have to call these methods. But, in AOP we configure the aspects and method execution happens automatically. Read more about Spring AOP support at Spring AOP Example.
Don’t Miss: How To Conduct A Second Interview
Integrating Struts 2 Jsf With Spring Spring Web Flow
Learning Objectives – In this module, we will start with introduction to Struts 2.x and JSF technologies. We will see how Strut 2.x applications configure to use Spring easily. We will see how JSF resolver resolves the value binding expressions used for performing a look up in the Spring configuration files. We will discuss details of mechanisms of using Spring with JSF and Struts applications. We will also understand how Spring Web Flow is motivated and how Spring Web Flow works.
Topics – Understand Struts 2 and JSF, Integration Spring with Struts, Developing a Struts 2 application, Integrating JSF with Spring, Developing a JSF application, Spring Web Flow.
Why In Spring Boot Opinionated Is Used
It takes after “Opinionated Defaults Configuration” Approach to lessen Developer exertion. Because of the Opinionated perspective of spring boot, what is required to begin yet additionally we can get out if not appropriate for the application. Spring Boot utilizes sensible defaults, opinions, for the most part in light of the classpath substance.
Read Also: How To Pass An Amazon Interview
How Does The Main Method Look Like In Spring Boot Or Code Snippet Of Main Method
Below code structure represent the main method of spring boot which use to kick start the spring boot application along with the configuration details which is in annotated form.
import org.springframework.boot.SpringApplication import org.springframework.boot.autoconfigure.SpringBootApplication /* Below code is sample code to run the spring boot application from main method.* We have to add the ComponentScan annotation to instantiate the bean for onfigure class.* Need to add the EnableAuutoConfiguration to do auto configuraion according to the * *dependencies. * SpringBootApplication annotation responsible to make the class as a spring boot application.*/@ComponentScan@EnableAutoConfiguration@SpringBootApplicationpublic class SpringBootApplication }
To run the above code, we have to run the below command either from command prompt or shell: mvn spring-boot: run
What Is Contextloaderlistener
ContextLoaderListener is the listener class used to load root context and define spring bean configurations that will be visible to all other contexts. Itâs configured in web.xml file as:
< context-param> < param-name> contextConfigLocation< /param-name> < param-value> /WEB-INF/spring/root-context.xml< /param-value> < /context-param> < listener> < listener-class> org.springframework.web.context.ContextLoaderListener< /listener-class> < /listener>
Read Also: How Much Does Interview Kickstart Cost
Example Of @get Endpoint
Below code structure represent the Rest Controller class developed using spring boot framework which use to act as Http Get method.
// Below series of import is important package specially org.springframework package.import java.util.HashMap import java.util.Map import org.springframework.http.HttpStatus import org.springframework.http.ResponseEntity import org.springframework.web.bind.annotation.RequestMapping import org.springframework.web.bind.annotation.RestController import com.practise.springboot.model.Product // Need to mention the RestController so that it will behave as rest end point.@RestControllerpublic class ProductServiceController // Below method act as a GET method, which is responsible to receive the HTTP GET call and return back the Product details as a response along with the HTTP Status OK. @RequestMapping public ResponseEntity< Object> getProduct }