I have a Mule flow that I wish to configure via a Spring .properties file. I have read the Mule documentation on how to do this and I'm pretty sure I have it correct, but I get:
Could not load properties; nested exception is java.io.FileNotFoundException:
class path resource [agt-commission.properties] cannot be opened because
it does not exist
My Mule flow XML file begins with:
<mule xmlns:tracking="http://www.mulesoft.org/schema/mule/ee/tracking"
xmlns="http://www.mulesoft.org/schema/mule/core"
xmlns:mulexml="http://www.mulesoft.org/schema/mule/xml"
xmlns:http="http://www.mulesoft.org/schema/mule/http"
xmlns:file="http://www.mulesoft.org/schema/mule/file"
xmlns:jdbc-ee="http://www.mulesoft.org/schema/mule/ee/jdbc"
xmlns:data-mapper="http://www.mulesoft.org/schema/mule/ee/data-mapper"
xmlns:doc="http://www.mulesoft.org/schema/mule/documentation"
xmlns:spring="http://www.springframework.org/schema/beans"
xmlns:core="http://www.mulesoft.org/schema/mule/core"
xmlns:context="http://www.springframework.org/schema/context"
version="EE-3.4.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.mulesoft.org/schema/mule/xml
http://www.mulesoft.org/schema/mule/xml/current/mule-xml.xsd
http://www.mulesoft.org/schema/mule/http
http://www.mulesoft.org/schema/mule/http/current/mule-http.xsd
http://www.mulesoft.org/schema/mule/file
http://www.mulesoft.org/schema/mule/file/current/mule-file.xsd
http://www.mulesoft.org/schema/mule/ee/jdbc
http://www.mulesoft.org/schema/mule/ee/jdbc/current/mule-jdbc-ee.xsd
http://www.mulesoft.org/schema/mule/ee/data-mapper
http://www.mulesoft.org/schema/mule/ee/data-mapper/current/mule-data-mapper.xsd
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-current.xsd
http://www.mulesoft.org/schema/mule/core
http://www.mulesoft.org/schema/mule/core/current/mule.xsd
http://www.mulesoft.org/schema/mule/ee/tracking
http://www.mulesoft.org/schema/mule/ee/tracking/current/mule-tracking-ee.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<spring:beans>
<context:property-placeholder location="classpath:agt-commission.properties"/>
</spring:beans>
My properties file is located in the src/main/app folder as specified by the documentation and I have copied and pasted the file name, so I know that is the same.
I am running the project from Mule Studio (3.5.0) as a Mule application.
Why can't Spring find the properties file?
Store your agt-commission.properties file in src/main/resources instead of src/main/app.
By default files under app doesn't get copied to classes which is causing FileNotFoundException
For Property placeholder files the better location is src/main/resources.
As properties file to be provided in the app folder, is the mule-deploy properties which has the details of the application deployment.
Move your resource file to src/main/resources and it should work with your config file.
Note: This will work when the build in the POM has got the src/main/resources declared as resource
<resources>
<resource>
<filtering>true</filtering>
<directory>src/main/resources</directory>
</resource>
</resources>
Hope this helps.
Related
I have two modules in my project which are children of the main app.pom
<modules>
<module>commons</module>
<module>webapp</module>
</modules>
The webapp module refers to commons this way:
<dependency>
<groupId>com.app.whatever</groupId>
<artifactId>commons</artifactId>
<version>${project.version}</version>
</dependency>
The commons module is just packaged in .jar file and that's it. All their dependencies webapp and commons inherit from app.pom.
Both commons and webapp use Spring 3 with .xml files that describe application contexts.
In webapp module I want to use a bean from commons module. Here's applicationContext for commons:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
<context:component-scan base-package="com.app.whatever.anything"/>
</beans>
For webapp applicationContext.xml I have:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd">
<import resource="classpath*:/applicationContext-commons.xml"/>
<context:component-scan base-package="com.app.whatever.something.controller.spring"/>
But unfortunately this doesn't work. Constantly get an exception about no candidates for autowiring, though I can use any classes from the second module in the first one after setting the above maven dependency.
When I write this import without asterisk, it says that the file doesn't exist.
I tried without leading slash - the same exception.
This import works perfectly for any file from the same module and even for external libraries I have. But it can't resolve the file from this new commons module I've just created.
If you have any idea, that'd be really helpful, thanks
If it's not under a subdirectory, e.g. spring/applicationContext-commons.xml then the following (which you've already tried) should work:
<import resource="classpath*:applicationContext-commons.xml"/>
With an asterisk, Spring will use all matching files on the classpath, without the asterisk it will use the first one it finds.
Also, with the asterisk Spring will ignore the file if it wasn't found. Without the asterisk Spring will give an error if it can't find the file. Therefore your file is not on the classpath.
I'd check the location of applicationContext-commons.xml under the target folder to make sure <import resource= has the correct path and re-run mvn install from the root pom.
I am using my liquibase xml config file (spring integration) with follow notation
<databaseChangeLog
xmlns="http://www.liquibase.org/xml/ns/dbchangelog"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.liquibase.org/xml/ns/dbchangelog
http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-3.1.xsd">
So.. I would use my app offline mode and i put "classpath" notation into my changelog:
<databaseChangeLog
xmlns="http://www.liquibase.org/xml/ns/dbchangelog"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.liquibase.org/xml/ns/dbchangelog
classpath:xsd/dbchangelog-3.1.xsd">
But i have follow error. Can you help me?
Caused by: java.net.MalformedURLException: unknown protocol: classpath
at java.net.URL.<init>(URL.java:592)
at java.net.URL.<init>(URL.java:482)
at java.net.URL.<init>(URL.java:431)
at com.sun.org.apache.xerces.internal.impl.XMLEntityManager.setupCurrentEntity(XMLEntityManager.java:610)
at com.sun.org.apache.xerces.internal.impl.XMLVersionDetector.determineDocVersion(XMLVersionDetector.java:189)
at com.sun.org.apache.xerces.internal.impl.xs.opti.SchemaParsingConfig.parse(SchemaParsingConfig.java:582)
at com.sun.org.apache.xerces.internal.impl.xs.opti.SchemaParsingConfig.parse(SchemaParsingConfig.java:685)
at com.sun.org.apache.xerces.internal.impl.xs.opti.SchemaDOMParser.parse(SchemaDOMParser.java:530)
at com.sun.org.apache.xerces.internal.impl.xs.traversers.XSDHandler.getSchemaDocument(XSDHandler.java:2175)
I fixed this problem by adding the related XSD file dbchangelog-3.5.xsd locally inside the project and updating the Liquibase configuration file (the xsi:schemaLocation part) like this:
<databaseChangeLog
xmlns="http://www.liquibase.org/xml/ns/dbchangelog"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.liquibase.org/xml/ns/dbchangelog dbchangelog-3.5.xsd">
So now the XSD file location is configured like dbchangelog-3.5.xsd. Just make sure the XSD file is located in the same directory as the Liquibase configuration file.
Spring cant find my property file (MyPropFile.properties) inside src/main/resources and throws an exception like below
java.io.FileNotFoundException: class path resource [file*:/src/main/resources/MyPropFile.properties] cannot be opened because it does not exist
But if I place MyPropFile.properties at the root of my project (MyProject/MyPropFile.properties) spring can find it and the programs executes properly.
How do I configure this so that I can place my .properties file inside src/main/resources
this is my namespace
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
">
this is my bean
<context:property-placeholder location="classpath:MyPropFile.properties" />
Java:
#Value("${message.fromfile}")
private String message;
Thanks in advance guys.
Try this. Make this entry in your application config file:
<beans xmlns:context="http://www.springframework.org/schema/context"
.....
xsi:schemaLocation="...
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
....>
<context:property-placeholder location="classpath:MyPropFile.properties" />
....
</beans>
and access the message property:
#Value("${messageFromfile}")
private String message;
I'd expect Maven to copy this to the target directory and for the classpath to be set appropriately. I wouldn't expect Maven to search the source directory other than at compile time.
You should use
<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location">
<value>classpath:/MyPropFile.properties</value>
</property>
</bean>
Without the classpath: prefix, the PropertyPlaceholderConfigurer is attempting to resolve the resource as a file resource, so is looking for it in your current working directory.
As an addendum to #ryanp solution which is the correct and sanitized way to deal with files resources, which should be under the classpath location.
Maven will automagically collect the configured file resources and append them to your runtime classpath holder so that resources prefixed with classpath: can be resolved.
Otherwise, if you find your self stack with Maven not being able to stream file resources, you may hook into Maven's resources configuration and map your files location as follows:
<build>
<resources>
<resource>
<directory>src/main/resources</directory>
<includes>
<include>*.properties</include>
</includes>
</resource>
</resources>
</build>
I tried everything I can think of, but nothing seems to work.
I am using the current version of Spring Framework (3.2) in my java web application.
Every time I start my project I get the following error:
cvc-elt.1: Cannot find the declaration of element 'beans'
this is my applicationContext.xml:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd"
</beans>
I tried to :
change the version of the schema (3.2, 2.0...)
copy the schema from the jar into WEB-INF
change unix EOL into windows EOL
and nothing seems to work, except using the DTD declaretion instead of the XSD.
What should I do?
Spring is having trouble finding the xsds. Take a look at this SO post. If you are sure you have the required spring-beans jar in your classpath, you may have a corrupted META-INF/spring.schemas file. The spring.schemas file tells spring which classpath to use to find the corresponding xsd file in the spring jars. I have had this occur when using the maven-shade plugin improperly configured.
The xsd came down fine for me when I pasted it in the browser.
This one works fine for me:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<context:annotation-config/>
<context:component-scan base-package="example"/>
</beans>
for my spring-mvc application I created several types of configuration (unittest, integration, qa, production). All the configs are in one war-file, so there is only one type of application I create. Which configuration to take should be decided by the server, where the application is running.
To decide what kind of configuration should be used, I have to look into a file. After that I can decide which configuration should be used by spring mvc.
For now by convention there is always the -servlet.xml used. Is there a way how to decide dynamically which config to take?
Regards,
Michael
Here is a solution that I use. It works very well:
Put the configuration differences in property files.
Keep a single Spring xml with placeholders.
Use PropertyPlaceholderConfigurer to set the properties.
PropertyPlaceholderConfigurer can use system properties to resolve the name of the property file to load.
Set a system property with the name of your environment before initilizing the PropertyPlaceholderConfigurer (you can do this in a bean that reads the value out of your file).
And there you go! The environment will be cleanly detected, and the relevant properties will be loaded!
No need to wait for Spring 3.1, you can use this solution today with 3.0.
I have the same setup, but I use maven to build the WARs differently. I use a PropertyPlaceholderConfigurer in the context:
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<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"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
<context:property-placeholder location="classpath:datasource.properties" ignore-unresolvable="true" />
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"
p:driverClassName="${jdbc.driver}"
p:url="${jdbc.url}"
p:username="${jdbc.username}"
p:password="${jdbc.password}" />
<!--other beans-->
</beans>
then I setup an environments folder:
src
--main
----environments
------dev
--------datasource.properties
------cert
--------datasource.properties
------prod
--------datasource.properties
Then in my Maven pom, I use a build profile to copy anything in the environment folder based on a parameter flag in the maven command:
<profiles>
<profile>
<id>environment</id>
<activation>
<property>
<name>environment</name>
</property>
</activation>
<build>
<resources>
<resource>
<directory>
src/main/environments/${environment}
</directory>
</resource>
</resources>
<!-- other build config and plugins -->
so the following command:
mvn clean -Denvironment=dev install
would copy the dev datasource.properties to the war
After all I'm using PropertyPlaceholderConfigurer but slightly differnt than Axel mentioned: I load just one property from my configuration and use it to determine which import to use. Because of https://jira.springframework.org/browse/SPR-1332 I cant use a file to store the instance-type, but have to use environment-variables.
<bean id="propertyConfigurerOne" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"/>
<import resource="classpath:/web${vabse.Environment}.xml"/>