CannotLoadBeanClassException in first spring app - java

I'm attempting my first data access example using Spring and have run into this CannotLoadBeanClassException in reference to a class noted in applicationContext.xml file. Here's my directory structure:
My applicationContext.xml file:
When the applicationContext.xml file is read it complains that it cannot find the file JDBCTest. I don't get it. The application compiles fine and the class files are in the target folder. The path is correct in the xml file: com.example.jdbcdataaccess.JDBCTest
So why am I getting:
org.springframework.beans.factory.CannotLoadBeanClassException: Cannot find class [com.example.djbcdataaccess.JDBCTest] for bean with name 'JDBCTest' defined in class path resource [applicationContext.xml]; nested exception is java.lang.ClassNotFoundException: com.example.djbcdataaccess.JDBCTest
at org.springframework.beans.factory.support.AbstractBeanFactory.resolveBeanClass(AbstractBeanFactory.java:1385)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.determineTargetType(AbstractAutowireCapableBeanFactory.java:641)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.predictBeanType(AbstractAutowireCapableBeanFactory.java:609)
at org.springframework.beans.factory.support.AbstractBeanFactory.isFactoryBean(AbstractBeanFactory.java:1484)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.doGetBeanNamesForType(DefaultListableBeanFactory.java:425)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBeanNamesForType(DefaultListableBeanFactory.java:395)
at org.springframework.context.support.PostProcessorRegistrationDelegate.invokeBeanFactoryPostProcessors(PostProcessorRegistrationDelegate.java:81)
at org.springframework.context.support.AbstractApplicationContext.invokeBeanFactoryPostProcessors(AbstractApplicationContext.java:686)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:524)
at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:139)
at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:83)
at com.example.jdbcdataaccess.JdbcdataaccessApplication.main(JdbcdataaccessApplication.java:12)

Related

Eclipse tomcat on web application start java.lang.ClassNotFoundException: org.mockito.Mockito

I can not start web app in eclipse embedded tomcat, but if I deploy war file in standalone tomcat then it works.
The exception I get is:
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name defined in class path resource Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Factory method threw exception; nested exception is java.lang.NoClassDefFoundError: org/mockito/Mockito
The file exception is referring to is annotated with #Configuration and is located in src/test/java;
purpose of the file is to create mock beans for test cases.
The fix to the problem is this link:
How to prevent eclipse from deploying test classes on Tomcat?
The cause of the problem was, when eclipse was assembling a war file it would include src/test/java dir in it, and the dependencies were missing for test cases.
because in pom dependencies for test had test which means that maven will not include those jar files in final bucked product meaning war file.

Either application or integration test Spring context creation fails due to properties file location type

