【发布时间】:2019-03-29 20:30:32
【问题描述】:
我尝试学习 SpEl,并从属性中设置对象字段值。但结果对象字段变为空。这是我的代码
配置类:
@Configuration
@ComponentScan(basePackageClasses = ExpressiveConfig.class)
@PropertySource("classpath:
/fourthversion/chapter3.placeholders/app.properties")
public class ExpressiveConfig {
@Bean
public
static PropertySourcesPlaceholderConfigurer placeholderConfigurer() {
return new PropertySourcesPlaceholderConfigurer();
}
}
类,我尝试在其中设置字段值:
@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。
【问题讨论】:
标签: spring properties spring-el