例如,请参阅下面的代码(取自另一个问题). entryPoint()方法用@Bean注释,所以我想spring会创建一个BasicAuthenticationEntryPoint的新实例作为bean.然后,我们在配置块中再次调用entryPoint(),但似乎entryPoint()返回bean实例,并且多次调用(我尝试记录,只有一个日志条目).我们可能会在配置的其他部分多次调用entryPoint(),并且我们总是会获得相同的实例.我对此的理解是否正确? Spring是否对使用@Bean注释的方法进行了一些神奇的重写?
@Bean
public BasicAuthenticationEntryPoint entryPoint() {
BasicAuthenticationEntryPoint basicAuthEntryPoint = new BasicAuthenticationEntryPoint();
basicAuthEntryPoint.setRealmName("My Realm");
return basicAuthEntryPoint;
}
@Override
protected void configure(HttpSecurity http) throws Exception {
http
.exceptionHandling()
.authenticationEntryPoint(entryPoint())
.and()
.authorizeUrls()
.anyRequest().authenticated()
.and()
.httpBasic();
}
本文由 投稿者 创作,文章地址:https://blog.isoyu.com/archives/zaispring-javapeizhizhongdiaoyongbeanzhushifangfa.html
采用知识共享署名4.0 国际许可协议进行许可。除注明转载/出处外,均为本站原创或翻译,转载前请务必署名。最后编辑时间为:9 月 18, 2019 at 09:58 下午