Spring property placeholder not working inside an import directive - java

I have a spring configuration with a property placeholder directive attribute that is not being recognised. I have verified that the actual property can be resolved by creating a simple class that outputs the property:
<bean id="propTest" class="com.johnsands.unite.PropertyLogger">
<constructor-arg value="security.configuration"/>
<constructor-arg value="${security.configuration}"/>
</bean>
This simple bean outputs the given parameters, which it does just fine. But when I try to use that same property in an import, as in the following:
<import resource="${security.configuration}"/>
I get the following error.
org.springframework.beans.factory.BeanDefinitionStoreException:
Unexpected exception parsing XML document from ServletContext resource
[/WEB-INF/spring/root-context.xml]; nested exception is
java.lang.IllegalArgumentException: Could not resolve placeholder
'security.configuration' in string value "${security.configuration}"
This does not make sense as I have verified the property can be resolved. It also appears that the directive should be supported as spring is trying to resolve the placeholder but is not able to.

Related

Cannot convert value type 'Configuration' to required type FreeMarkerConfigurationFactoryBean for property freeMarkerConfigurationFactoryBean

Getting below error while using free-marker template in spring:pls suggest.
applicationContext.xml:
Dependent bean:
<bean id="domainManager" class="DomainManager">
<property name="freeMarkerConfigurationFactoryBean" ref="freeMarkerConfigurationFactoryBean"/>
<property name="sampleFile" value="sampleFile.ftl" />
</bean>
Error:
Caused by: org.springframework.beans.ConversionNotSupportedException: Failed to convert property value of type 'freemarker.template.Configuration' to required type 'org.springframework.ui.freemarker.FreeMarkerConfigurationFactoryBean' for property 'freeMarkerConfigurationFactoryBean'; nested exception is java.lang.IllegalStateException: Cannot convert value of type 'freemarker.template.Configuration' to required type 'org.springframework.ui.freemarker.FreeMarkerConfigurationFactoryBean' for property 'freeMarkerConfigurationFactoryBean': no matching editors or conversion strategy found
No need to keep the freeMarkerConfigurationFactoryBean in dependent beans.And use below bean
Where ever we are using Reports keep below template class as autowired with that class.
#Autowired
Configuration freemarkerConfiguration;
freemarkerConfiguration.setClassForTemplateLoading(this.getClass(), CUSTOM_Constant.FREEMARKER_TEMP_LOCATION);
body = FreeMarkerTemplateUtils.processTemplateIntoString( freemarkerConfiguration.getTemplate(fmTemplateName),templateParams);

Spring jms:listener-container concurrency not working with parametrized value

I have a application with spring 2.5.6 that uses spring jms:listener-container
<jms:listener-container concurrency="25" acknowledge="auto"
connection-factory="jmsCachingConnectionFactory" cache="none">
I works perfectly but now I need do externalize the concurrency value in a properties file. I already have others properties being loaded without error.
I tried the below code:
<jms:listener-container concurrency="${spring.jms.concurrency}" acknowledge="auto"
connection-factory="jmsCachingConnectionFactory" cache="none">
But it prints a sort of a parsing error:
[2018-06-25 17:36:05,292] ERROR [MSC service thread 1-1] context.ContextLoader => initWebApplicationContext: (ContextLoader.java:215) - Context initialization failed: org.springframework.beans.factory.parsing.BeanDefinitionParsingException: Configuration problem: Invalid concurrency value [${spring.jms.concurrency}]: only single maximum integer (e.g. "5") and minimum-maximum combo (e.g. "3-5") supported.
Offending resource: class path resource [app-context.xml]; nested exception is java.lang.NumberFormatException: For input string: "${spring.jms.concurrency}"
at org.springframework.beans.factory.parsing.FailFastProblemReporter.error(FailFastProblemReporter.java:68)
at org.springframework.beans.factory.parsing.ReaderContext.error(ReaderContext.java:85)
Also tried to create a String bean and load the parameter then inject into the listener like below, but it print the same error
<bean id="concurrency" class="java.lang.String" >
<constructor-arg value="${spring.jms.concurrency}" />
</bean>
Is there a way to externalize/parametrize the concurrency value in the spring jms listener-container.
PS. Currently using spring 2.5.6 and cannot upgrade it right now.

spring #Value("${property.value}") not returning value

