Spring自动配置与显式的@Enable*配置声明

huangapple go评论90阅读模式
英文:

Spring auto configuration vs explicit @Enable* configuration declaration

问题

Spring 4提供了一个很好的功能,自动配置与@Conditional API一起使用。Spring还有许多内置的@Enable*配置(例如@EnableWebMvc@EnableCaching@EnableScheduling等)。但是,我在何时应该显式地使用这些@Enable配置,或者只需要自动配置并定义一些自定义bean来进行覆盖,就已经足够了。请为我解释这个问题。

英文:

Spring 4 provides a great feature with auto configuration along with @Conditional API. Spring also has many built-in @Enable*** configuration (such as @EnableWebMvc, @EnableCaching, @EnableScheduling...). But I am confused when I should use explicitly these @Enable** configuration or I just need Auto Configuration and define some custom beans to override is enough. Please explain me this issue.

答案1

得分: 3

@EnableWebMvc@EnableCaching@EnableScheduling 是启用 Spring 的不同功能的方式,通过其中一些模块提供。这些都不是 Spring Boot 的一部分。这些功能涉及很多繁重的工作,因此默认情况下不会被激活。

例如,即使你使用 @Scheduled,除非你使用 @EnableScheduling 启用调度功能,否则调度程序不会被启用。这使得 Spring 能够执行支持此功能所需的必要操作。这可能包括运行多个自动配置(AutoConfiguration)类。

自动配置类提供了一种在启动期间需要完成的配置插件的方式。这是可扩展的,意味着你可以编写自己的自动配置类。这些自动配置类是 Spring Boot 的一部分。

Spring Boot 主要是基于观点的,这意味着它会根据 POM 中的依赖项做出许多决策。例如,当它看到 MongoDB 依赖项时,它会尝试连接到本地主机和端口 27017 的 MongoDB 数据库。这是通过自动配置类完成的。这是可扩展的,可以通过 application.properties 提供自定义值或提供自定义实现来实现。

英文:

@EnableWebMvc, @EnableCaching, @EnableScheduling are ways to enable different capabilities that spring provide through some of its module.

These are not part of springboot

. These features involves lot of heavy lifting to be done. And hence these are not activated by default.

For example even if you use @Scheduled, scheduler will not be enabled unless you enable scheduling capability using @EnableScheduling. This enables spring to do necesasry things required to support this feature. This may/may not include running several AutoConfiguration classes

AutoConfiguration classes provide a way to plugin configuration which needs to be done during startup. This is extendable. This means, you can write your own Autoconfiguration classes. And these Autoconfiguration classes are part of springboot

Spring boot is mainly opinionated, which means it does take a lot of decisions based on the dependecies it sees in the pom. For example when it sees mongo dependecies, it tries to connect to moongo database on localhost and port 27017. This is done through AutoConfiguration classes. Which is extendable, by ways of providing custom values through application.properties or provinding custom implementations

答案2

得分: 0

以下是翻译好的部分:

如果您使用Spring Boot,则自动配置会自动启用,如果您可以使用application.properties文件来处理这些配置。

如果您覆盖/自定义某些配置,请查看以下代码:

import java.util.concurrent.Executor;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;

@Configuration
@EnableAsync
public class AsyncConfig 
{
    @Bean(name = "taskExecutor")
    public Executor taskExecutor()
    {
        ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
        executor.setCorePoolSize(2);
        executor.setMaxPoolSize(2);
        executor.setQueueCapacity(100);
        executor.setThreadNamePrefix("Thread-");
        executor.initialize();
        return executor;
    }
}

在这里,我们可以自定义配置异步功能。

英文:

If you use Spring Boot then AutoConfiguration is automatically enabled and if you can handle those using application.properties file.

If you override/customize some Configuration then please check following code :

import java.util.concurrent.Executor;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;

@Configuration
@EnableAsync
public class AsyncConfig 
{
	@Bean(name ="taskExecutor")
    public Executor taskExecutor()
	{
        ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
        executor.setCorePoolSize(2);
        executor.setMaxPoolSize(2);
        executor.setQueueCapacity(100);
        executor.setThreadNamePrefix("Thread-");
        executor.initialize();
        return executor;
    }
}

Here we can custom configure Asynchronous functionality.

huangapple
  • 本文由 发表于 2020年4月5日 19:51:22
  • 转载请务必保留本文链接:https://go.coder-hub.com/61042134.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定