Properties how to use in spring xml - spring

<context:property-placeholder location="WEB-INF/classes/config.config"/>
config.config:
path=com
<import resource="../../conf/${path}/test.xml"/>
error:
org.springframework.beans.factory.BeanDefinitionStoreException: Unexpected exception parsing XML document from file [test.service.xml]; nested exception is java.lang.IllegalArgumentException: Could not resolve placeholder 'path'
at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.doLoadBeanDefinitions(XmlBeanDefinitionReader.java:412)Caused by: java.lang.IllegalArgumentException: Could not resolve placeholder 'path'
at org.springframework.util.PropertyPlaceholderHelper.parseStringValue(PropertyPlaceholderHelper.java:173)
${path} can`t use in import,how to do ??

Can you try to do with this way?
<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location" value="classpath:yourpropertiesfile"></property>
</bean>
It is possible to put your properties in other location? Maybe this is the problem.

Related

Integrating Cassandra with Apache Ignite

I have downloaded the 2.7.5 binary of Apache Ignite. I referred to the xml file in the Cassandra-Ignite integration official docs. However, all the beans are failing to load. The configuration file is in my desktop folder. Running Ignite from the folder where I have extracted the binary zip.
The error:
Failed to instantiate Spring XML application context [springUrl=file:/C:/Users/Username/Desktop/ignite-con-new.xml, err=Configuration problem: Failed to import bean definitions from URL location
The config file:
<?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.xsd">
<!-- Cassandra connection settings -->
<import resource="classpath:org/apache/ignite/tests/cassandra/connection-settings.xml" />
<!-- Persistence settings for 'cache1' -->
<bean id="cache1_persistence_settings" class="org.apache.ignite.cache.store.cassandra.persistence.KeyValuePersistenceSettings">
<constructor-arg type="org.springframework.core.io.Resource" value="classpath:org/apache/ignite/tests/persistence/blob/persistence-settings-1.xml" />
</bean>
<!-- Persistence settings for 'cache2' -->
<bean id="cache2_persistence_settings" class="org.apache.ignite.cache.store.cassandra.persistence.KeyValuePersistenceSettings">
<constructor-arg type="org.springframework.core.io.Resource" value="classpath:org/apache/ignite/tests/persistence/blob/persistence-settings-3.xml" />
</bean>
<!-- Ignite configuration -->
<bean id="ignite.cfg" class="org.apache.ignite.configuration.IgniteConfiguration">
<property name="cacheConfiguration">
<list>
<!-- Configuring persistence for "cache1" cache -->
<bean class="org.apache.ignite.configuration.CacheConfiguration">
<property name="name" value="cache1"/>
<property name="readThrough" value="true"/>
<property name="writeThrough" value="true"/>
<property name="cacheStoreFactory">
<bean class="org.apache.ignite.cache.store.cassandra.CassandraCacheStoreFactory">
<property name="dataSourceBean" value="cassandraAdminDataSource"/>
<property name="persistenceSettingsBean" value="cache1_persistence_settings"/>
</bean>
</property>
</bean>
<!-- Configuring persistence for "cache2" cache -->
<bean class="org.apache.ignite.configuration.CacheConfiguration">
<property name="name" value="cache2"/>
<property name="readThrough" value="true"/>
<property name="writeThrough" value="true"/>
<property name="cacheStoreFactory">
<bean class="org.apache.ignite.cache.store.cassandra.CassandraCacheStoreFactory">
<property name="dataSourceBean" value="cassandraAdminDataSource"/>
<property name="persistenceSettingsBean" value="cache2_persistence_settings"/>
</bean>
</property>
</bean>
</list>
</property>
<!-- Explicitly configure TCP discovery SPI to provide list of initial nodes. -->
<property name="discoverySpi">
<bean class="org.apache.ignite.spi.discovery.tcp.TcpDiscoverySpi">
<property name="ipFinder">
<!--
Ignite provides several options for automatic discovery that can be used
instead os static IP based discovery. For information on all options refer
to our documentation: http://apacheignite.readme.io/docs/cluster-config
-->
<!-- Uncomment static IP finder to enable static-based discovery of initial nodes. -->
<!--<bean class="org.apache.ignite.spi.discovery.tcp.ipfinder.vm.TcpDiscoveryVmIpFinder">-->
<bean class="org.apache.ignite.spi.discovery.tcp.ipfinder.multicast.TcpDiscoveryMulticastIpFinder">
<property name="addresses">
<list>
<!-- In distributed environment, replace with actual host IP address. -->
<value>127.0.0.1:47500..47509</value>
</list>
</property>
</bean>
</property>
</bean>
</property>
</bean>
</beans>
I understand it is failing to find the bean locations. What do you think is a possible fix to this?
The full stacktrace:
C:\Users\Username\Downloads\ignitecode\s1>bin\ignite.bat C:\Users\Username\Desktop\ignite-con-new.xml
class org.apache.ignite.IgniteException: Failed to instantiate Spring XML application context [springUrl=file:/C:/Users/Username/Desktop/ignite-con-new.xml, err=Configuration problem: Failed to import bean definitions from URL location [classpath:org/apache/ignite/tests/cassandra/connection-settings.xml]
Offending resource: URL [file:/C:/Users/Username/Desktop/ignite-con-new.xml]; nested exception is org.springframework.beans.factory.BeanDefinitionStoreException: IOException parsing XML document from class path resource [org/apache/ignite/tests/cassandra/connection-settings.xml]; nested exception is java.io.FileNotFoundException: class path resource [org/apache/ignite/tests/cassandra/connection-settings.xml] cannot be opened because it does not exist]
at org.apache.ignite.internal.util.IgniteUtils.convertException(IgniteUtils.java:1026)
at org.apache.ignite.Ignition.start(Ignition.java:351)
at org.apache.ignite.startup.cmdline.CommandLineStartup.main(CommandLineStartup.java:301)
Caused by: class org.apache.ignite.IgniteCheckedException: Failed to instantiate Spring XML application context [springUrl=file:/C:/Users/Username/Desktop/ignite-con-new.xml, err=Configuration problem: Failed to import bean definitions from URL location [classpath:org/apache/ignite/tests/cassandra/connection-settings.xml]
Offending resource: URL [file:/C:/Users/Username/Desktop/ignite-con-new.xml]; nested exception is org.springframework.beans.factory.BeanDefinitionStoreException: IOException parsing XML document from class path resource [org/apache/ignite/tests/cassandra/connection-settings.xml]; nested exception is java.io.FileNotFoundException: class path resource [org/apache/ignite/tests/cassandra/connection-settings.xml] cannot be opened because it does not exist]
at org.apache.ignite.internal.util.spring.IgniteSpringHelperImpl.applicationContext(IgniteSpringHelperImpl.java:392)
at org.apache.ignite.internal.util.spring.IgniteSpringHelperImpl.loadConfigurations(IgniteSpringHelperImpl.java:104)
at org.apache.ignite.internal.util.spring.IgniteSpringHelperImpl.loadConfigurations(IgniteSpringHelperImpl.java:98)
at org.apache.ignite.internal.IgnitionEx.loadConfigurations(IgnitionEx.java:751)
at org.apache.ignite.internal.IgnitionEx.start(IgnitionEx.java:952)
at org.apache.ignite.internal.IgnitionEx.start(IgnitionEx.java:861)
at org.apache.ignite.internal.IgnitionEx.start(IgnitionEx.java:731)
at org.apache.ignite.internal.IgnitionEx.start(IgnitionEx.java:700)
at org.apache.ignite.Ignition.start(Ignition.java:348)
... 1 more
Caused by: org.springframework.beans.factory.parsing.BeanDefinitionParsingException: Configuration problem: Failed to import bean definitions from URL location [classpath:org/apache/ignite/tests/cassandra/connection-settings.xml]
Offending resource: URL [file:/C:/Users/Username/Desktop/ignite-con-new.xml]; nested exception is org.springframework.beans.factory.BeanDefinitionStoreException: IOException parsing XML document from class path resource [org/apache/ignite/tests/cassandra/connection-settings.xml]; nested exception is java.io.FileNotFoundException: class path resource [org/apache/ignite/tests/cassandra/connection-settings.xml] cannot be opened because it does not exist
at org.springframework.beans.factory.parsing.FailFastProblemReporter.error(FailFastProblemReporter.java:70)
at org.springframework.beans.factory.parsing.ReaderContext.error(ReaderContext.java:118)
at org.springframework.beans.factory.parsing.ReaderContext.error(ReaderContext.java:103)
at org.springframework.beans.factory.xml.DefaultBeanDefinitionDocumentReader.importBeanDefinitionResource(DefaultBeanDefinitionDocumentReader.java:233)
at org.springframework.beans.factory.xml.DefaultBeanDefinitionDocumentReader.parseDefaultElement(DefaultBeanDefinitionDocumentReader.java:184)
at org.springframework.beans.factory.xml.DefaultBeanDefinitionDocumentReader.parseBeanDefinitions(DefaultBeanDefinitionDocumentReader.java:169)
at org.springframework.beans.factory.xml.DefaultBeanDefinitionDocumentReader.doRegisterBeanDefinitions(DefaultBeanDefinitionDocumentReader.java:142)
at org.springframework.beans.factory.xml.DefaultBeanDefinitionDocumentReader.registerBeanDefinitions(DefaultBeanDefinitionDocumentReader.java:94)
at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.registerBeanDefinitions(XmlBeanDefinitionReader.java:508)
at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.doLoadBeanDefinitions(XmlBeanDefinitionReader.java:392)
at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:336)
at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:304)
at org.apache.ignite.internal.util.spring.IgniteSpringHelperImpl.applicationContext(IgniteSpringHelperImpl.java:379)
... 9 more
Caused by: org.springframework.beans.factory.BeanDefinitionStoreException: IOException parsing XML document from class path resource [org/apache/ignite/tests/cassandra/connection-settings.xml]; nested exception is java.io.FileNotFoundException: class path resource [org/apache/ignite/tests/cassandra/connection-settings.xml] cannot be opened because it does not exist
at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:344)
at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:304)
at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:181)
at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:217)
at org.springframework.beans.factory.xml.DefaultBeanDefinitionDocumentReader.importBeanDefinitionResource(DefaultBeanDefinitionDocumentReader.java:227)
... 18 more
Caused by: java.io.FileNotFoundException: class path resource [org/apache/ignite/tests/cassandra/connection-settings.xml] cannot be opened because it does not exist
at org.springframework.core.io.ClassPathResource.getInputStream(ClassPathResource.java:172)
at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:330)
... 22 more
Failed to start grid: Failed to instantiate Spring XML application context [springUrl=file:/C:/Users/Username/Desktop/ignite-con-new.xml, err=Configuration problem: Failed to import bean definitions from URL location [classpath:org/apache/ignite/tests/cassandra/connection-settings.xml]
Offending resource: URL [file:/C:/Users/Username/Desktop/ignite-con-new.xml]; nested exception is org.springframework.beans.factory.BeanDefinitionStoreException: IOException parsing XML document from class path resource [org/apache/ignite/tests/cassandra/connection-settings.xml]; nested exception is java.io.FileNotFoundException: class path resource [org/apache/ignite/tests/cassandra/connection-settings.xml] cannot be opened because it does not exist]
Rule of thumb of reading Spring errors is scrolling all the way down.
nested exception is java.io.FileNotFoundException:
class path resource [org/apache/ignite/tests/cassandra/connection-settings.xml]
cannot be opened because it does not exist
Looks like the resource is not where your XML expects it to be. Please note that src/main/resources can contain arbitrary directory structure, such as src/main/resources/org/apache/ignite/tests/cassandra <-- maybe that's where the file should go?

JNDI lookup failure

i would be grateful if someone can help me rectify the issue in my code. Not sure where I'm going wrong.
Currently my persistence.xml contains
<property name="hibernate.transaction.manager_lookup_class" value="#####.hibernate.JbossTSTransactionManagerLookup"/>
<property name="hibernate.current_session_context_class" value="jta"/>
along with
<bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="persistenceUnitName" value="####"/>
<property name="jpaDialect" ref="jpaDialect"/>
</bean>
<bean id="transactionManager" class="org.springframework.transaction.jta.JtaTransactionManager">
<property name="transactionManagerName" value="java:comp/env/TransactionManager"/>
</bean>
<tx:annotation-driven transaction-manager="transactionManager" />
Context initialization failed : org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'transactionManager' defined in class path resource [###XMLConfig.xml]: Invocation of init method failed; nested exception is org.springframework.transaction.TransactionSystemException: JTA TransactionManager is not available at JNDI location [java:comp/env/TransactionManager]; nested exception is org.springframework.jndi.TypeMismatchNamingException: Object of type [class com.arjuna.ats.internal.jta.transaction.arjunacore.TransactionManagerImple] available at JNDI location [java:comp/env/TransactionManager] is not assignable to [javax.transaction.TransactionManager]
Caused by: org.springframework.transaction.TransactionSystemException: JTA TransactionManager is not available at JNDI location [java:comp/env/TransactionManager]; nested exception is org.springframework.jndi.TypeMismatchNamingException: Object of type [class com.arjuna.ats.internal.jta.transaction.arjunacore.TransactionManagerImple] available at JNDI location [java:comp/env/TransactionManager] is not assignable to [javax.transaction.TransactionManager]
at org.springframework.transaction.jta.JtaTransactionManager.lookupTransactionManager(JtaTransactionManager.java:598)
Caused by: org.springframework.jndi.TypeMismatchNamingException: Object of type [class com.arjuna.ats.internal.jta.transaction.arjunacore.TransactionManagerImple] available at JNDI location [java:comp/env/TransactionManager] is not assignable to [javax.transaction.TransactionManager] at org.springframework.jndi.JndiTemplate.lookup()
at org.springframework.transaction.jta.JtaTransactionManager.lookupTransactionManager()
Looks like there is some issue with the classpath of your project. Check if there are two versions of the same dependency in your project. In my case I had multiple versions of the jboss-transaction jar in the classpath, keeping just one version of it fixed the issue for me. Check if there is any transitive dependency which is causing this issue which you can exclude in your maven pom.

Configuration Issue during spring upgrade

Below is the issue i am facing during the spring upgrade.I feel the issue is with the doctype.Please suggest
Bean 'cdqiPerformanceMonitor'; nested exception is
org.springframework.beans.factory.parsing.BeanDefinitionParsingException:
Configuration problem: Old 1.x 'singleton' attribute in use - upgrade to
'scope' declaration
Offending resource: ServletContext resource [/WEB-
INF/applicationContext.xml]
Below is the application context xml
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
<bean id="cdqiPerformanceMonitor"
class="com.tms.cdqi.framework.aop.interceptor.CDQIPerformanceMonitorInterceptor" />
<bean id="cdqiApplicationContextFactory"class="com.tms.cdqi.framework.context.CDQIApplicationContextFactoryImpl" />
<bean id="commandInvoker"class="org.springframework.ejb.access.LocalStatelessSessionProxyFactoryBean"
lazy-init="true">
<property name="jndiName">
<value>ejb/CommandInvokerLocal</value>
</property>
<property name="businessInterface">
<value>
com.tms.cdqi.application.command.invoker.CommandInvoker
</value>
</property>
</bean>
</beans>
Below is the stack trace
listener org.springframework.web.context.ContextLoaderListener
failed:
org.springframework.beans.factory.parsing.BeanDefinitionParsingException:
Configuration problem: Unexpected failure during bean definition parsing
Offending resource: ServletContext resource [/WEB-
INF/applicationContext.xml]
Bean 'cdqiPerformanceMonitor'; nested exception is
org.springframework.beans.factory.parsing.BeanDefinitionParsingException:
Configuration problem: Old 1.x 'singleton' attribute in use - upgrade to
'scope' declaration
Offending resource: ServletContext resource [/WEB-
INF/applicationContext.xml]
See SPR-12167. Spring no longer accepts singleton="true", so you must use scope="singleton" instead.

Resolving system environment variable using Spring Expression Language

I want to resolve system environment variable using `Spring Expression Language' in spring servlet configuration file. My first approach was:
<bean id="propertyConfigurer"
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"
p:location="/WEB-INF/jdbc.#{systemEnvironment.THREAD_ENV}.properties" />
This is throwing below exception:
Caused by: org.springframework.beans.factory.BeanExpressionException: Expression parsing failed; nested exception is org.springframework.expression.spel.SpelEvaluationException: EL1008E:(pos 18): Field or property 'THREAD_ENV' cannot be found on object of type 'java.util.Collections$UnmodifiableMap'
Then I have tried:
<bean id="propertyConfigurer"
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"
p:location="/WEB-INF/jdbc.#{systemEnvironment['THREAD_ENV']}.properties" />
and
<bean id="propertyConfigurer"
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"
p:location="/WEB-INF/jdbc.#{systemEnvironment[THREAD_ENV]}.properties" />
which both fail and resolve to empty string. I am using Tomcat6 and I export this variable just before restarting Tomcat server:
export THREAD_ENV=live;
/etc/init.d/tomcat6 restart;
Would like to mention that all three methods work on some of my Tomcat6 instances but not on all - what could be the reason of such a strange behaviour? Any idea what I am doing wrong?
The SpEL variable is systemProperties, not systemEnvironment.
<bean id="propertyConfigurer"
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"
p:location="/WEB-INF/jdbc.#{systemProperties['THREAD_ENV']}.properties" />

