静态工具类使用application.properties 中的配置

class AppleMaker {
   static String COLOR = "red";
   static Apple makeApple() {
       return new Apple(COLOR);
   }
}

class AppleMakerTest {
    void foo() {
        Apple apple = AppleMaker.makeApple();
    }
}

改造后:


====application.properties===
com.demo.apple-color=${APPLE_COLOR:red}



@Configuration
public class AppConfig {
    
    @Value("${com.demo.apple-color}")
    String appleColor;
    
    @Bean
    Apple makeApple() {
       return new Apple(appleColor);
    }
}

@Component
class AppleMakerTest {

    @Autowired
    Apple apple;
}

 

你可能感兴趣的:(#,java,web,-,configuration)