Spring 如何知道在哪里搜索组件或 Bean? [英] How does Spring know where to search for Components or Beans?

查看:26
本文介绍了Spring 如何知道在哪里搜索组件或 Bean?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在一次采访中,面试官问我Spring 如何知道在哪里搜索组件或 Bean?".

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.

我说通过@Component@Bean我们可以找到.但是面试官对这个问题并不满意.如果有人知道,请分享您的知识.TIA

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

推荐答案

我喜欢回答面试问题.阅读下文...

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

@ComponentScan

如果你了解组件扫描,你就会了解 Spring.

If you understand Component Scan, you understand Spring.

Spring 是一个依赖注入框架.这都是关于 bean 和依赖关系的连接.

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

定义 Spring Beans 的第一步是添加正确的注解——@Component@Service@Repository.

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

然而,Spring 不知道 bean 的存在,除非它知道在哪里搜索它.

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

这部分告诉 Spring 在哪里搜索"称为组件扫描.

您定义必须扫描的包.

一旦你为一个包定义了一个组件扫描,Spring 就会在这个包及其所有子包中搜索组件/beans.

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

定义组件扫描

  • 如果您使用的是 Spring Boot,请检查方法 1 中的配置.
  • 如果你正在做一个 JSP/Servlet 或一个 Spring MVC 应用程序而没有使用 Spring Boot,使用方法 2.

方法一:Spring Boot 项目中的组件扫描

如果您的其他包层次结构位于带有 @SpringBootApplication 注释的主应用程序之下,则隐式组件扫描将覆盖您.如果其他包中有bean/components不是主包的子包,需要手动添加为@ComponentScan

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

考虑下面的类

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);
        }
    }
}

@SpringBootApplicationSpringbootIn10StepsApplication 类中定义,该类位于 com.in28minutes.springboot.basics.springbootin10steps

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

@SpringBootApplication 定义了对包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.

但是,假设其中一个组件定义在 com.in28minutes.springboot.somethingelse

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.

您有两个选择:

选项 1:

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

选项 2::定义为数组

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

方法 2:非 Spring Boot 项目

选项 1:

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

选项 2:

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

XML 应用程序上下文:

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

特定的多个包:

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

这篇关于Spring 如何知道在哪里搜索组件或 Bean?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