@allargsconstructor注解
它是lombok中的注解,作用在类上;
使用后添加一个构造函数,该构造函数含有所有已声明字段属性参数
==@NoArgsConstructor注解 ==
它是Lombok插件三种生成不同构造方法的注解,
使用后添加一个构造函数,生成一个无参数的构造方法
@Component和@Configuration作为配置类的差别
@Configuration
@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Component
public @interface Configuration {
@AliasFor(
annotation = Component.class
)
String value() default "";
}
可以看到在@Configuration注解中是包含@Component注解的,被@Configuration修饰的类被定义为一个Spring容器(应用上下文)
@Configuration就相当于Spring配置文件中的标签,里面可以配置bean
@Bean相当于Spring配置文件中的标签可以在Spring容器中注入一个bean
Spring发现方法所请求的Bean已经在容器中,那么就直接返回容器中的Bean。所以全局只有一个对象的实例。
造成这种差异的原因如下:
概括就是 @Configuration 中所有带 @Bean 注解的方法都会被动态代理,因此调用该方法返回的都是同一个实例。
其工作原理是:如果方式是首次被调用那么原始的方法体会被执行并且结果对象会被注册到Spring上下文中,之后所有的对该方法的调用仅仅只是从Spring上下文中取回该对象返回给调用者。
如果要使@Component的作用和@Configuration一样,可以使用@Autowired.
@EnableWebSecurity的作用
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.TYPE})
@Documented
@Import({WebSecurityConfiguration.class, SpringWebMvcImportSelector.class, OAuth2ImportSelector.class})
@EnableGlobalAuthentication
@Configuration
public @interface EnableWebSecurity {
boolean debug() default false;
}
首先,在@Import({WebSecurityConfiguration.class})处激活了WebSecurityConfiguration配置类,在这个配置类中, 注入了一个非常重要的bean, bean的name为: springSecurityFilterChain,这是Spring Secuity的核心过滤器, 这是请求的认证入口。
在@EnableGlobalAuthentication 处又使用了EnableGlobalAuthentication 注解, 注解源码为:
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.TYPE})
@Documented
@Import({AuthenticationConfiguration.class})
@Configuration
public @interface EnableGlobalAuthentication {
}
在这个注解中,激活了AuthenticationConfiguration配置类, 这个类是来配置认证相关的核心类, 这个类的主要作用是,向spring容器中注入AuthenticationManagerBuilder, AuthenticationManagerBuilder其实是使用了建造者模式, 他能建造AuthenticationManager, 这个类前面提过,是身份认证的入口。
所以,到这为止,EnableWebSecurity注解有两个作用,
1: 加载了WebSecurityConfiguration配置类, 配置安全认证策略。
2: 加载了AuthenticationConfiguration, 配置了认证信息。
持续跟新中。。。
因为是以前保存的草稿,具体从哪里的文章参考的不太清楚了,如果后面看到了原文章,会补上参考链接,主要是发布了之后好看些。。。