利用*.properties定义文件对类进行初始化或通过xml注入

前端之家收集整理的这篇文章主要介绍了利用*.properties定义文件对类进行初始化或通过xml注入前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
  1. public class EhCache*** implements PCache {
  2. private int maxElementsInMemory = Integer.parseInt(ResourceBundle.getBundle("cache")
  3. .getString("cache.maxElementsInMemory").toString());
  4. private boolean overflowToDisk = Boolean.getBoolean(ResourceBundle.getBundle("cache")
  5. .getString("cache.overflowToDisk").toString());
  6. private boolean eternal = Boolean.getBoolean(ResourceBundle.getBundle("cache")
  7. .getString("cache.eternal").toString());
  8. private long timeToIdleSeconds = Long.parseLong(ResourceBundle.getBundle("cache")
  9. .getString("cache.timeToIdleSeconds").toString());;
  10. private long timeToLiveSeconds = Long.parseLong(ResourceBundle.getBundle("cache")
  11. .getString("cache.timeToLiveSeconds").toString());;
  12. Cache cache = null;
  13. public EhCache***(String cacheName){
  14. cache = new Cache(cacheName,this.getMaxElementsInMemory(),this.isOverflowToDisk(),this.isEternal(),this.getTimeToLiveSeconds(),this.getTimeToIdleSeconds());
  15. }

不过最好用Sring的Bundle


也可以这样初始化

  1. public class EhCache** implements PCache {
  2. 	private  int maxElementsInMemory;
  3. 	private  boolean overflowToDisk;
  4. 	private  boolean eternal;
  5. 	private  long timeToIdleSeconds;
  6. 	private  long timeToLiveSeconds;
  7. 	private String cacheName = "";
  8.     CacheManager manager = CacheManager.create();
  9.     private  Cache cache = null;
  10.     
  11. 	public EhCache**(String cacheName){
  12. 		this.cacheName = cacheName;
  13. 	}
  14. 	public void init(){		
  15. 	     cache = new Cache(this.cacheName,this.getTimeToIdleSeconds());
  16. 			
  17. 	}
  18. }
  19. xml中,通过注入属性对其进行初始化

  20. <bean id="ehCache" class="com.yk.platform.cache.impl.EhCache*" scope="singleton" init-method="init">
  21.            <constructor-arg type="java.lang.String">
  22.              <value>newCache</value>
  23.            </constructor-arg>
  24.            <property name = "maxElementsInMemory" value="500"></property>
  25.            <property name = "eternal" value="false"></property>
  26.            <property name = "timeToIdleSeconds" value="300"></property>
  27.            <property name = "timeToLiveSeconds" value="1200"></property>
  28.            <property name = "overflowToDisk" value="true"></property>
  29.      </bean>
  30.     
  31.         
  32.   

猜你在找的XML相关文章