构造函数中使用Spring @Value注解

xkrivzooh2019年7月23日
小于 1 分钟

构造函数中使用Spring @Value注解

如果想在构造函数中使用的@value注解的话,demo如下:

// File: sample/Message.groovy
package sample

import org.springframework.beans.factory.annotation.*
import org.springframework.stereotype.*

@Component
class Message {

    final String text

    // Use @Autowired to get @Value to work.
    @Autowired
    Message(
        // Refer to configuration property
        // app.message.text to set value for 
        // constructor argument message.
        @Value('${app.message.text}') final String text) {
        this.text = text
    }

}
上次编辑于: 2022/9/13 17:40:58
Loading...