{"id":3083,"date":"2019-02-25T08:19:41","date_gmt":"2019-02-25T08:19:41","guid":{"rendered":"http:\/\/codethataint.com\/blog\/?p=3083"},"modified":"2021-04-24T13:36:46","modified_gmt":"2021-04-24T13:36:46","slug":"spring-boot-interview-questions","status":"publish","type":"post","link":"https:\/\/codethataint.com\/blog\/spring-boot-interview-questions\/","title":{"rendered":"Spring Boot Interview Questions"},"content":{"rendered":"<p><strong>Q1.What is use of @SpringBootApplication<\/strong><\/p>\n<pre>\r\n @SpringBootApplication =  @Configuration + @EnableAutoConfiguration + @ComponentScan\r\n<\/pre>\n<p><strong>@Configuration<\/strong> &#8211; Spring Configuration annotation indicates that the class has @Bean definition methods. So Spring container can process the class and generate Spring Beans to be used in the application.Refer <a href=\"http:\/\/codethataint.com\/blog\/configuration\/\">here<\/a><\/p>\n<p><strong>@EnableAutoConfiguration<\/strong> &#8211; @EnableAutoConfiguration automatically configures the Spring application based on its included jar files, it sets up defaults or helper based on dependencies in pom.xml. Auto-configuration is usually applied based on the classpath and the defined beans. Therefore, we donot need to define any of the DataSource, EntityManagerFactory, TransactionManager etc and magically based on the classpath, Spring Boot automatically creates proper beans and registers them for us. For example when there is a tomcat-embedded.jar on your classpath you likely need a TomcatEmbeddedServletContainerFactory (unless you have defined your own EmbeddedServletContainerFactory bean). <\/p>\n<p><strong>@EnableAutoConfiguration<\/strong> has a exclude attribute to disable an auto-configuration explicitly otherwise we can simply exclude it from the pom.xml, for example if we do not want Spring to configure the tomcat then exclude spring-bootstarter-tomcat from spring-boot-starter-web.<br \/>\n<strong>@ComponentScan<\/strong> &#8211; @ComponentScan provides scope for spring component scan, it simply goes though the provided base package and picks up dependencies required by @Bean or @Autowired etc, In a typical Spring application, @ComponentScan is used in a configuration classes, the ones annotated with @Configuration. Configuration classes contains methods annotated with @Bean. These @Bean annotated methods generate beans managed by Spring container. Those beans will be auto-detected by @ComponentScan annotation. There are some annotations which make beans auto-detectable like @Repository , @Service, @Controller, @Configuration, @Component. In below code Spring starts scanning from the package including BeanA class<\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\n@Configuration\r\n@ComponentScan(basePackageClasses = BeanA.class)\r\n@EnableAutoConfiguration(exclude = {DataSourceAutoConfiguration.class})\r\npublic class Config {\r\n\r\n  @Bean\r\n  public BeanA beanA(){\r\n    return new BeanA();\r\n  }\r\n\r\n  @Bean\r\n  public BeanB beanB{\r\n    return new BeanB();\r\n  }\r\n\r\n}\r\n<\/pre>\n<p><strong>Q2.What @Configurable annotation Does?<\/strong><br \/>\n<strong>@Configurable<\/strong> &#8211; @Configurable is an annotation that injects dependencies into objects, that are not managed by Spring using aspectj libraries. You still use old way of instantiation with plain new operator to create objects but the spring will take care of injecting the dependencies into that object automatically for you.<br \/>\nRefer <a href=\"http:\/\/codethataint.com\/blog\/configuration\/\">here<\/a><\/p>\n<p><strong>Q3.What is Starter Project?What are the Starter Project offered?<\/strong><br \/>\nStarter POMs are a set of convenient dependency descriptors that you can include in your application<br \/>\nspring-boot-starter-web-services &#8211; SOAP Web Services<br \/>\nspring-boot-starter-web &#8211; Web &#038; RESTful applications<br \/>\nspring-boot-starter-test &#8211; Unit testing and Integration Testing<br \/>\nspring-boot-starter-jdbc &#8211; Traditional JDBC<br \/>\nspring-boot-starter-hateoas &#8211; Add HATEOAS features to your services<br \/>\nspring-boot-starter-security &#8211; Authentication and Authorization using Spring Security<br \/>\nspring-boot-starter-data-jpa &#8211; Spring Data JPA with Hibernate<br \/>\nspring-boot-starter-data-rest &#8211; Expose Simple REST Services using Spring Data REST<\/p>\n<p><strong>Q4.What SpringApplication.run() method does?<\/strong><\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\n@SpringBootApplication\r\npublic class EmployeeManagementApplication \r\n{\r\n public static void main(String&#x5B;] args) \r\n {\r\n  SpringApplication.run(EmployeeManagementApplication.class, args);\r\n }\r\n}\r\n<\/pre>\n<p>&#8211; SpringApplication class is used to bootstrap and launch a Spring application from a Java main method.<br \/>\n&#8211; Creates the ApplicationContext from the classpath, scan the configuration classes and launch the application<br \/>\n&#8211; You can find the list of beans loaded by changing the code as below.Spring Boot provides auto-configuration, there are a lot of beans getting configured by it. <\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">  \r\n@SpringBootApplication(scanBasePackages = &quot;com.mugil.org.beans&quot;)\r\npublic class EmployeeManagementApplication\r\n{\r\n\tpublic static void main(String&#x5B;] args)\r\n\t{\r\n\t\tApplicationContext ctx = SpringApplication.run(EmployeeManagementApplication.class, args);\r\n\t\tString&#x5B;] beans = ctx.getBeanDefinitionNames();\r\n\t\tfor(String s : beans) System.out.println(s);\r\n\t}\r\n}\r\n\r\n@Component\r\npublic class Employee \r\n{\r\n.\r\n.\r\n.\r\n}\r\n<\/pre>\n<p>In the console you could see employeeManagementApplication and employee getting printed with their starting<br \/>\nletter in lower case. <\/p>\n<p><strong>Q5.Why Spring Boot created?<\/strong><br \/>\nThere was lot of difficulty to setup Hibernate Datasource, Entity Manager, Session Factory, and Transaction Management. It takes a lot of time for a developer to set up a basic project using Spring with minimum functionality. Spring Boot does all of those using AutoConfiguration and will take care of all the internal dependencies that your application needs \u2014 all you need to do is run your application. It follows <strong>\u201cOpinionated Defaults Configuration\u201d<\/strong> Approach to reduce Developer effort.Spring Boot looks at a) Frameworks available on the CLASSPATH b) Existing configuration for the application. Based on these, Spring Boot provides the basic configuration needed to configure the application with these frameworks. This is called Auto Configuration.<\/strong><\/p>\n<p><strong>Q6.<\/strong><br \/>\n<strong>Q7.<\/strong><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Q1.What is use of @SpringBootApplication @SpringBootApplication = @Configuration + @EnableAutoConfiguration + @ComponentScan @Configuration &#8211; Spring Configuration annotation indicates that the class has @Bean definition methods. So Spring container can process the class and generate Spring Beans to be used in the application.Refer here @EnableAutoConfiguration &#8211; @EnableAutoConfiguration automatically configures the Spring application based on its included&hellip; <a href=\"https:\/\/codethataint.com\/blog\/spring-boot-interview-questions\/\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[255],"tags":[],"class_list":["post-3083","post","type-post","status-publish","format-standard","hentry","category-interview-questions-spring-boot"],"_links":{"self":[{"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/posts\/3083","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/comments?post=3083"}],"version-history":[{"count":5,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/posts\/3083\/revisions"}],"predecessor-version":[{"id":4225,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/posts\/3083\/revisions\/4225"}],"wp:attachment":[{"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/media?parent=3083"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/categories?post=3083"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/tags?post=3083"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}