Spring通過類方法載入配置檔案
阿新 • • 發佈:2019-02-12
通過呼叫某個類中的方法通過流的方式獲取到配置檔案
public static Properties getRedisResourcePath(){ String filePath = Config.getConfigFolder() + "redis.properties"; Properties properties = new Properties(); try { properties.load(new FileInputStream(new File(filePath))); } catch (IOException e) { logger.error("redis 配置配置檔案載入失敗~~ ", e); } return properties; } 下面的bean實體實現了配置資訊的讀入,如此配置中的就可以再下面的beans實體中可以呼叫了 <bean id="refRedisProperties" class="org.springframework.beans.factory.config.PropertiesFactoryBean"> <property name="properties"> <value>#{T(com.utils.RedisConfig).getRedisResourcePath()}</value> </property> </bean> 下面redis的 配置資訊通過配置引用的方式,通過bean物件獲取 <context:property-placeholder properties-ref="refRedisProperties" /> <!-- 配置redis 例項--> <bean id="jedisPoolConfig" class="redis.clients.jedis.JedisPoolConfig"> <property name="timeBetweenEvictionRunsMillis"value="${redis.pool.timeBetweenEvictionRunsMillis}" /> <property name="minEvictableIdleTimeMillis" value="${redis.pool.minEvictableIdleTimeMillis}" /> <property name="maxIdle" value="${redis.pool.maxIdle}"/> <property name="minIdle" value="${redis.pool.minIdle}"/> <property name="maxTotal" value="${redis.pool.maxTotal}"/> <property name="maxWaitMillis" value="${redis.pool.maxWait}"/> <property name="testOnBorrow" value="${redis.pool.testOnBorrow}"/> <property name="testOnReturn" value="true"/> <property name="testWhileIdle" value="true"/> </bean>
<!-- redis的連線池pool,不是必選項:timeout/password --> <bean id = "jedisSentinelPool" class="redis.clients.jedis.JedisSentinelPool" destroy-method="destroy"> <constructor-arg index="0" value="${redis.sentinels.masterName}" /> <constructor-arg index="1"> <set> <value>${redis.sentinels.server1}</value> <value>${redis.sentinels.server2}</value> </set> </constructor-arg> <constructor-arg index="2" ref="jedisPoolConfig" /> <constructor-arg index="3" value="3000" type="int"/> </bean>