Java 为什么扩展WebSecurityConfigureAdapter且不声明任何bean的类会用配置注释?
根据: 指示类声明一个或多个@Bean方法,并可由Spring容器处理,以在运行时为这些Bean生成Bean定义和服务请求,例如: 我记得我经常遇到扩展Java 为什么扩展WebSecurityConfigureAdapter且不声明任何bean的类会用配置注释?,java,spring,spring-bean,Java,Spring,Spring Bean,根据: 指示类声明一个或多个@Bean方法,并可由Spring容器处理,以在运行时为这些Bean生成Bean定义和服务请求,例如: 我记得我经常遇到扩展websecurityConfigureAdapter的类,这些类不包含任何@Bean方法,并使用@Configuration进行注释。 它甚至出现在官方博客和一些示例中,请参见: 或在此: 为什么即使没有@Bean方法,这些类也用@Configuration注释?Bean是 Spring功能,例如异步方法执行、计划任务执行、注释驱动的事务管理
websecurityConfigureAdapter
的类,这些类不包含任何@Bean
方法,并使用@Configuration
进行注释。
它甚至出现在官方博客和一些示例中,请参见:
或在此:
为什么即使没有@Bean
方法,这些类也用@Configuration
注释?Bean是
Spring功能,例如异步方法执行、计划任务执行、注释驱动的事务管理,甚至Spring MVC,都可以通过@Configuration类使用各自的“@Enable”注释来启用和配置。有关详细信息,请参阅@EnableSync、@EnableScheduling、@EnableTransactionManagement、@EnableSpectJautoProxy和@EnableWebMvc
发件人:
将此注释添加到@Configuration类中,以便在任何WebSecurity配置程序中定义Spring安全配置,或者更可能通过扩展WebSecurity配置程序基类并重写单个方法来定义:
@Configuration
不需要bean方法。@Configuration
或多或少是定义应用程序配置的类的标识符。websecurityConfigureAdapter
就是这样一个东西,它是应用程序配置的一部分。另外,@配置
类的处理方式与其他@组件
注释稍有不同。最后,@EnableWebSecurity
将导入其他几个提供bean的@Configuration
类。@M.Deinum我发现一个引号:>Configuration是一个原型,它将类声明为一个配置类,该类正在创建要添加到上下文中的bean实例(通过bean方法)和/或影响上下文的其他顶级声明(如ComponentScan或EnableXyz)。我更喜欢的是:>配置不需要bean方法。配置或多或少是定义应用程序配置的类的标识符。因为我可以想象有人可能对配置有错误的定义。quote来源:
@Configuration
public class AppConfig {
@Bean
public MyBean myBean() {
//instantiate, configure and return bean ...
}
}
@Configuration
@EnableWebSecurity
public class HelloWebSecurityConfiguration
extends WebSecurityConfigurerAdapter {
@Autowired
public void configureGlobal(AuthenticationManagerBuilder auth) {
auth
.inMemoryAuthentication()
.withUser("user").password("password").roles("USER");
}
}
@Order(1) 2
public static class ApiWebSecurityConfigurationAdapter extends WebSecurityConfigurerAdapter {
protected void configure(HttpSecurity http) throws Exception {
http
.antMatcher("/api/**") 3
.authorizeRequests(authorizeRequests ->
authorizeRequests
.anyRequest().hasRole("ADMIN")
)
.httpBasic(withDefaults());
}
}