拼写@Value 设置为空

Spel @Value sets null

我尝试学习 SpEl,并从属性中设置对象字段值。但结果对象字段变为空。这是我的代码

配置class:

@Configuration
@ComponentScan(basePackageClasses = ExpressiveConfig.class)
@PropertySource("classpath:
/fourthversion/chapter3.placeholders/app.properties")

public class ExpressiveConfig {

@Bean
public
static PropertySourcesPlaceholderConfigurer placeholderConfigurer() {
    return new PropertySourcesPlaceholderConfigurer();
}
}

Class,我尝试设置字段值的地方:

@Component
public class BlankDisc implements CompactDisc {

private String title;
private String artist;

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

}
getters and setters
}

这是我的测试:

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = ExpressiveConfig.class)
public class BlankDiskTest {

@Autowired
private BlankDisc blankDisc;

@Test
public void isBlankDiscNotNull() {
    assertNotNull(blankDisc);
    System.out.println(blankDisc.getTitle());
    System.out.println(blankDisc.getArtist());
}
}

测试通过,对象不为空,但系统打印为空,空 为什么字段值没有设置?

PS

如果我这样做但是这样:

public BlankDisc(@Value("${disc.title}") String title, 
@Value("${disc.artist}") String artist) {
    this.title = title;
    this.artist = artist;

}

那么就OK了。

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

}

因为你从系统属性中初始化数据,所以你需要确保它存在

@Test
public void isBlankDiscNotNull() {
    assertNotNull(blankDisc);
    assertNotNull(System.getProperty("disc.title"));
    assertNotNull(System.getProperty("disc.artist"));
    assertEquals(blankDisc.getTitle(),System.getProperty("disc.title"));
    assertEquals(blankDisc.getArtist(),System.getProperty("disc.artist"));
    System.out.println(blankDisc.getTitle());
    System.out.println(blankDisc.getArtist());
}