I'm building my project with maven so according to maven way, config should be in src/main/conf , how can I say to my spring application context that that is where jdbc.properties is found? Here is example bean :
<bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location" value="jdbc.properties" />
</bean>
Spring assumens that this configuration is inside src/main/webapp/WEB-INF, I hope I've been clear if not I'll rephrase my question thank you
I'm building my project with maven so
according to maven way, config should
be in src/main/conf
Actually, configuration data should generally go in src/main/resources, that way it will be on the classpath and you can reference your property file like:
<property name="location" value="classpath:jdbc.properties" />
I think it is not quite clear as to what a "config file" means. I am thinking it is the config files used by the other maven plugins (such as surefire plugins, assembly plugins etc).
Surely in the 10+ web app projects that I have worked the bean files, jdbc.properties files have all been under src/main/resources
Try this
<bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location" value="src/main/config/jdbc.properties" />
</bean>
Related
I have a problem with spring service import problem through xml files.
A.xml
<bean id="AtomService" class="{class}">
<property name="sqlSession" ref="sqlSession"/>
</bean>
B.xml
<bean id="BtomService" class="{class}">
<property name="AtomService" ref="AtomService"/>
</bean>
I have tried to put import syntax in B.xml file like (path was accurate)
<import resource:~~A.xml>
and to configure Beans support(config Sets) tab on STS. but they were not helpful.
Error occurs log is not about beans but make nullPointerException for variables referenced.
If you have any solutions please let me know..
I'm facing an issue with spring placeholder configuration. I've searched the web trying to find a solution but nothing worked for me at all.
We had used to use spring configurer for loading our .properties files and everything worked fine since the configfiles where located in META-INF dir.
Now we need to have our config files located in /etc/sep/properties directory or in some other filesystem directory.
I tried to use
<context:property-placeholder location="file:/etc/sep/properties/jdbc.properties" />
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName" value="${jdbc.driverClassName}" />
<property name="url" value="${jdbc.databaseurl}" />
<property name="username" value="${jdbc.username}" />
<property name="password" value="${jdbc.password}" />
<property name="initialSize" value="${jdbc.initialPoolSize}" />
</bean>
the content of /etc/sep/properties/jdbc.properties is following:
cat /etc/sep/properties/jdbc.properties
jdbc.driverClassName= org.postgresql.Driver
jdbc.dialect=org.hibernate.dialect.PostgreSQLDialect
jdbc.databaseurl=jdbc:postgresql://localhost:5432/sep?characterEncoding=utf8&autoReconnect=true
jdbc.username=****
jdbc.password=****
I also tried using another approach as folows, but it worked for me neither.
<context:property-placeholder properties-ref="prop" />
<util:properties id="prop" location="reso"/>
<bean id="reso" class="org.springframework.core.io.FileSystemResource">
<constructor-arg index="0" value="/etc/sep/properties/jdbc.properties" />
</bean>
I don't know if it matters but we are using maven building, so that the application-context.xml is placed in core-lib.jar which is used in our web-app as dependency. Other config, such as logging work great.
I would be grateful for any suggestions.
This is just an idea but it may work,
Have you checked that the .property file has the correct access rights? I mean, is it accessible by the user that runs your Spring application?
It would help a lot if you show the error displayed.
Ok, I've finally resolved it. There were two things about it.
At First: My tomcat server was not updating deployed files properly.
And finally I'm not pretty sure if it helped, but we added one more slash after file: specification, so that the result was:
<context:property-placeholder location="file:///etc/sep/properties/*.properties" />
Now it is loading all config files properly.
I'm having problems taking the message values to a properties file.
I'm using intelliJ IDEA and i have a package
com.test.messages
and inside I have the messages.properties file.
Here is my xml
<bean id="messageSource" class="org.springframework.context.support.ResourceBundleMessageSource">
<property name="basename" value="com.test.messages.messages"/>
</bean>
When I restart the server this is the WARNING i get
WARNING: ResourceBundle [com.test.messages.messages] not found for MessageSource: Can't find bundle for base name com.test.messages.messages, locale en_US
Any ideas?
Do not keep properties in packages.
Resources directory is a standart place to keep internal properties.
/src/main/resources
or
/src/test/resources
If you want to keep properties separated from the project - use something this kind of spring configuration:
<bean id="props" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer" >
<property name="ignoreResourceNotFound" value="true"/>
<property name="localOverride" value="true"/>
<property name="locations">
<list>
<value>classpath*:messages.properties</value>
<value>file:messages.properties</value>
</list>
</property>
</bean>
So you'd be able to override internal properties with external (don't forget to add the to a classpath using -cp).
Anyway the declaration of the been will look like this:
<bean id="messageSource" class="org.springframework.context.support.ResourceBundleMessageSource">
<property name="basename" value="${property.name}"/>
</bean>
you should have a file in your project as such:
src\main\resources\com\test\messages.properties
Although I am not IDEA expert, I know that resources such as .property files placed in the source code hierarchy are copied to the classpath when using the IDE alone. However, if you use maven, then it takes over the build and whatever is in the java path is not copied by default. You have to place all your classpath resources in src\main\resources and under whatever hierarchy mirrors the package name to have the same result. So in your case :
src\main\resources\com\test\messages\messages.properties
and maven will copy it properly
I know this is old question, but let me drop my solution :)
Source folder (resources is not package):
/src/main/java/
somepackage/abc.java
/src/main/resources/
messages.properties
Eclipse default output java build path:
myProject/build/classes
Eclipse default deploy path:
WEB-INF/classes
My project's spring context XML file path:
/src/main/webapp/WEB-INF/context.xml
Inject resource bundles,
<bean id="messageSource" class="org.springframework.context.support.ResourceBundleMessageSource">
<property name="basenames" value="messages"/>
</bean>
As Azee said keeping properties in package is not encourage. In case, messages.properties is in resources package as below:
/src/resources/messages.properties
context.xml should be like:
<property name="basenames" value="resources/messages"/>
I am following the following article.
http://www.mkyong.com/spring/spring-quartz-scheduler-example/
Everything works fine.
<bean id="simpleTrigger"
class="org.springframework.scheduling.quartz.SimpleTriggerBean">
<property name="jobDetail" ref="runMeJob" />
<property name="repeatInterval" value="5000" />
<property name="startDelay" value="1000" />
</bean>
I created a property file app.properties which has
repeatInterval = 5000
startDelay = 1000
I want to load these data into bean properties. Right now I have to hard code the values into the xml file.
I want to be able to load the data from property file into the bean properties. Is it possible?
EDIT:
I have
<property name="repeatInterval" value="5000" />
What I am looking for is a way to make it
<property name="repeatInterval" value= "get 5000 from property file" />
To find a file myPropertyFileName.properties that is on your classpath and load it into your spring config, create the following bean:
<bean id="myPropertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location" value="classpath:myPropertyFileName.properties"/>
<property name="placeholderPrefix" value="${props:"/>
</bean>
Then use a property name defined like
repeatInterval=5000
like this:
<property name="repeatInterval" value="${props:repeatInterval}"/>
Use Spring propertyPlaceholderConfigurer to achieve this. Follow this guide.
I have run into something similar in the past. I needed to load a bunch of beans using Spring but I wanted them to be user editable bean files. So I didn't want to include them in the jar packaging. What I did was create my user-defined bean files outside the jar but in a know relative location. My packaged bean definition file referenced the beans defined in the user-defined bean file and when I loaded the application context I provided both files (user-defined & packaged).
Bit unorthodox but it worked.
I've got a question regarding the difference between PropertyPlaceholderConfigurer (org.springframework.beans.factory.config.PropertyPlaceholderConfigurer) and normal filters defined in my pom.xml.
I've been looking at examples, and it seems that even though filters are defined and marked to be active by default in the pom.xml they still make use of PropertyPlaceholderConfigurer in Spring's applicationContext.xml.
This means that the pom.xml has a reference to a filter-LOCAL.properties while applicationContext.xml has a reference to application.properties and they both contain the same settings.
Why is that? Is that how it is supposed to be done? I'm able to run the goal mvn jetty:run without the application.properties present, but if I add settings to the application.properties that differ from the filter-LOCAL.properties they don't seem to override.
Here's an example of what I mean:
pom.xml
<profiles>
<profile>
<id>LOCAL
<activation>
<activeByDefault>true
</activation>
<properties>
<env>LOCAL
</properties>
</profile>
</profiles>
applicationContext.xml
<bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<list>
<value>classpath:application.properties
</list>
</property>
<property name="ignoreResourceNotFound" value="true"/>
</bean>
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
<property name="driverClassName" value="${jdbc.driver}"/>
<property name="url" value="${jdbc.url}"/>
<property name="username" value="${jdbc.username}"/>
<property name="password" value="${jdbc.password}"/>
</bean>
an example of the content of application.properties and filters-LOCAL.properties
jdbc.driver=org.postgresql.Driver
jdbc.url=jdbc:postgresql://localhost/shoutbox_dev
jdbc.username=tester
jdbc.password=tester
Can I remove the propertyConfigurer from the applicationContext, create a PROD filter and disregard the application.properties file, or will that give me issues when deploying to the production server?
You should rather use Maven to select which Spring properties file to use depending on which environment you're building for.
When you're testing in your IDE, you should just start the Spring container from the test without using Maven for anything else than managing your dependencies.
For the record, here is what the author of the blog series the OP is following wrote in this comment:
I used to be a big fan of Spring’s
PropertyPlaceholderConfigurer but ever
since I started using maven I don’t
find it as useful as maven’s filters,
using either a filters file as
explained here, or by having multiple
profiles in the pom for the different
deployment layers with each profile
specifying the properties.
The biggest gripe I have with the
PropertyPlaceholderConfigurer is that
you can only have one
PropertyPlaceholderConfigurer bean.
And it’s not well documented.
With maven’s filter files you can have
as many as you like.
The other reason I prefer maven’s
filters is that with them you can do a
‘mvn package’ and then poke around in
the target directory and eyeball the
filtered config files and see what it
did. With Spring’s
PropertyPlaceholderConfigurer you
don’t find out what’s been substituted
until the app is started.
I second this opinion and prefer the filter approach over using the PropertyPlaceholderConfigurer and the Antrun plugin to copy say test.properties into application.properties when running my tests. And using filtered resources is well supported by all major IDEs (Eclipse, IntelliJ, NetBeans) so I don't see why I should not use it.