在Spring java配置中调用@Bean注释方法

我很好奇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.

上一篇:基于SpringBoot实现AOP+jdk/CGlib动态代理详解


下一篇:Spring