Spring3中的@value注解

在spring 3.0中,可以通过使用@value,对一些如xxx.properties文件
中的文件,进行键值对的注入,例子如下:

1 首先在applicationContext.xml中加入:
Java代码 收藏代码
  1. <beans xmlns:util="http://www.springframework.org/schema/util"   
  2.     xsi:schemaLocation="http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.1.xsd">   
  3. </beans> 
 
 
   的命名空间,然后

2
Java代码 收藏代码
  1. <util:properties id="settings" location="WEB-INF/classes/META-INF/spring/test.properties" />   


3 创建test.properties
   abc=123

4
Java代码 收藏代码
  1. import org.springframework.beans.factory.annotation.Value;    
  2. import org.springframework.stereotype.Controller;    
  3. import org.springframework.web.bind.annotation.RequestMapping;    
  4.    
  5. @RequestMapping("/admin/images")    
  6. @Controller    
  7. public class ImageAdminController {    
  8.    
  9.     private String imageDir;    
  10.            @Value("#{settings['test.abc']}")    
  11.     public void setImageDir(String val) {    
  12.         this.imageDir = val;    
  13.     }    
  14.    
  15.  
  16. }   

这样就将test.abc的值注入了imageDir中了
【转载地址】
http://jackyrong.iteye.com/blog/1330946

你可能感兴趣的:(Spring3中的@value注解)