I am trying to use Spring to allow my Java classes to access a properties file. I have done quite a bit of googleing and there seems to be several ways of doing this. I have tried to use two of the different ways, and they are both failing.
Attempt 1
XML
<bean class="org.springframework.context.support.PropertySourcesPlaceholderConfigurer">
<property name="location" value=classpath:config.properties />
<property name="ignoreUnresolvablePlaceholders" value="true"/>
</bean>
Java
public class App
{
@Autowired
private static Environment env;
public static void main(String[] args)
{
System.out.println(env.getProperty("DatabaseName"));
}
}
Attempt 2
XML
<util:properties id="myProperties" location="classpath:config.properties"/>
Java
public class App
{
@Resource(name="myProperties")
private static Properties myProperties;
public static void main(String[] args)
{
System.out.println(myProperties.getProperty("DatabaseName"));
}
}
In both cases I get a Null Pointer Exception when calling the "getProperty" method. I am new to Spring and am guessing I am missing something simple. In addition to getting these attempts to work, I would like to know what is the "best" way to expose a properties file with Spring.
Thank you in advance for any help.