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']}" />

 

posted @ 2016-03-03 12:11  shamgod  阅读(223)  评论(0编辑  收藏  举报
haha