Utilizando Singleton para um arquivo .properties [RESOLVIDO]

3 respostas
Arizoide

Boa tarde galera…

Estou com uma dúvida e queria saber se vocês podem me ajudar.

Eu preciso criar uma classe com o padrão Singleton que defina a instância de um arquivo .properties meu para eu não ter que ficar criando instâncias toda que vez que fora utilizar o mesmo.

Alguém poderia me ajudar?

O código que repito é o seguinte:

File file = new File( "C:\\Users\\athomazini\\Desktop\\spring-workspace\\HelloWorldVelocity\\mensagens.properties"); Properties props = new Properties(); FileInputStream fis = null; fis = new FileInputStream(file); props.load(fis);

Obrigado desde já.

Atenciosamente,

3 Respostas

D
public class ConfigPropriedades {

	private static Properties configProperties;
	
	
	public static String PATH_CONFIG_PROPERTIES 		= "resources" + File.separator + "config.properties";

	private static long dataArquivoConfig;

	

	public static Properties getConfigProperties() {
		
		File configFile = new File(PATH_CONFIG_PROPERTIES);	
		
		long dataModificacao = configFile.lastModified();
		
		if(configProperties == null || (dataArquivoConfig != dataModificacao))
		{
			if(configProperties == null)
			{
				configProperties = new Properties();
			}
			else{
				configProperties.clear();
			}
		
			try {
				
				configProperties.load(new FileInputStream(new File(PATH_CONFIG_PROPERTIES)));
				
				dataArquivoConfig = configFile.lastModified();
				
			} catch (IOException e) {
				e.printStackTrace();
			}
		}

		return configProperties;
	}

	public static void salvarConfigProperties(String comentario) throws FileNotFoundException, IOException
	{
		configProperties.store(new FileOutputStream(PATH_CONFIG_PROPERTIES), comentario);
	}
}

para usar vc faz

ConfigPropriedades.getConfigProperties().getProperty(property);
regifelix
package br.com.dsf.projeto;

import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

public class Singleton {

	private static Singleton instance;

	private String usuario;
	private String nome;

	private Singleton() {
		Properties propriedades = new Properties();
		InputStream stream;

		String arquivo = "nome_arquivo.properties";

		ClassLoader classLoader = ClassLoader.getSystemClassLoader();
		stream = classLoader.getResourceAsStream(arquivo);

		if (stream == null) {
			stream = br.com.dsf.projeto.Singleton.class.getClassLoader()
					.getResourceAsStream(arquivo);
		}

		if (stream == null) {
			System.out
					.println("Erro ao carregar arquivo de configurações "
							+ arquivo
							+ " verifique se o arquivo esta numa package que esteja como Source Folder. ");
		}

		try {

			propriedades.load(stream);
			usuario = propriedades.getProperty("usuario");
			nome = propriedades.getProperty("nome");
		} catch (IOException e) {
			usuario = null;
			nome = null;
		}

	}

	public static Singleton getInstance() {
		if (instance == null)
			instance = new Singleton();
		return instance;
	}

	public String getUsuario() {
		return usuario;
	}

	public void setUsuario(String usuario) {
		this.usuario = usuario;
	}

	public String getNome() {
		return nome;
	}

	public void setNome(String nome) {
		this.nome = nome;
	}

}
Arizoide

Valeu galera.

Consegui.

Criado 6 de dezembro de 2012
Ultima resposta 6 de dez. de 2012
Respostas 3
Participantes 3