Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/svg/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如果我们可以使用@component,那么bean工厂是做什么的?_Java_Spring_Spring Boot - Fatal编程技术网

Java 如果我们可以使用@component,那么bean工厂是做什么的?

Java 如果我们可以使用@component,那么bean工厂是做什么的?,java,spring,spring-boot,Java,Spring,Spring Boot,最近我看到这样的代码: @Bean @Validated @ConfigurationProperties(prefix = "jobrunner.filesystem") public ExecutionFileSystemProperties executionFileSystemProperties() { return new ExecutionFileSystemProperties(); } 为什么这样做?我们可以使用@Component,Spring将自己找到它们 另外,

最近我看到这样的代码:

@Bean
@Validated
@ConfigurationProperties(prefix = "jobrunner.filesystem")
public ExecutionFileSystemProperties executionFileSystemProperties() {
    return new ExecutionFileSystemProperties();
}
为什么这样做?我们可以使用@Component,Spring将自己找到它们


另外,同一个项目有@Component、@service等。

来自斯卡夫曼的回答:

@组件和@Bean做两件完全不同的事情,不应该混淆

@组件和@Service和@Repository用于使用类路径扫描自动检测和配置bean。在注释类和bean之间有一个隐式的一对一映射,即每个类一个bean。这种方法对布线的控制非常有限,因为它纯粹是声明性的

@Bean用于显式声明单个Bean,而不是像上面那样让Spring自动声明。它将bean的声明与类定义分离,并允许您按照自己的选择创建和配置bean


根据斯卡夫曼的回答:

@组件和@Bean做两件完全不同的事情,不应该混淆

@组件和@Service和@Repository用于使用类路径扫描自动检测和配置bean。在注释类和bean之间有一个隐式的一对一映射,即每个类一个bean。这种方法对布线的控制非常有限,因为它纯粹是声明性的

@Bean用于显式声明单个Bean,而不是像上面那样让Spring自动声明。它将bean的声明与类定义分离,并允许您按照自己的选择创建和配置bean