属性文件在controller层中的应用

设置一个env.properties文件后,如果在service中应用,则

@Value("${image_url}")  //属性名为image_url
    private String  IMAGE_URL;

在applicationContext.xml配置文件中添加其属性文件

<context:property-placeholder location="classpath:*.properties"/>

如果在controller层中应用,由于controller层加载时spring配置文件还未加载,所以不能直接使用,需要额外在springmvc-servlet.xml配置属性文件

<context:property-placeholder location="classpath:env.properties"/>

然后再controller中像以上一样的方法使用

猜你喜欢

转载自www.cnblogs.com/psxfd4/p/11668056.html