zoukankan      html  css  js  c++  java
  • SPRING IN ACTION 第4版笔记-第三章ADVANCING WIRING-009-用SPEL给bean运行时注入依赖值

    1.When injecting properties and constructor arguments on beans that are created

    via component-scanning, you can use the @Value annotation, much as you saw earlier
    with property placeholders. Rather than use a placeholder expression, however, you
    use a S p EL expression. For example, here’s what the BlankDisc constructor might
    look like, drawing the album title and artist from system properties:

    public BlankDisc(
        @Value("#{systemProperties['disc.title']}") String title,
        @Value("#{systemProperties['disc.artist']}") String artist) {
        this.title = title;
        this.artist = artist;
    }

    2.In XML configuration, you can pass in the S p EL expression to the value attribute of

    <property> or <constructor-arg> , or as the value given to a p-namespace or c-
    namespace entry. For example, here’s the XML declaration of the BlankDisc bean
    that has its constructor arguments set from a S p EL expression:

    <bean id="sgtPeppers"
    class="soundsystem.BlankDisc"
    c:_title="#{systemProperties['disc.title']}"
    c:_artist="#{systemProperties['disc.artist']}" />
  • 相关阅读:
    06
    05
    继承
    0713作业
    0712作业
    0711作业
    福彩双色球作业
    0709作业
    选择语句+循环语句作业
    0706作业
  • 原文地址:https://www.cnblogs.com/shamgod/p/5238088.html
Copyright © 2011-2022 走看看