Context Initialization Failure -- BeanCreationNotAllowedException - java

The Spring Context of my application is failing to initialize. Can anyone help me understand why it is failing and how to fix it?
Below are the warning & error messages I'm getting:
[WARN] Invocation of destroy method 'shutdown' failed on bean with
name 'cxf'
org.springframework.beans.factory.BeanCreationNotAllowedException:
Error creating bean with name 'entityManagerFactory': Singleton bean
creation not allowed while the singletons of this factory are in
destruction (Do not request a bean from a BeanFactory in a destroy
method implementation!)
org.springframework.web.context.ContextLoader
[ERROR] Context initialization failed
<bean id="cxf" class="org.apache.cxf.bus.CXFBusImpl" destroy-method="shutdown"/>
<bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="persistenceUnitName" value="${persistence.unit}"/>
<property name="dataSource" ref="pooledDs"/>
<property name="jpaVendorAdapter">
<bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
<property name="databasePlatform" value="org.hibernate.dialect.MySQLDialect"/>
<property name="showSql" value="false"/>
<property name="generateDdl" value="false"/>
</bean>
</property>
</bean>

The CXF website doesn't include the destroy-method call in its example configurations, so it seems like this is a misconfiguration. See this page for details: http://cxf.apache.org/docs/interceptors.html.
I also found a bug tracker for this issue: https://issues.apache.org/jira/browse/CXF-2164. It appears that the destroy method was not implicitly being called in earlier versions of CXF, but that has been fixed in v2.2.11.
So, my suggestion would be to get up to at least that version and just have <bean id="cxf" class="org.apache.cxf.bus.CXFBusImpl" /> in your config.

In my case I had same problem with version 2.5.0, but that was my fault.
I had wrong bean in context.
In detail: I had Spring MVC Controller (named OrderController) annotated with #Controller without defined name (annotation driven). On the other CXF requires xml configuration AFAIK, so I named bean using java configuration (using #Bean) as orderController and somehow when cxf was initialized Spring used this wrong MVC controller and it failed on error listed above.
org.springframework.beans.factory.BeanCreationNotAllowedException: Error creating bean with name 'cxf': Singleton bean creation not allowed while the singletons of this factory are in destruction (Do not request a bean from a BeanFactory in a destroy method implementation!)

Related

Spring how to set proxy property dynamically to rest template

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

Spring : Initialization of properties before any bean creation

I have the project structure as following -
Facade -> Service-> DAO
In the DAO layer, when the beans are initialized then many dependencies are injected from a property file. Therefore, the properties file must be read first and then the remaining dao beans must be created. When the application is started then it gives an error that Spring cannot resolve a placeholder.
The DAO-application-context.xml is like-
<bean
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="prop">
<value>app.properties</value>
</property>
</bean>
<import resource = "a-dao.xml" />
<import resource = "b-dao.xml" />
<import resource = "c-dao.xml" />
Now in all the child application contexts i.e. a-dao, etc, we have-
<bean ....>
<property name = "xyz">
<value>${appValue}<value/>
</property>
<bean>
The error received is that appValue cannot be resolved. I think that it may be due to incorrect sequence of bean creation. However, the same config is working in another larger project.
I have checked Order of Spring Bean Initialization but implementing that solution would not be feasible. Is there any other way ?
Reg this Block of Configuration, property prop seems to be wrong
<bean
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="prop">
<value>app.properties</value>
</property>
</bean>
According to the Spring documentation
You could use the property location or locations to set the one or multiple values of the properties file.
So the code should be refactored to
<bean
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location">
<value>app.properties</value>
</property>
</bean>

Spring Instantiation exception with java.lang.String

What is wrong with the below bean? Using spring-beans-2.0 I'm getting below exception:
<bean id="logger" class="java.lang.String">
<constructor-arg value="logger"/>
</bean>
logger bean ibjecting to :
<bean id="loggerType" class="java.lang.String" scope="prototype">
<constructor-arg value="logger" />
</bean>
loggerbean injecting other bean that is correctly have argument as "java.lang.String".
Exception
Could not instantiate bean class [java.lang.String]: Illegal arguments for constructor;
nested exception is java.lang.IllegalArgumentException:
java.lang.ClassCastException#5083198c
If you are injecting another bean then use ref attribute instead of value attribute.
<bean id="loggerType" class="java.lang.String" scope="prototype">
<constructor-arg ref="logger" />
</bean>
Or use <ref/> tag with bean as attribute
<bean id="loggerType" class="java.lang.String" scope="prototype">
<constructor-arg>
<ref bean="logger"/>
</constructor-arg>
</bean>
For more info have a look at Spring documentation References to other beans (collaborators)
I suggest to move latest version of Spring - 4.0.6.RELEASE
String class has many one-argument constructors so Spring could choose wrong one, hence the exception.
I doubt it happens in newer versions of Spring. You said that you use Spring 2 and there is a bug related to this. But it seems to be fixed in newer versions.
The bug report says that it is fixed in version 3.0.3.

How to inject child dependency using byType autowiring?

I have two child classes(PermanentEmployee and ContractEmployee) of Employee class.
I want spring to inject the dependencies under TextEditor1 by type . Along with this i want to inject the PermanentEmployee depndency under TextEditor1.
similarily want to inject the contractEmployee dependency under TextEditor2. Rest should be injected
automatically by type?
<bean id="textEditor1" class="com.TextEditor" autowire="byType">
<property name="employee" ref="permanentEmployee" />
</bean>
<bean id="textEditor2" class="com.TextEditor" autowire="byType">
<property name="employee" ref="contractEmployee" />
</bean>
<bean id="permanentEmployee" class="com.PermanentEmployee" >
</bean>
<bean id="contractEmployee" class="com.ContractEmployee">
</bean>
But i get the error Caused by: org.springframework.beans.factory.NoUniqueBeanDefinitionException: saying two match are found ?
Update :- i also tried below but it didn't work either
<bean id="textEditor1" class="com.TextEditor" autowire="byType">
<qualifier type="permanentEmployee"/>
</bean>
I think that's because those two com.PermanentEmployee and com.ContractEmployee are implementing another interface like com.Employee?
That way, Spring will recognize those same type and can't choose which bean Spring have to auto-wire into the bean.
So you might need to add those byName, not byType in this case.
If you change those injection with #Autowired annotation or #Resource annotation, you can use #Qualifier( for Autowired ) or name property of Resource annotation to specify bean name.

Webservice using CXF and Spring 3 webserviceTemplate gets error on Jboss 5 due to Soap Message Factory

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>

Categories