在Spring java配置中调用@Bean注释方法
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了在Spring java配置中调用@Bean注释方法,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含2066字,纯文字阅读大概需要3分钟。
内容图文
![在Spring java配置中调用@Bean注释方法](/upload/InfoBanner/zyjiaocheng/713/fdab6f8450df4fc1ab3f4ce66fa70f84.jpg)
我很好奇spring spring如何使用@Bean注释处理调用方法.如果我在方法上放置一个@Bean注释,并返回一个实例,我理解这会告诉spring通过调用该方法并获取返回的实例来创建一个bean.但是,有时该bean必须用于连接其他bean或设置其他代码.通常的方法是调用@Bean带注释的方法来获取实例.我的问题是,为什么这不会导致有多个bean浮动的实例?
例如,请参阅下面的代码(取自另一个问题). 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();
}
解决方法:
是的,Spring确实有些神奇.检查Spring Docs:
This is where the magic comes in: All
@Configuration
classes are subclassed at startup-time with 07001. In the subclass, the child method checks the container first for any cached (scoped) beans before it calls the parent method and creates a new instance.
这意味着对@Bean方法的调用是通过CGLIB代理的,因此返回bean的缓存版本(不创建新的版本).
@Beans的默认范围是SINGLETON,如果指定不同的范围(如PROTOTYPE),则调用将传递给原始方法.
请注意,这对静态方法无效.根据春季文档:
Calls to static
@Bean
methods never get intercepted by the container, not even within@Configuration
classes (as described earlier in this section), due to technical limitations: CGLIB subclassing can override only non-static methods. As a consequence, a direct call to another@Bean
method has standard Java semantics, resulting in an independent instance being returned straight from the factory method itself.
内容总结
以上是互联网集市为您收集整理的在Spring java配置中调用@Bean注释方法全部内容,希望文章能够帮你解决在Spring java配置中调用@Bean注释方法所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。