Maven Spring fileUpload using uploadTempDir property

Actually my requirement is to store uploaded files in the classpath directory i.e. src/main/resources/uploads. (I am using maven project)
But the dispatcher is not able to find this path. I am getting the following error.
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'multipartResolver' defined in ServletContext resource [/WEB-INF/dispatcher-servlet.xml]: Error setting property values; nested exception is org.springframework.beans.PropertyBatchUpdateException; nested PropertyAccessExceptions (1) are:
PropertyAccessException 1: org.springframework.beans.MethodInvocationException: Property 'uploadTempDir' threw exception; nested exception is java.io.FileNotFoundException: class path resource [uploads] cannot be resolved to URL because it does not exist
The below configuration is added in the dispatcher file:
<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<property name="maxUploadSize" value="1048576"/>
<property name="uploadTempDir" ref="uploadDirResource"/>
</bean>
<bean id="uploadDirResource" class="org.springframework.core.io.ClassPathResource">
<constructor-arg>
<value>/uploads</value>
</constructor-arg>
</bean>
The path src/main/resources(/uploads) is a path used by Maven BEFORE compiling. Your Server does not know anything about this path!
The artefacts in src/main/resources(/uploads) are copied within the war file into the directory WEB-INF/classes/uploads.
But this directory also contain the java class fiels, and you should never allow a user to modify or add files to that directory!
One point that is may not correct with you configration is the whitespace in
<value>/uploads </value>

Resources