首页 > 解决方案 > Spring Boot 自定义注解

问题描述

在我所有的项目中,我都需要相同的 WebSecurityConfig。所以我复制了扩展 WebSecurityConfigurerAdapter 的相同实现。

我喜欢将它添加到我在每个项目中使用的公共库中,并为此创建一个注释。但是注解类不能有超类。

我试过这个:

@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
@Configuration
@EnableWebSecurity
public @interface EnableGlobalSecurity extends WebSecurityConfigurerAdapter {
}

有没有更好的方法让这个工作?我只想通过添加注释或类似的东西来添加我的 SecurityConfiguration。

标签: javaspring-bootannotations

解决方案


您应该能够在每个应该使用它的应用程序中@Import您的共享类。@Configuration

另一种方法是创建自定义自动配置。这样,声明对共享库的依赖就足够了。


推荐阅读