I have all my configuration details like (queries bean, url mapping etc) in one file (businesscaliber-servlet.xml) but i want separate out how do i do?
Using
<import resource="..."/>
See documentation. So in your businesscaliber-servlet.xml file, you could have:
<beans>
<import resource="queries-bean.xml"/>
<import resource="url-mappings.xml"/>
... etc ...
</beans>
Spring doesn't care which beans go into which file, they all get combined at runtime.
Related
I used the following configuration in my applicationContext.xml
<context:property-placeholder location="classpath:system.properties,file:/data/conf/system.properties,file:/data/conf/1033.properties" ignore-unresolvable="true" />
to load some placeholders:
use these properties defined in classpath:system.properties;
if file or properties exist in /data/conf/system.properties, use them instead of above;
if file or properties exist in /data/conf/1033.properties, use them instead of above.
Now Spring started ok if both /data/conf/system.properties and /data/conf/1033.properties exists, but it will throw rg.springframework.beans.factory.BeanInitializationException: Could not load properties; nested exception is java.io.FileNotFoundException: ... if either of them does not exist.
How to tell spring load these properties but ignore missing ones.
You have to add ignore-resource-not-found="true"
<context:property-placeholder location="classpath:system.properties,file:/data/conf/system.properties,file:/data/conf/1033.properties"
ignore-unresolvable="true"
ignore-resource-not-found="true"/>
I'm writing a WebApp back-end with spring in java. in the code there are a lot of magic numbers. is there a way to put this in a config in such a way that any changes in this config will be put in effect without restarting the entire app?
When java process start it loads spring context, once spring context is loaded it only reads properties file once so if you are changing any property you have to restart your app thats good to have way.
OR you can replace java.util.Properties with a PropertiesConfiguration from the Apache Commons Configuration project. It supports automatic reloading, either by detecting when the file changes, or by triggering through JMX.
one more alternate way is to keep all your prop variables in database and refresh your reference cache periodically that way you don't have to restart your app and can change properties on-fly from database.
You can call the configuration file through the below steps:
Use the #Configuration annotation for the class which calls the
config file.
Another annotation to be declared above the class for defining the path to the config file #PropertySource({"URL/PATH_OF_THE_CONFIG_FILE"})
#Value("${PROPERTY_KEY}") annotation above a variable where the value corresponding to the property_key needs to be assigned.
The the following bean in the same configuration invoking class.
#Bean
public static PropertySourcesPlaceholderConfigurer propertyConfigInDev() {
return new PropertySourcesPlaceholderConfigurer();
}
Make sure the #ComponentScan covers the folder where the config file is placed
Here is the way you can configure it
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p" xsi:schemalocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
<!--To load properties file -->
<bean id="placeholderConfig" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location" value="classpath:META-INF/*-config.properties">
</property></bean>
<bean id="createCustomer" class="com.example.Customer">
<property name="propertyToInject" value="${example.propertyNameUnderPropertyFile}">
</beans>
You can also refer it in java file straight away
public class Customer {
#Value("${example.propertyNameUnderPropertyFile}")
private String attr;
}
If I have a statement in my servlet.xml like this:
<import resource="classes/com/au/curtin/example.xml/>
and an example.xml at that directory location that has Spring bean definitions as well as things like <component:context-scan/> and <tx:annotation-driven/>. Are these statements imported along with the bean definitions into the servlet.xml context or do I need to include duplicate <component:context-scan/> in my servlet.xml file?
The main purpose of import is to avoid duplication. With import the file is completely imported.Everything including bean defination and other configurations are copied.
Before post this Question, I google to get Properties from Spring project(Its NOT web-based project). I am confused as every one are talking about application-context.xml and have configuration like
However, I am working on normal Java Project with Spring(NO Web-app and stuff like that). But I would like to get some common properties from properties file and that needs to be used in JAVA file. How can achieve this by using Spring/Spring Annotations?
Where I should configure myprops.properties file under my project and how to invoke through spring?
My understanding is application-context.xml is used ONLY for web based projects. If not, how should I configure this application-context.xml as I do NOT have web.xml to define the application-context.xml
You can create an XML based application context like:
ApplicationContext ctx = new ClassPathXmlApplicationContext("conf/appContext.xml");
if the xml file is located on your class path. Alternatively, you can use a file on the file system:
ApplicationContext ctx = new FileSystemXmlApplicationContext("conf/appContext.xml");
More information is available in the Spring reference docs. You should also register a shutdown hook to ensure graceful shutdown:
ctx.registerShutdownHook();
Next, you can use the PropertyPlaceHolderConfigurer to extract the properties from a '.properties' file and inject them into your beans:
<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations" value="classpath:com/foo/jdbc.properties"/>
</bean>
<bean id="dataSource" destroy-method="close" class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName" value="${jdbc.driverClassName}"/>
<property name="url" value="${jdbc.url}"/>
<property name="username" value="${jdbc.username}"/>
<property name="password" value="${jdbc.password}"/>
</bean>
Lastly, if you prefer annotation based config, you can use the #Value annotation to inject properties into you beans:
#Component
public class SomeBean {
#Value("${jdbc.url}")
private String jdbcUrl;
}
As of Spring 4, you can use the #PropertySource annotation in a Spring #Configuration class:
#Configuration
#PropertySource("application.properties")
public class ApplicationConfig {
// more config ...
}
If you would like to have your config outside of your classpath, you can use the file: prefix:
#PropertySource("file:/path/to/application.properties")
Alternatively, you can use an environmental variable to define the file
#PropertySource("file:${APP_PROPERTIES}")
Where APP_PROPERTIES is an environmental variable that has the value of the location of the property file, e.g. /path/to/application.properties.
Please read my blog post Spring #PropertySource for more information about #PropertySource, its usage, how property values can be overridden and how optional property sources can be specified.
You don't have to use Spring.
You can read with plain java like this:
Properties properties = new Properties();
properties.load(Thread.currentThread().getContextClassLoader().getResourceAsStream(fileName));
Can you figure out how your project will be used in the whole app? If your project is used as a build path for a web app and the configuration in your project is achieved through spring annotations, so no doubt you are puzzled about how to add an application.xml file. My suggest is you have to announce the guys who will use your project, tell them what you need and you just need to add #Value("${valuename}") in your code.
Create new property file inside your src/main/resources/ directory and file extension must be .properties e.g. db.properties
Write following context properties in your spring xml configuration file:
<context:property-placeholder location="db.properties"/>
Usage: ${property-key}
when I have a bean
<bean name="myBean" class="mypackage.myBean">
<property name="otherBean" ref="otherBeanRef" />
</bean>
and I click on otherBeanRef I'm redirected to definition of otherBeanRef, however this only works if its in the same file.
how to configure spring ide to also support other spring files?
You need to add both files to the Spring Spring Config File Set.
Spring Explorer/Properties/Config Sets