当你需要在xml中使用参数形式${hostName}获取某些动态值时,则需要在Spring初始化时就将动态值加载进去。
import org.springframework.context.ApplicationListener;
import org.springframework.context.event.ContextRefreshedEvent;
import org.springframework.stereotype.Component;
@Component
public class ApplicationInit implements ApplicationListener<ContextRefreshedEvent>{
@Override
public void onApplicationEvent(ContextRefreshedEvent contextRefreshedEvent) {
System.setProperty("hostName","127.0.0.1");System.setProperty("port","8090");
}
}