代码之家  ›  专栏  ›  技术社区  ›  Alex R

在springboot2.x中设置@Bean属性的更具声明性、更不冗长的方法?

  •  0
  • Alex R  · 技术社区  · 6 年前

    我的问题已经被别人问过了,11年前这里有了答案:

    How can I inject a property value into a Spring Bean which was configured using annotations?

    @Configuration
    public class Config {
    
        private @Value("${region}") String region;
    
        private @Value("${bucket}") String bucket;
    
        @Bean
        AwsS3Template getAwsS3Template() {
            AwsS3Template s3 = new AwsS3Template();
            s3.setRegion(region);
            s3.setBucket(bucket);
            return s3;
        }
    
    }
    

    @Configuration
    public class Config {
    
        @Bean
        @AutoApplyPropertyValues /* pseudo-code, no such annotation exists */
        AwsS3Template getAwsS3Template() {
            return new AwsS3Template();
        }
    
    }
    
    0 回复  |  直到 6 年前
        1
  •  0
  •   Alex R    6 年前

    从评论中

    @Configuration
    public class Config {
    
        @Bean
        @ConfigurationProperties
        AwsS3Template getAwsS3Template() {
            return new AwsS3Template();
        }
    
    }