Spring:通过属性文件/环境变量进行动态命名

时间:2015-12-23 09:45:42

标签: spring spring-mvc properties

对于我们的一个Web应用程序,我们使用基于tomcat环境的动态加载属性文件。

在tomcat中我们设置环境变量 -Dcustom.env.name =“production”,因此根据环境,通过Spring的PropertyPlaceholderConfigurer获取并加载相应的属性文件。

 <bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
    <property name="locations">
      <list>
        <value>classpath:jdbc_${custom.env.name}.properties</value>
        <value>classpath:configuration_${custom.env.name}.properties</value>
      </list>
    </property>
 </bean>

现在由于某种原因,我们希望通过驻留在war文件之外的文件系统上的另一个属性文件来指定 custom.env.name 属性。以最少的代码更改来实现此目的的方法是什么。

1 个答案:

答案 0 :(得分:0)

使用import java.awt.*; import javax.swing.*; import java.awt.event.*; import java.awt.image.*; import javax.imageio.*; import java.io.*; import java.util.*; import java.awt.font.*; import java.awt.geom.*; class TextToImageDemo { public static void main(String[] args) throws IOException { String sampleText = "SAMPLE TEXT",s="ALPHA"; BufferedReader br = null; try { br = new BufferedReader(new FileReader("E:\\Java\\file.txt")); while ((sampleText = br.readLine()) != null) { System.out.println(sampleText); s=sampleText; } } catch (IOException e) { e.printStackTrace(); } finally { try { if (br != null)br.close(); } catch (IOException ex) { ex.printStackTrace(); } } String fileName = "Image"; File newFile= new File("./" + fileName + ".jpeg"); Font font = new Font("Stencil", Font.PLAIN, 100); FontRenderContext frc = new FontRenderContext(null, true, true); BufferedImage image = new BufferedImage(2000, 200, BufferedImage.TYPE_INT_RGB); Graphics2D g = image.createGraphics(); System.out.println(s); g.setColor(Color.WHITE); g.fillRect(0, 0, 2000, 200); g.setColor(Color.BLACK); g.setFont(font); g.drawString(s, 800,150); g.dispose(); try { FileOutputStream fos = new FileOutputStream("E:\\Java\\1111.jpg"); ImageIO.write(image,"jpg",fos); fos.close(); } catch(Exception ex) { ex.printStackTrace(); } } } 阅读此媒体资源。