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>
Related
I am trying to set the proxy to spring rest template bean based on the property, I tried below configuration,
<bean id="forwardProxy" class="java.net.Proxy">
<constructor-arg>
<util:constant static-field="java.net.Proxy.Type.HTTP"/>
</constructor-arg>
<constructor-arg>
<bean class="java.net.InetSocketAddress">
<constructor-arg value="${forward.proxy.host}"/>
<constructor-arg value="${forward.proxy.port}"/>
</bean>
</constructor-arg>
</bean>
<util:constant id="noProxy" static-field="java.net.Proxy.NO_PROXY" />
<bean id="myRestTemplate" class="org.springframework.web.client.RestTemplate">
<constructor-arg>
<bean class="org.springframework.http.client.SimpleClientHttpRequestFactory">
<property name="connectTimeout" value="5000"/>
<property name="readTimeout" value="5000"/>
<property name="proxy" ref="#{ ${aem.enableProxy}==true ? forwardProxy : noProxy }"/>
</bean>
</constructor-arg>
</bean>
but I am getting below error saying No bean named 'DIRECT' available
Caused by (repeated) ... : org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'myRestTemplate' defined in class path resource [environment/application.xml]:
Cannot create inner bean 'org.springframework.http.client.SimpleClientHttpRequestFactory#1177bca' of type [org.springframework.http.client.SimpleClientHttpRequestFactory] while setting constructor argument; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.springframework.http.client.SimpleClientHttpRequestFactory#1177bca' defined in class path resource [environment/application.xml]:
Cannot resolve reference to bean '#{ false==true ? forwardProxy : noProxy }' while setting bean property 'proxy'; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'DIRECT' available
Proxy.Type.DIRECT is a enum, not sure how to create a bean for that. Is it possible to achieve what I am doing without creating a factory beans option.
Using spring version: 4.3.8
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>
In rest-servlet.xml the below configuration is made ..
<property name="defaultViews">
<list>
<!-- JSON View -->
<bean class="org.springframework.web.servlet.view.json.MappingJacksonJsonView">
</bean>
<!-- XML view -->
<bean class="org.springframework.web.servlet.view.xml.MarshallingView">
<constructor-arg>
<bean class="org.springframework.oxm.castor.CastorMarshaller">
</bean>
</constructor-arg>
</bean>
<!-- PDF view -->
<bean class="com.hmkcode.view.PDFView">
</bean>
<!-- XLSX "Excel" view -->
<bean class="com.hmkcode.view.ExcelView">
</bean>
</list>
</property>
The below error occurs.. can any one provide solution for this.
Thanks in advance
SEVERE: StandardWrapper.Throwable
org.springframework.beans.factory.BeanCreationException: Error
creating bean with name
'org.springframework.web.servlet.view.ContentNegotiatingViewResolver#0'
defined in ServletContext resource [/WEB-INF/rest-servlet.xml]: Cannot
create inner bean
'org.springframework.web.servlet.view.xml.MarshallingView#1e9ad27' of
type [org.springframework.web.servlet.view.xml.MarshallingView] while
setting bean property 'defaultViews' with key [1]; nested exception is
org.springframework.beans.factory.BeanCreationException: Error
creating bean with name
'org.springframework.web.servlet.view.xml.MarshallingView#1e9ad27'
defined in ServletContext resource [/WEB-INF/rest-servlet.xml]: Cannot
create inner bean
'org.springframework.oxm.castor.CastorMarshaller#10651e1' of type
[org.springframework.oxm.castor.CastorMarshaller] while setting
constructor argument; nested exception is
org.springframework.beans.factory.BeanCreationException: Error
creating bean with name
'org.springframework.oxm.castor.CastorMarshaller#10651e1' defined in
ServletContext resource [/WEB-INF/rest-servlet.xml]: Invocation of
init method failed; nested exception is
java.lang.NoClassDefFoundError:
org/castor/core/util/AbstractProperties at
org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveInnerBean(BeanDefinitionValueResolver.java:290)
Looks like you miss the castor-core.jar in your classpath. Please add it.
if you use maven you can add
<dependency>
<groupId>org.codehaus.castor</groupId>
<artifactId>castor-core</artifactId>
<version>1.3.3</version>
</dependency>
to your pom.xml rebuild your project and deploy it again.
I'm trying to migrate a Spring webapp from Websphere Application Server to JBoss AS, but am running into this problem when deploying:
org.springframework.beans.factory.BeanCreationException: Error creating bean with name
'org.springframework.security.filterChains':
Cannot resolve reference to bean 'org.springframework.security.web.DefaultSecurityFilterChain#2'
while setting bean property 'sourceList' with key [2]; nested exception is org.springframework.beans.factory.BeanCreationException:
Error creating bean with name 'org.springframework.security.web.DefaultSecurityFilterChain#2':
Cannot resolve reference to bean 'preAuthenticationFilter' while setting constructor argument with key [3];
nested exception is org.springframework.beans.factory.BeanCreationException:
Error creating bean with name 'preAuthenticationFilter': Injection of autowired dependencies failed;
nested exception is org.springframework.beans.factory.BeanCreationException:
Could not autowire field: private com.m.g.utils.WebUtils com.m.g.auth.PreAuthenticationFilter.webUtils;
nested exception is org.springframework.beans.factory.BeanCreationException:
Error creating bean with name 'webUtils': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field:
private com.m.g.helper.Delegate com.m.g.utils.WebUtils.Delegate;
nested exception is org.springframework.beans.factory.BeanCreationException:
Error creating bean with name 'delegate' defined in URL [file:/workspace/Web/WebContent/WEB-INF/core-context.xml]:
Cannot resolve reference to bean 'authenticationService' while setting bean property 'authenticationService';
nested exception is org.springframework.beans.factory.BeanCreationException:
Error creating bean with name 'authenticationService' defined in URL [file:/workspace/M-G-Web/WebContent/WEB-INF/core-context.xml]: Initialization of bean failed;
nested exception is org.springframework.beans.ConversionNotSupportedException:
Failed to convert property value of type 'org.springframework.ldap.core.LdapTemplate' to required type 'org.springframework.ldap.core.LdapTemplate' for property 'ldapTemplate'; nested exception is java.lang.IllegalStateException:
Cannot convert value of type [org.springframework.ldap.core.LdapTemplate] to required type [org.springframework.ldap.core.LdapTemplate] for property 'ldapTemplate': no matching editors or conversion strategy found
The relevant part of Core-context.xml looks like this:
<bean id="authenticationService" class="com.m.g.serviceimpl.AuthenticationServiceImpl">
<property name="wmbService" ref="wmbService" />
<property name="authenticationDAO" ref="authenticationDAO" />
<property name="customerDAO" ref="customerDAO" />
<property name="ldapTemplate" ref="ldapTemplate" />
</bean>
...
<bean id="contextSource" class="org.springframework.ldap.core.support.LdapContextSource">
<property name="url" value="#{systemProperties['ad.write.url']}" />
<property name="base" value="" />
<property name="userDn" value="#{systemProperties['ad.admin.userDn']}" />
<property name="password" value="#{systemProperties['ad.admin.password']}" />
</bean>
<bean id="ldapTemplate" class="org.springframework.ldap.core.LdapTemplate">
<constructor-arg ref="contextSource" />
</bean>
And the relevant part of AuthenticationServiceImpl.java:
#Component("authenticationService")
public class AuthenticationServiceImpl implements IAuthenticationService {
private IWMBService wmbService;
#Autowired
private IAuthenticationDAO authenticationDAO;
#Autowired
private IServiceFactory serviceFactory;
#Autowired
private CustomerManagementDAO customerDAO;
private LdapTemplate ldapTemplate;
#Autowired
private IProperties properties;
I've searched to try to figure out why this error is occurring, but I've haven't found anything that worked. This exact same code worked under Websphere, so I suspect it's something like a build path issue. Any help is much appreciated!
I ended up figuring it out in the end. The problem was I had two different versions of spring-ldap-core in my project, which resulted in two different implementations of LdapTemplate. Removing one of the jars resolved the issue.
When I run my app from command line (java -jar forward.jar) everythings works ok, no exceptions thrown. However, when running the same app as a windows service I get NullPointerException.
19.06.2014 14:44:46.442 [DefaultListableBeanFactory] [AbstractBeanFactory.java] [DEBUG] [Thread-0 ] Ignoring bean creation
exception on FactoryBean type check:
org.springframework.beans.factory.UnsatisfiedDependencyException:
Error creating bean with name 'phoneNumberRepository' defined in URL
[jar:file:/C:/forward.jar!/com/forward/persistence/db/PhoneNumberRepository.class]:
Unsatisfied dependency expressed through bean property
'sqlSessionFactory': : Error creating bean with name
'sqlSessionFactory' defined in URL
[jar:file:/C:/forward.jar!/mybatis-context.xml]: Invocation of init
method failed; nested exception is java.lang.NullPointerException;
nested exception is
org.springframework.beans.factory.BeanCreationException: Error
creating bean with name 'sqlSessionFactory' defined in URL
[jar:file:/C:/prem-forward.jar!/mybatis-context.xml]: Invocation of
init method failed; nested exception is java.lang.NullPointerException
and then
Caused by:
org.springframework.beans.factory.NoSuchBeanDefinitionException: No
qualifying bean of type
[com.forward.persistence.db.PhoneNumberRepository] found for
dependency: expected at least 1 bean which qualifies as autowire
candidate for this dependency. Dependency annotations:
{#org.springframework.beans.factory.annotation.Autowired(required=true)}
at
org.springframework.beans.factory.support.DefaultListableBeanFactory.raiseNoSuchBeanDefinitionException(DefaultListableBeanFactory.java:988)
~[spring-beans-3.2.4.RELEASE.jar:3.2.4.RELEASE] at
org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:858)
~[spring-beans-3.2.4.RELEASE.jar:3.2.4.RELEASE] at
org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:770)
~[spring-beans-3.2.4.RELEASE.jar:3.2.4.RELEASE] at
org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:486)
~[spring-beans-3.2.4.RELEASE.jar:3.2.4.RELEASE] ... 28 common frames
omitted
Hier ist my myBatis-context.xml definition:
<import resource="classpath*:datasource-context.xml"/>
<mybatis:scan base-package="com.forward.persistence.db" />
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<property name="typeAliasesPackage" value="com.forward.model"/> <!-- Register the short names of beans as aliases -->
<property name="configLocation" value="classpath:mybatis-config.xml"/>
</bean>
<bean id="sqlSession" class="org.mybatis.spring.SqlSessionTemplate">
<constructor-arg index="0" ref="sqlSessionFactory" />
</bean>
<!-- scan for mappers and let them be autowired -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.forward.persistence.db" />
</bean>
UPDATE: The definition of PhoneNumberRepository.xml is as follows
<?xml version="1.0" encoding="UTF-8"?>
<resultMap type="com.forward.model.PhoneNumber" id="phoneMap">
<result column="number" property="number"/>
<result column="partition" property="partition"/>
</resultMap>
<select id="findAll" resultMap="phoneMap">
SELECT *
FROM PhoneNumber
ORDER BY number;
</select>
MyBatis mapps the interphace PhoneNumberRepository to this .xml file to create the bean.
The problem was with Java service being different on the deploying machine and local machine where the project was compiled.