Our application should use a .properties file which is created in deploy time on the host filesystem, thus it is not being bundled with the application .war in build time. It is opened and read by a loader class during the Spring application context creation
<bean id="propertiesLoader" class="com....PropertiesLoader">
<property name="propertiesFileLocation" value="${PROPERTIES_FILE_LOCATION}"/>
...
where the file path value of PROPERTIES_FILE_LOCATION is defined in theapplication.properties file in src\main\resources of the application's -web maven module
PROPERTIES_FILE_LOCATION=/home/.../propertyfilename.properties
and the type of the propertiesFileLocation field is String and it is being accessed via new FileInputStream(propertiesFileLocation) in PropertiesLoader.java class
With this setup the application context creation is successful. However, we also have some integration tests in our project which also setup a Spring context for their execution. We though to store a copy of propertyfilename.properties file into src\test\resources of the -it maven module and refer it from its it.properties like
PROPERTIES_FILE_LOCATION=classpath:somedirectory/propertyfilename.properties
however during the creation of the integration test context we receive the following exception
[ERROR] Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean
with name 'propertiesLoader' defined in URL
[ERROR] Caused by: java.io.FileNotFoundException: classpath:somedirectory\propertyfilename.properties
(The filename, directory name, or volume label syntax is incorrect)
Now if we modify the type of the propertiesFileLocation field to org.springframework.core.io.Resource and we access it via propertiesFileLocation.getInputStream() in PropertiesLoader.java, that makes the integration tests context creation succesful, but at the same the real application context creation fails with
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'propertiesLoader' defined in URL
[file:/.../tomcat/work/Catalina/localhost/_/loader/conf/spring/springmvc/springmvc-util.xml]: Invocation of init method failed;
nested exception is java.io.FileNotFoundException: Could not open ServletContext resource [/home/.../propertyfilename.properties]
This seems to be a "deadlock" situation, as either the application or the integration test context creation fails. Is there a way with which both context can be succesfully created?
Resource loading is quite well documenten in the Spring References Guide. When you don’t prefix the resources with the location to load it from it will default to the default location of the ApplicationContext. For the web application that will be the root of the archive, basically what is in the WEB-INF directory. \
You want to load a file resource for this prefix the resource with file: and in your test use classpath: to have it loaded from the `classpath. This way it will work regardless of which default location the application context uses.

WebLogic 10.3.7 File Upload returns "Error creating bean with name 'portletMultipartResolver'"

I am using Spring Portlet mvc with file upload. When I was using WebLogic 10.3.0 it was working fine. I am migrating to WebLogic 10.3.7. Here with the same code I am getting below error:
javax.portlet.PortletException:
org.springframework.beans.factory.BeanCreationException: Error
creating bean with name 'portletMultipartResolver' defined in
PortletContext resource Instantiation of bean failed; nested exception
is org.springframework.beans.BeanInstantiationException: Could not
instantiate bean class
[org.springframework.web.portlet.multipart.CommonsPortletMultipartResolver]:
Constructor threw exception; nested exception is
java.lang.NoClassDefFoundError: javax/portlet/ActionRequest
WebLogic server is not able to find ActionRequest.class. I have tried several options like placing jar file (netuix_common.jar) which contains ActionRequest.class inside the application war file but still it didn't work.
What can I try to resolve this?
java.lang.NoClassDefFoundError - you get this error when weblogic server finds this class in its classloader but not the one with the correct version.
you did the right thing by adding it to the application war file, you may want to add the flag of " prefer-web-inf-classes "
https://docs.oracle.com/cd/E23943_01/web.1111/e13712/weblogic_xml.htm#WBAPP601

Cannot find class for bean with name

I am trying to run my web application on tomcat server and I am getting this error
Related cause:
org.springframework.beans.factory.CannotLoadBeanClassException: Cannot
find class [com.mycom.myproj.utility.CustomPassword] for bean with
name 'customPassword' defined in ServletContext resource
[/WEB-INF/spring-security.xml]; nested exception is
java.lang.ClassNotFoundException:
com.mycom.myproj.utility.CustomPassword
and my configuration inside spring-security.xml
<beans:bean id="customPassword" class="com.mycom.myproj.utility.CustomPassword" />
and my project folder structure is
src
--main
--resources
application.properties
-- java
Webcontent
--WEB-INF
spring-servelet.xml
spring-security.xml
--jsp
--lib
I build this project using POM.xml
Please let me know if you need more information

Error instantiating extensions registry for chart.theme

I'm trying to generate a PDF with JasperReports.
At the first time I try, I get this error:
ERROR [net.sf.jasperreports.extensions.DefaultExtensionsRegistry] Error instantiating extensions registry for chart.theme
from vfszip:My-App.ear/lib/jasperreports-chart-themes-4.7.0.jar/jasperreports_extension.properties
org.springframework.beans.factory.parsing.BeanDefinitionParsingException: Configuration problem:
Failed to import bean definitions from relative location [defaultChartPropertiesBean.xml]
Offending resource: URL
[My-App.ear/lib/jasperreports-chart-themes-4.7.0.jar/net/sf/jasperreports/chartthemes/spring/beans/chartThemesBeans.xml];
nested exception is org.springframework.beans.factory.parsing.BeanDefinitionParsingException: Configuration problem:
Failed to import bean definitions from relative location [chartConstantsBean.xml]
Offending resource: URL
[My-App.ear/lib/jasperreports-chart-themes-4.7.0.jar/net/sf/jasperreports/chartthemes/spring/beans/defaultChartPropertiesBean.xml];
nested exception is org.springframework.beans.factory.BeanDefinitionStoreException: Unexpected exception parsing XML document from URL
[My-App.ear/lib/jasperreports-chart-themes-4.7.0.jar/net/sf/jasperreports/chartthemes/spring/beans/chartConstantsBean.xml];
nested exception is org.springframework.beans.FatalBeanException:
Class [org.springframework.beans.factory.xml.UtilNamespaceHandler] for namespace
[http://www.springframework.org/schema/util] does not implement the [org.springframework.beans.factory.xml.NamespaceHandler] interface
at org.springframework.beans.factory.parsing.FailFastProblemReporter.error(FailFastProblemReporter.java:68)
at org.springframework.beans.factory.parsing.ReaderContext.error(ReaderContext.java:85)
at org.springframework.beans.factory.parsing.ReaderContext.error(ReaderContext.java:76)
at org.springframework.beans.factory.xml.DefaultBeanDefinitionDocumentReader.importBeanDefinitionResource(DefaultBeanDefinitionDocumentReader.java:219)
at org.springframework.beans.factory.xml.DefaultBeanDefinitionDocumentReader.parseDefaultElement(DefaultBeanDefinitionDocumentReader.java:148)
at org.springframework.beans.factory.xml.DefaultBeanDefinitionDocumentReader.parseBeanDefinitions(DefaultBeanDefinitionDocumentReader.java:133)
at org.springframework.beans.factory.xml.DefaultBeanDefinitionDocumentReader.registerBeanDefinitions(DefaultBeanDefinitionDocumentReader.java:93)
at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.registerBeanDefinitions(XmlBeanDefinitionReader.java:493)
at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.doLoadBeanDefinitions(XmlBeanDefinitionReader.java:390)
at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:334)
at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:302)
at org.springframework.beans.factory.xml.XmlBeanFactory.<init>(XmlBeanFactory.java:73)
at org.springframework.beans.factory.xml.XmlBeanFactory.<init>(XmlBeanFactory.java:61)
at net.sf.jasperreports.extensions.SpringExtensionsRegistryFactory.getBeanFactory(SpringExtensionsRegistryFactory.java:101)
However, the PDF is generated without any problem.
Plus, the next time I try to generate one PDF, no more errors. This only occurs on the first generation.
What is a reason? And how I can avoid this error?
OK Problem Solved.
I included jasperreports-chart-themes-4.7.0.jar in my classpath. However, this jar wasn't used anymore. I only used the default themes from jasper.
Deleting this jar from .ear and everything works fine.
Thanks anyway

Categories