Hi I'm using Spring PropertyPlaceholderConfigurer to fetch values from a properties file.
It's working when i get the value in xml. but in a java class
#Value("${application.imagesPath}")
private String imagesPath;
Returns the literal value "${application.imagesPath}" even though when running the server if the variable application.imagesPath is not in the properties file i get the error
Could not autowire field: private java.lang.String com.ymobility.filltrack.controller.ApiController.imagesPath; nested exception is java.lang.IllegalArgumentException: Could not resolve placeholder 'application.imagesssPath' in string value
(the typo imagesssPath is on purpose to show that if i misspell it, it is knowing that the property doesn't exist which indicates that it is actually reading the properties file correctly, my issue is when there is no error, the value retrieved is the literal string "${application.imagesPath}" instead of it's value in properties file)
So the controller is actually getting the value from the properties file, but the value of the string when used is still the literal value "${application.imagesPath}".
My context.xml file contains the following config
<bean id="appProperties"
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<list>
<value>/WEB-INF/properties/app.properties</value>
</list>
</property>
</bean>
The class I'm using has the following annotations
#RestController
#RequestMapping("/api")
Any idea what might be the problem ?
Thanks

Q - Restlet/Spring - NotWritablePropertyException with tutorial sample?

I'm training about the combo Restlet/Spring but there are some things i still don't understand... I hope you can help me.
In fact, i'm trying to the use Inject dependancies systeme of Spring with Restlet (Like in this tutorial : http://restlet.com/technical-resources/restlet-framework/guide/2.3/introduction/getting-started/maven-spring). So I tried to do it myself but that didn't work. My code returns this exception :
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'basecampComponent' defined in ServletContext resource [/WEB-INF/applicationContext.xml]:
Cannot resolve reference to bean 'basecampAppliction' while setting bean property 'defaultTarget'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'basecampAppliction' defined in ServletContext resource [/WEB-INF/applicationContext.xml]:
Error setting property values; nested exception is org.springframework.beans.NotWritablePropertyException: Invalid property 'root' of bean class[com.mycompany.restlet.basecamp.application.BaseCampApplication]:
Bean property 'root' is not writable or has an invalid setter method. Does the parameter type of the setter match the return type of the getter?
So i was looking for the file "ApplicationContext.xml" and this is his content :
<bean id="basecampComponent" class="org.restlet.ext.spring.SpringComponent">
<property name="defaultTarget" ref="basecampAppliction" />
</bean>
<bean id="basecampAppliction">class="com.mycompany.restlet.basecamp.application.BaseCampApplication">
<property name="root" ref="router" />
</bean>
<!-- Define the router -->
<bean name="router" class="org.restlet.ext.spring.SpringBeanRouter" />
Someone has an idea where could I look for a way to debug this ?
By the way, i'm in Java 1.8.0_60.
Thanks for all your helps.
Benjamin
After looking for some informations over the web, i've a hypothesis how I solved this.
On this link (Spring & Restlet : 100% XML configuration?), he's binding the router with the property "inboundroot" of Application. So I think there's a minimal changed (not noticed in the tutorial). In fact, i tried the project proposed in the archive (doesn't work) and the way you code yourself the tutorial. This is again two solutions.
The final solution consists so by changing the name of the property to "inboundroot" to "root".
"Never trusts the tuto"
Thanks for the time you took to help me.
I think that there is no root attribute in the Application class. You should add one in your BaseCampApplication class and use it to configure your application (see createInboundRoot method), as described below:
public class BaseCampApplication extends Application {
private Restlet root;
public Restlet createInboundRoot() {
return root;
}
public void setRoot(Restlet root) {
this.root = root;
}
}
Hope it helps you,
Thierry

Error when creating bean with type java.io.File [Ambiguous constructor argument types]

I have the following spring bean configuration
<bean id="fileBean" class="java.io.File">
<constructor-arg type="java.lang.String"
value="$prop{file.path.property}" />
</bean>
I'm getting the following error
org.springframework.beans.factory.UnsatisfiedDependencyException:
Error creating bean with name 'fileBean' defined in class path resource [context.xml]:
Unsatisfied dependency expressed through constructor argument with index 0 of type
[java.net.URI]: Ambiguous constructor argument types - did you specify the correct
bean references as constructor arguments?
There is only one constructor for java.io.File with a single String parameter so I'm not sure why this is ambiguous. Any help appreciated.
Found this link that explains what is happening. It turns out that spring will match arguments by type if there is no argument index specified. In this case spring takes my single String argument and passes it to java.io.File constructor that takes TWO strings. This can be fixed by specifying the constructor-arg index.
<bean id="fileBean" class="java.io.File">
<constructor-arg index="0"
type="java.lang.String"
value="$prop{file.path.property}" />
</bean>
Just my two cents here: I had the exact same problem today. I have a unit test to check if Spring can read my XML config and generate all necessary beans. It was failing because I was editing the wrong XML file. I was editing a "dist" version from an Ant build, instead of the correct version from source control.
Lesson learned: Read those Spring exception messages (with XML file paths) very closely!

Categories