简体   繁体   English

Spring Boot 自定义注解

[英]Spring Boot custom annotations

In all of my projects I need the same WebSecurityConfig.在我的所有项目中,我都需要相同的 WebSecurityConfig。 So I copy the same implementation of extending WebSecurityConfigurerAdapter.所以我复制了扩展 WebSecurityConfigurerAdapter 的相同实现。

I like to add this to a common library I am using in every project and create a annotation for that.我喜欢将它添加到我在每个项目中使用的公共库中,并为此创建一个注释。 But the annotation class cannot have a superclass.但是注解类不能有超类。

I tried this:我试过这个:

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

Is there a better way to get this working?有没有更好的方法来让这个工作? I just want to add my SecurityConfiguration by adding an annotation or something similar.我只想通过添加注释或类似的东西来添加我的 SecurityConfiguration。

You should be able to just @Import your shared @Configuration class in every application that should use it.您应该能够在每个应该使用它的应用程序中@Import您共享的@Configuration类。

Another way would be to create a custom auto-configuration .另一种方法是创建自定义自动配置 This way, declaring a dependency on your shared library would suffice.这样,声明对共享库的依赖就足够了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM