@Configuration的proxyBeanMethods的意义

一、首先理解@Bean注解

1、@Bean表示方法产生一个由Spring管理的bean,属性的名称语义与Spring XML中的 标签配置的一样,源码如下:

  @Target({ElementType.METHOD, ElementType.ANNOTATION_TYPE})
  @Retention(RetentionPolicy.RUNTIME)
  @Documented
  public @interface Bean {
      @AliasFor("name")
      String[] value() default {};


      @AliasFor("value")
      String[] name() default {};


      @Deprecated
      Autowire autowire() default Autowire.NO;


      boolean autowireCandidate() default true;


      String initMethod() default "";


      String destroyMethod() default AbstractBeanDefinition.INFER_METHOD;
 }

@Bean 用在方法上(此方法需要有返回值),表示可以通过编程手段自行对需要初始化的对象进行赋值。

例如:

@Bean
public RedisTemplate<Object, Object> redisTemplate(RedisConnectionFactory connectionFactory)
{
    RedisTemplate<Object, Object> template = new RedisTemplate<>();
    template.setConnectionFactory(connectionFactory);

    FastJson2JsonRedisSerializer serializer = new FastJson2JsonRedisSerializer(Object.class);

    ObjectMapper mapper = new ObjectMapper();
    mapper.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
    mapper.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);
    serializer.setObjectMapper(mapper);

    template.setValueSerializer(serializer);
    // 使用StringRedisSerializer来序列化和反序列化redis的key值
    template.setKeySerializer(new StringRedisSerializer());
    template.afterPropertiesSet();

    return template;
}

2、IOC容器会扫描到有@Bean注解的方法,会将此方法的返回值作为一个bean管理起来,并会执行整个bean生命周期。

二、@Configuration注解

@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Component
public @interface Configuration {

    @AliasFor(annotation = Component.class)
    String value() default "";

    boolean proxyBeanMethods() default true;

}

使用proxyBeanMethods的流程图如下,具体的可以阅读源码

image.png
void validate(ProblemReporter problemReporter) {
        // A configuration class may not be final (CGLIB limitation) unless it declares proxyBeanMethods=false
        Map<String, Object> attributes = this.metadata.getAnnotationAttributes(Configuration.class.getName());
        if (attributes != null && (Boolean) attributes.get("proxyBeanMethods")) {
            if (this.metadata.isFinal()) {
                problemReporter.error(new FinalConfigurationProblem());
            }
            for (BeanMethod beanMethod : this.beanMethods) {
                beanMethod.validate(problemReporter);
            }
        }
    }

测试代码,可调整SpringConfig类上的proxyBeanMethods的值,观察输出结果


public class User {

    String id;
    String name;

    public User(String id, String name) {
        this.id = id;
        this.name = name;
    }

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
}


@Configuration(proxyBeanMethods = false)
public class SpringConfig {

    @Bean
    public User myUser() {
        User user = new User("1","green");
        return user;
    }
}

@SpringBootApplication(scanBasePackages={"cn.hutool.extra.spring"})
public class MySpringApplication {

    public static void main(String[] args) {
        SpringApplication.run(MySpringApplication.class,args);

        User user1 = SpringUtil.getBean(User.class);
        User user2 = SpringUtil.getBean(User.class);

        System.out.println(user1);
        System.out.println(user2);

        SpringConfig config = SpringUtil.getBean(SpringConfig.class);
        User user3 = config.myUser();
        System.out.println(user3);

        User user4 = SpringUtil.getBean(User.class);
        System.out.println(user4);
    }
}

最终结论:
1、配置类上有了proxyBeanMethods=false后,表示配置类不会被代理,可以提高容器的初始化性能。其差别,主要再使用@Bean的方法上

(1)proxyBeanMethods=true,会给config类生成代理对象,当直接调用方法时,不会生成新的对象,会从spring 容器中获取
(2)proxyBeanMethods=false,不会给config类生成代理对象,当直接调用方法时,会生成新的对象,

2、如果配置类中的方法没有调用关系,则可以把proxyBeanMethods 设置为 false。如果存在以下情况,则需要将proxyBeanMethods 设置为 true(如果是fasle,Idea中会提示编译报错)

@Bean
    public User hisUser() {
        // 内部调用
        User yourUser = this.yourUser();
        User hUser = new User("2","his");
        hUser.setName(hUser.getName() + yourUser.getName());
        return hisUser();
    }
    
    @Bean
    public User yourUser() {
        User user = new User("1","your");
        return user;
    }
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容