ClassNotFoundException Issue with Spring DI Demo - spring

I'm not able to run the program.
Exception in thread "main" org.springframework.beans.factory.CannotLoadBeanClassException: Cannot find class [com.tutorialspoint.HelloWorld] for bean with name 'hello' defined in class path resource [beans.xml]; nested exception is java.lang.ClassNotFoundException:

Your bean definition must point to the class not the package name
<bean id="hello" class="com.tutorialspoint.HelloWorld.MainApp">
<property name="message" value="Hello Man"></property>
</bean>

your Spring Beans Configuration is perfect but you have to give correct path for ApplicationContext in MainApp class according to your code your path should be "main/resource/beans.xml" . Hope this will work...

Related

How to resolve "cyclic loading of class path resource"?

I have two separate projects. One is maven, spring webservices(Let's say this is project A) and the other is maven batch project(Let's call it B).
I am trying to inject a class(let's say class "batch.java") from B into a class(let's call it "api.java") of A.
One major problem statement is that B has a dependency on A( on a different class of A, not on api.java).And I believe this is causing the cyclic loading error on startup.
I will paste my code here and I request to please provide a solution to it:
file:B.xml
<import resource="classpath:A.xml" />
<bean id="batch" class="XXX">
<!-- lookup methods from project A -->
</bean>
file:A.xml
<import resource="classpath*:B.xml"/>
<bean id="api" scope="prototype" lazy-init="true">
<property name="batch" ref="batch" />
</bean>
The code compiles fine but when I try to start my wsdl I get the following error:
org.springframework.beans.factory.parsing.BeanDefinitionParsingException: Configuration problem: Failed to import bean definitions from URL location [classpath:B.xml]
Offending resource: class path resource [A.xml]; nested exception is org.springframework.beans.factory.parsing.BeanDefinitionParsingException: Configuration problem: Failed to import bean definitions from URL location [classpath:A.xml]
Offending resource: class path resource [B.xml]; nested exception is org.springframework.beans.factory.BeanDefinitionStoreException: Detected cyclic loading of class path resource [A.xml] - check your import definitions!
org.springframework.beans.factory.parsing.FailFastProblemReporter.error(FailFastProblemReporter.java:68)
Unfortunately you need to break the cyclic dependency.
Put all commonly used bean definitions into a separate context and reference this context in your sub-contexts.
Then you should end up with at least 3 context.xml's - eg:
common-context.xml (contains all bean definitions that are used in all sub-contexts)
a-context.xml (includes common-context.xml)
b-context.xml (includes common-context.xml)

Unable to create transactionManager (JTA) bean using Spring 2.5.6 on JBOSS EAP 6.3

I'm getting this error:
Error creating bean with name 'wsJtaTm' defined in ServletContext resource [/WEB-INF/applicationContext.xml]:
Instantiation of bean failed; nested exception is org.springframework.beans.BeanInstantiationException: Could not instantiate bean class
[org.springframework.transaction.jta.JotmFactoryBean]: Constructor threw exception; nested exception is java.lang.NoSuchMethodError: stub class initialization failed
I'm using the following piece of code to create txnmanager bean for Jboss
Datasource and JNDI correctly loading and configured.
applicationcontext.xml
<bean id="wsJtaTm" class="org.springframework.transaction.jta.JotmFactoryBean" />
<bean id="transactionManager" class="org.springframework.transaction.jta.JtaTransactionManager">
<property name="userTransaction" ref="wsJtaTm" />
</bean>
What is wrong with my code?

Spring context scanning behavior

I'm getting the following:
2013-03-27 18:51:54,944 ERROR pringframework.web.context.ContextLoader: 227 - Context initialization failed
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'exporter' defined in URL ... Cannot resolve reference to bean 'dynamicNamingStrategy' while setting bean property 'namingStrategy'; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'dynamicNamingStrategy' is defined
at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveReference(BeanDefinitionValueResolver.java:328)
at
and also STS is complaining:
Multiple annotations found at this line:
- Referenced bean 'dynamicNamingStrategy'
not found
with the following Spring contexts setup:
In a project, I have a jmx management context (core-app-web-common-management-context.xml) with following (excerpt):
<context:annotation-config />
<context:component-scan
base-package="com.a.b.c.management.*" />
<bean id="exporter" class="org.springframework.jmx.export.MBeanExporter"
lazy-init="false">
<property name="autodetect" value="true"></property>
<property name="namingStrategy" ref="dynamicNamingStrategy"/>
where dynamicNamingStrategy is defined in a different project (and packaged into a different jar) and is referred to in the component-scan above, as follows
package com.a.b.c.management;
#Component("dynamicNamingStrategy")
public class DynamicNamingStrategy extends KeyNamingStrategy {
......
The above Spring context is in turn imported into the main context located in the same project:
<import resource="classpath*:/META-INF/spring/core-app-web-common-management-context.xml"/>
So, somehow the #Component scanning for DynamicNamingStrategy is not working...
If I instead use this property definition in exporter, then it works:
<property name="namingStrategy"><bean class="com.a.b.c.management.DynamicNamingStrategy" /></property>
but I'd like to understand why the component scanning mechanism falters in the first instance, apparently I'm missing something.
Also, Spring Explorer view in STS is missing that bean as well. Enabling support for elements in configuration files option doesn't seem to make a difference.
Drop the .* at the end of your package name in your <context:component-scan> elements base-package attribute. I would think that this is causing the component-scan to look for a package named *, which there probably isn't one (can't be one).
The base-package is just that, the base package. The component-scan will scan all children of all base packages, therefore there is no need to have the .* wildcard at the end of your package name.

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>

replaced-method question in spring 3

Excuse me. I'm a beginner of spring. Now, there's some question in it.
I'm reading Spring in action and try the code in MyEclipse. But when I try the code of replaced-method, there's a error.
<bean id="harry" class="com.springinaction.sprintidol.Magician">
<property name="magicBox" ref="magicBox" />
<property name="magicWords" value="Bippity boppity boo" />
</bean>
<bean id="magicBox" class="com.springinaction.sprintidol.MagicBoxImpl">
<replaced-method name="getContents" replacer="tigerReplacer" />
</bean>
<bean id="tigerReplacer" class="com.springinaction.sprintidol.TigerReplacer" />
This is the applicationContext.xml. And the exception as followed:
Exception in thread "main" org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'stevie' defined in class path resource [applicationContext.xml]: Instantiation of bean failed; nested exception is java.lang.NoClassDefFoundError: net/sf/cglib/proxy/CallbackFilter
All of the code come from the book. Why do I get the exception? And how to solve?
Thanks.
I found the reason. Because I didn't include the Spring 3.0 Persistence Core Libraries.Thanks all the same.

Resources