Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How does Spring know where to search for Components or Beans?

Tags:

spring-boot

In an interview i was asked by the interviewer that "How does Spring know where to search for Components or Beans?".

As I was not aware about the internal flow details I was not able to answer the question properly.

I said through @Component and @Bean we can find. But the interviewer was not happy with the question. If anybody knows please share your knowledge. TIA

like image 952
Naren Reddy Avatar asked Jun 04 '20 02:06

Naren Reddy


People also ask

What is component scanning How does Spring do component scanning?

Using component scan is one method of asking Spring to detect Spring-managed components. Spring needs the information to locate and register all the Spring components with the application context when the application starts.

How does Spring boot component scan work?

Using @ComponentScan in a Spring Application. With Spring, we use the @ComponentScan annotation along with the @Configuration annotation to specify the packages that we want to be scanned. @ComponentScan without arguments tells Spring to scan the current package and all of its sub-packages.

What is difference between @component and @bean in Spring?

@Component is a class-level annotation, but @Bean is at the method level, so @Component is only an option when a class's source code is editable. @Bean can always be used, but it's more verbose. @Component is compatible with Spring's auto-detection, but @Bean requires manual class instantiation.

How does @bean works in Spring?

Spring @Bean Annotation is applied on a method to specify that it returns a bean to be managed by Spring context. Spring Bean annotation is usually declared in Configuration classes methods. In this case, bean methods may reference other @Bean methods in the same class by calling them directly.


1 Answers

I love to nswer interview questions. Read below...

@ComponentScan

If you understand Component Scan, you understand Spring.

Spring is a dependency injection framework. It is all about beans and wiring in dependencies.

The first step of defining Spring Beans is by adding the right annotation — @Component or @Service or @Repository.

However, Spring does not know about the bean unless it knows where to search for it.

This part of “telling Spring where to search” is called a Component Scan.

You define the packages that have to be scanned.

Once you define a Component Scan for a package, Spring would search the package and all its sub packages for components/beans.

Defining a Component Scan

  • If you are using Spring Boot, check the configuration in Approach 1.
  • If you are doing a JSP/Servlet or a Spring MVC application without using Spring Boot, use Approach 2.

Approach 1: Component Scan in a Spring Boot Project

If your other package hierarchies are below your main app with the @SpringBootApplication annotation, you’re covered by the implicit Component Scan. If there are beans/components in other packages that are not sub-packages of the main package, you should manually add them as @ComponentScan

Consider below class

package com.in28minutes.springboot.basics.springbootin10steps;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ConfigurableApplicationContext;
@SpringBootApplication
public class SpringbootIn10StepsApplication {
    public static void main(String[] args) {
        ApplicationContext applicationContext =
            SpringApplication.run(SpringbootIn10StepsApplication.class, args);
        for (String name: applicationContext.getBeanDefinitionNames()) {
            System.out.println(name);
        }
    }
}

@SpringBootApplication is defined in the SpringbootIn10StepsApplication class which is in the package com.in28minutes.springboot.basics.springbootin10steps

@SpringBootApplication defines an automatic Component Scan on the package com.in28minutes.springboot.basics.springbootin10steps.

You are fine if all your components are defined in the above package or a sub-package of it.

However, let’s say one of the components is defined in package com.in28minutes.springboot.somethingelse

In this case, you would need to add the new package into Component Scan.

You have two options:

Option 1:

@ComponentScan(“com.in28minutes.springboot”)
@SpringBootApplication
public class SpringbootIn10StepsApplication {...}

Option 2:: Define as array

@ComponentScan({"com.in28minutes.springboot.basics.springbootin10steps","com.in28minutes.springboot.somethingelse"})
@SpringBootApplication
public class SpringbootIn10StepsApplication {...}

Approach 2: Non-Spring Boot Project

Option 1:

@ComponentScan(“com.in28minutes)
@Configuration
public class SpringConfiguration {...}

Option 2:

@ComponentScan({"com.in28minutes.package1","com.in28minutes.package2"})
@Configuration
public class SpringConfiguration {...}

XML application context:

<context:component-scan base-package="com.in28minutes" />

Specific multiple packages:

<context:component-scan base-package="com.in28minutes.package1, com.in28minutes.package2" />
like image 134
Kunal Vohra Avatar answered Nov 02 '22 04:11

Kunal Vohra