I created an spring xml config file with
<util:properties
id="com.abc.xyz.handler.abchandler"
location="classpath:/properties/myhandler_${spring.profile.active:e3}.properties" />
now from my java program when I am trying to get the property file like below:
Properties props = ((Properties).getBean(getClass().getName()));
it is saying NO bean with the name com.abc.xyz.handler.abchandler has been defined.
Please help !!
From your supplied code you are using getBean() but you have not passed in the properties file. Moreover, you have not declared the properties file as a bean in the spring-xml config file.
You could try accessing the properties file in this way:
Resource resource = new ClassPathResource("nameOfYourPropertiesFile.properties");
Properties properties = PropertiesLoaderUtils.loadProperties(resource);
Related
In my Spring proejct, I created a config.conf to store some variables.
And in a class I need to read these variables.
I am using the config library, https://github.com/lightbend/config.
And as the document says, I tried to used
private static final Config config = ConfigFactory.load(basename here goes here);
to load that config.conf file.
And by the document, https://lightbend.github.io/config/latest/api/com/typesafe/config/ConfigFactory.html#load-java.lang.String-, this method receives a string as classpath resource basename.
I am wondering how to get this "classpath resource basename" of the config.conf file?
My project's structure:
Thanks!
you can access file like this.
URL url = EmailService.class.getClassLoader().getResource("config.conf");
to load config, you can access path like below
config.load(url.getPath());
accessing property file from outside war file then am getting error message
its absolute path then working fine .Please help
<util:properties id="configs" location="${ext.prop.file}/config.properties" /> --Not working
ext.prop.file = "c:/test
<util:properties id="configs" location="file:/c:/test/config.properties" /> -- Working fine
You can do this by simply adding it to the context of the application at the time of context loading.
<context:property-placeholder location="${propfile}"/>
Above config will load the property file onto the context using the variable propFile. This variable is declared at runtime, as below.
-DpropFile="file:/path/to/file.properties"
Thereafter, you can use #Values annotation in your class to access properties from this file.
#Values("${someVal}")
private String someVal;
Above statement is trying to access property with key as someVal in property file and assign it to String someVal.
I need some help on injecting property value to a bean which is defined outside the web application.
The web application has a property file under src/main/resource.The spring application context xml has the property place holder defined as
<context:property-placeholder
location="classpath:test.properties,file:/etc/test1.properties"
ignore-resource-not-found="true"
/>
where test1.properties is another file which resides outside the application.The bean is injected with the property which is defined in the application (test.properties) ,but I want to inject the property that is defined in test1.properties (ideally the idea is to override the property values from application and read the one defined outside the application).
Thanks.
Hi use like below in applicationContext.xml
<util:properties id="property" location="classpath:test.properties"/>
In Java,
#Autowired
protected Properties property;
I guess this is what you are looking for
<context:property-placeholder location="file:c:/kp/sec.properties" order="1" ignore-resource-not-found="true" ignore-unresolvable="true" />
<context:property-placeholder location="classpath:kp-props.properties" order="2" />
If the file sec.properties exists take the value from sec.properties, if file or properties does not exist take the property from kp-props.properties file from resources directory(if the property is not found in either of place application will fail)
And say you have property my.prop and you can inject the property as follows.
#Component
public class KPProps {
#Value("${my.prop}")
private int props;
public void print(){
System.out.println(props);
}
}
I am trying to find out the syntax for specifying the spring's XML file in the constructor of ClassPathXmlApplicationContext. By syntax I don't mean the method signature but the actual string
For example the following three work.
ApplicationContext context = new ClassPathXmlApplicationContext("com/anshbansal/alarm/alarm.xml");
ApplicationContext context = new ClassPathXmlApplicationContext("classpath:alarm.xml");
ApplicationContext context = new ClassPathXmlApplicationContext("alarm.xml");
I have googled and tried to go through the Spring 3.1.0 source code also. But I got stuck after doResolvePlaceholders method in org.springframework.core.env.AbstractPropertyResolver class of Spring. Specificaly I was not able to understand how placeholders are resolving to the path.
Can anyone share what is the syntax for the string to specify the xml file?
EDIT
I mean the syntax to specify path to spring xml file like in the constructor. I do not mean the syntax of the xml file itself.
Ok, I understand the question now :-). PropertyResolver is used only to put environment values (or property file values) into spring XML file, i.e.:
<context:property-placeholder location="file:///some/path/file.properties"/>
and then resolving them inside this spring xml, i.e.:
<bean id="mailInviteMessage" class="org.springframework.mail.SimpleMailMessage">
<property name="from" value="${mail.from}"/>
<property name="subject" value="${mail.subject}"/>
</bean>
Putting spring XML in the classpath and naming in standard convention (i.e. beans.xml) is preferable. However, you may configure it putting a parameter to java invocation, i.e.
java -Dmy.parameter=/path/to/beans.xml ...
and then loading it manually:
Context context = new FileSystemXmlApplicationContext(System.getProperty("my.parameter"));
I have few properties in my Properties file like
dataCenterOptions.host="xyz"
dataCenterOptions.user="abc"
dataCenterOptions.password="def"
dataCenterOptions.port="ghi"
I'm using this is in my Spring class by annotating it with #Value. However instead of creating four separate variables can I have a single variable to access each of the following variables. Something like
#Value("${dataCenterOptions}")
Properties dataCenterProps;
How do I do this?
Oneway is
At code
#Resource(name="prop")
Properties prop;
At config file
<util:properties id="prop" location="properties/dataCenterOptions.properties" />