I have the following configuration in my spring bean.xml, when I run i get the ERROR: as mentioned below. I could not understand what the below configuration tag and use of it. where to specify the config.file
<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location">
<value>file:#{ systemProperties['config.file']}</value>
</property>
Error:
Exception in thread "main" org.springframework.beans.factory.BeanInitializationException: Could not load properties; nested exception is java.io.FileNotFoundException:
Are you remembering to pass the value -Dconfig.file=c:/blah/blah.config.props
Related
I'm using springframework first time. So wrote a small program and to test value of Instance variable generated in IoC. But I am getting below error:
Feb 24, 2019 10:40:13 PM org.springframework.context.support.AbstractApplicationContext refresh
WARNING: Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.BeanDefinitionStoreException: Invalid bean definition with name 'loadingObject' defined in class path resource [Spring-Config.xml]: Could not resolve placeholder 'log4j.configuration' in value "${log4j.configuration}"; nested exception is java.lang.IllegalArgumentException: Could not resolve placeholder 'log4j.configuration' in value "${log4j.configuration}"
Exception in thread "main" org.springframework.beans.factory.BeanDefinitionStoreException: Invalid bean definition with name 'loadingObject' defined in class path resource [Spring-Config.xml]: Could not resolve placeholder 'log4j.configuration' in value "${log4j.configuration}"; nested exception is java.lang.IllegalArgumentException: Could not resolve placeholder 'log4j.configuration' in value "${log4j.configuration}"
at org.springframework.beans.factory.config.PlaceholderConfigurerSupport.doProcessProperties(PlaceholderConfigurerSupport.java:228)
at org.springframework.beans.factory.config.PropertyPlaceholderConfigurer.processProperties(PropertyPlaceholderConfigurer.java:213)
at org.springframework.beans.factory.config.PropertyResourceConfigurer.postProcessBeanFactory(PropertyResourceConfigurer.java:86)
at org.springframework.context.support.PostProcessorRegistrationDelegate.invokeBeanFactoryPostProcessors(PostProcessorRegistrationDelegate.java:286)
at org.springframework.context.support.PostProcessorRegistrationDelegate.invokeBeanFactoryPostProcessors(PostProcessorRegistrationDelegate.java:166)
at org.springframework.context.support.AbstractApplicationContext.invokeBeanFactoryPostProcessors(AbstractApplicationContext.java:691)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:528)
at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:144)
at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:85)
at com.jcg.spring.log4j.Mainclass.main(Mainclass.java:12)
Caused by: java.lang.IllegalArgumentException: Could not resolve placeholder 'log4j.configuration' in value "${log4j.configuration}"
at org.springframework.util.PropertyPlaceholderHelper.parseStringValue(PropertyPlaceholderHelper.java:172)
at org.springframework.util.PropertyPlaceholderHelper.replacePlaceholders(PropertyPlaceholderHelper.java:124)
at org.springframework.beans.factory.config.PropertyPlaceholderConfigurer$PlaceholderResolvingStringValueResolver.resolveStringValue(PropertyPlaceholderConfigurer.java:232)
at org.springframework.beans.factory.config.BeanDefinitionVisitor.resolveStringValue(BeanDefinitionVisitor.java:296)
at org.springframework.beans.factory.config.BeanDefinitionVisitor.resolveValue(BeanDefinitionVisitor.java:217)
at org.springframework.beans.factory.config.BeanDefinitionVisitor.visitPropertyValues(BeanDefinitionVisitor.java:147)
at org.springframework.beans.factory.config.BeanDefinitionVisitor.visitBeanDefinition(BeanDefinitionVisitor.java:85)
at org.springframework.beans.factory.config.PlaceholderConfigurerSupport.doProcessProperties(PlaceholderConfigurerSupport.java:225)
... 9 more
I've placed my application.properties file and Spring-Config.xml metadata file to this location ...\src\main\resources
application.properties
log4j.configuration=C:\Softwares\ConfigFiles\log4j.properties
Spring-Config.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"
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">
<bean id="propertiesToBeTaken" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<list>
<value>classpath*:application.properties</value>
<value>classpath*:*keys.properties</value>
</list>
</property>
</bean>
<bean id = "loadingObject" class = "com.jcg.spring.log4j.TestController">
<property name="log4jConfig" value="${log4j.configuration}" />
</bean>
</beans>
Code Snippet
public class TestController {
public String log4jConfig;
public void setlog4j(String log4jConfig){
this.log4jConfig = log4jConfig;
}
public String getlog4j(){
return this.log4jConfig;
}
}
public class Mainclass {
public static void main(String[] args){
ApplicationContext context = new ClassPathXmlApplicationContext("Spring-Config.xml");
TestController obj = (TestController) context.getBean("TestController");
System.out.println(obj.log4jCongif);
}
}
Everything seems to be okay, But not sure why this error is coming.
Stuck in this for a while. Can anyone please have a look? What I am missing?
Thanks
It seems Spring container is trying to instantiate TestController bean before the PropertyPlaceholderConfigurer so property is not getting resolved, hence the error.
You can try putting <property name="ignoreUnresolvablePlaceholders" value="true"/> into Spring-Config.xml to tell spring to ignore unresolved properties. Once PropertyPlaceholderConfigurer will be instantiated possibly property will get resolved.
Try this
<bean id="propertiesToBeTaken" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<list>
<value>classpath:application.properties</value>
<value>classpath:keys.properties</value>
</list>
</property>
<property name="ignoreUnresolvablePlaceholders" value="true"/>
</bean>
Plus few more changes:
TestController obj = (TestController) context.getBean("loadingObject");
setter method name: setLog4jConfig
getter method name: getLog4jConfig
Try replacing PropertyPlaceHolder Bean with:
<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location">
<value>application.properties</value>
</property>
</bean>
Have the context spring conf like this, I have 2 property-placeholder in my context.
<?xml version="1.0" encoding="UTF-8"?>
<beans>
<context:annotation-config/>
<context:component-scan base-package="com.xxx.app.xxx"/>
<context:property-placeholder location="classpath:em-management.properties"/>
<context:property-placeholder location="file:///opt/ass/swp/conf/platform.properties"/>
</beans>
When I run the code, met this error:
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'CMClient': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private java.lang.String com.xxxx.app.xxxx.xx.client.CMClient.washost; nested exception is java.lang.IllegalArgumentException: Could not resolve placeholder 'com.ibm.CORBA.securityServerHost' in string value "${com.ibm.CORBA.securityServerHost}"
Caused by: org.springframework.beans.factory.BeanCreationException: Could not autowire field: private java.lang.String com.xxxx.app.xxx.xxx.client.CMClient.washost; nested exception is java.lang.IllegalArgumentException: Could not resolve placeholder 'com.ibm.CORBA.securityServerHost' in string value "${com.ibm.CORBA.securityServerHost}"
Caused by: java.lang.IllegalArgumentException: Could not resolve placeholder 'com.ibm.CORBA.securityServerHost' in string value "${com.ibm.CORBA.securityServerHost}"
how to solve this issue?
I found the solution for this particular problem,just append ignore-unresolvable="true" to each line.
<context:property-placeholder location="classpath:em-management.properties" ignore-unresolvable="true"/>
<context:property-placeholder location="file:///opt/ass/swp/conf/platform.properties" ignore-unresolvable="true"/>
Don't use multiple <context:property-placeholder/> tags, refer below code for the same and also make sure you have a key "com.ibm.CORBA.securityServerHost" with a value in either of your property file.
<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location">
<list>
<value>classpath:em-management.properties</value>
<value>file:///opt/ass/swp/conf/platform.properties</value>
</list>
</property>
<property name="ignoreUnresolvablePlaceholders" value="true"/>
</bean>
Trying to connect to Postgres DB, context params are as below:
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource" >
<property name="driverClassName" value="org.postgresql.Driver"/>
<property name="url" value="jdbc:postgresql://localhost:5432/****" />
<property name="username" value="****" />
<property name="password" value="****" />
</bean>
Getting exception on context loading:
SEVERE: Context initialization failed
org.springframework.beans.factory.BeanCreationException: Error
creating bean with name 'dataSource' defined in ServletContext
resource [/WEB-INF/ApplicationContext.xml]: Error setting property
values; nested exception is
org.springframework.beans.PropertyBatchUpdateException; nested
PropertyAccessExceptions (1) are: PropertyAccessException 1:
org.springframework.beans.MethodInvocationException: Property
'driverClassName' threw exception; nested exception is
java.lang.IllegalStateException: Could not load JDBC driver class
[org.postgresql.Driver]
I have the postgres driver in the LIB of the project, as I can check the class file org.postgresql.Driver. The Class.forName also gives a positive result for the driver class.
I have tried all versions of postgres drivers, but still this isn't going through.
Make sure the lib gets copied to the packaging correctly and that that directory is in the classpath.
I was able to resolve the issue , When I added the Postgres DB Jar to the WEBINF/Lib the bean was getting created , Some how it was not picking the jar from the JavaResource/Lib as i was doing this before .
The problem is a particular case which is not answered in most of the places well and just checking the classpath wont be a correct answer to this query.
I have configured the following beans in my application context:
<bean id="bindConnectionPool" parent="abstractConnectionPool"
p:connectionFactory-ref="bindConnectionFactory" />
<bean id="bindConnectionFactory"
class="org.ldaptive.DefaultConnectionFactory"
p:connectionConfig-ref="bindConnectionConfig" />
<bean id="bindConnectionConfig" parent="abstractConnectionConfig" />
<bean id="abstractConnectionConfig" abstract="true"
class="org.ldaptive.ConnectionConfig"
p:ldapUrl="ldaps://myldap.example.com:636"
p:connectTimeout="3000"
p:useStartTLS="true"
p:sslConfig-ref="sslConfig" />
<!-- EDIT/UPDATE: -->
<bean id="sslConfig" class="org.ldaptive.ssl.SslConfig">
<property name="credentialConfig">
<bean class="org.ldaptive.ssl.X509CredentialConfig"
p:trustCertificates="mycert" />
</property>
</bean>
When I deploy the WAR to Tomcat I get the following exception:
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean
with name ‘bindConnectionPool’ defined in ServletContext resource
[/WEB-INF/deployerConfigContext.xml]: Invocation of init method failed; nested exception
is java.lang.IllegalArgumentException: path must start with either classpath: or file:
What do I need to change the LDAPS-based URL to? Neither file: nor classpath: make sense here...thoughts?
Or, is ldapUrl a red herring, and is there some other path defined on this bindConnectionPool bean that isn't configured right? Oh, and obviously, can't post the LDAP server's actual name for security reasons (myldap.example.com is a different value).
Using this configuration (which works fine in Tomcat) for initializing my webserviceTemplate:
<bean id="messageFactory" class="org.springframework.ws.soap.saaj.SaajSoapMessageFactory">
<property name="messageFactory">
<bean class="com.sun.xml.internal.messaging.saaj.soap.ver1_2.SOAPMessageFactory1_2Impl"/>
</property>
</bean>
<bean id="webServiceTemplate" class="org.springframework.ws.client.core.WebServiceTemplate">
<constructor-arg ref="messageFactory"/>
</bean>
I get this stack trace:
Caused by: org.springframework.beans.ConversionNotSupportedException: Failed to convert property value of type 'com.sun.xml.internal.messaging.saaj.soap.ver1_2.SOAPMessageFactory1_2Impl' to required type 'javax.xml.soap.MessageFactory' for property 'messageFactory'; nested exception is java.lang.IllegalStateException: Cannot convert value of type [com.sun.xml.internal.messaging.saaj.soap.ver1_2.SOAPMessageFactory1_2Impl] to required type [javax.xml.soap.MessageFactory] for property 'messageFactory': no matching editors or conversion strategy found
I am unable to figure out the resolution, any ideas?
I encountered the same issue and finally noticed that the exception message suggested that a property "messageFactory" could not be set on the SaajSoapMessageFactory. This is confusing, because "messageFactory" is also the name of the bean being created as SaajSoapMessageFactory. Spring was failing to set the messageFactory property of SaajSoapMessageFactory to the same SaajSoapMessageFactory bean.
I found two solutions:
My element included default-autowire="byName". By removing
this, the issue disappeared.
Without removing the default-autowire attribute, I explicitly set the messageFactory property to null. This is a bit awkward but also
appears to work:
<bean id="messageFactory" class="org.springframework.ws.soap.saaj.SaajSoapMessageFactory">
<property name="soapVersion">
<util:constant static-field="org.springframework.ws.soap.SoapVersion.SOAP_12"/>
</property>
<property name="messageFactory">
<null />
</property>
</bean>