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
Related
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.
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.
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)
I am just learning spring and my first program is to load a bean xml and instantiate the classes.
I will be using spring core and I have created a Java project. Created a package called:
com.lecture
and put the java files there as well as the bean file
spring-beans.xml
<beans>
<bean id="rkdf" class="com.college.RKDFCollege"/>
<bean id="cseLect" class="com.college.CSELecturer"/>
</beans>
But when I am running the main class which has this code:
ApplicationContext context = new ClassPathXmlApplicationContext("com.lecture.spring-beans.xml");
it is giving me errors
Dec 07, 2014 9:10:37 PM
org.springframework.beans.factory.xml.XmlBeanDefinitionReader
loadBeanDefinitions INFO: Loading XML bean definitions from class path
resource [com.lecture.spring-beans.xml] Exception in thread "main"
org.springframework.beans.factory.BeanDefinitionStoreException:
IOException parsing XML document from class path resource
[com.lecture.spring-beans.xml]; nested exception is
java.io.FileNotFoundException: class path resource
[com.lecture.spring-beans.xml] cannot be opened because it does not
exist
I am new to this I dont have any idea so far! In the project module settings, I have added JDK and Spring jars. Guidance is appreciated.
Try using the following path to the XML: "com/lecture/spring-beans.xml"
ClassPathXmlApplicationContext treats your class path as a file system.
ApplicationContext context = new ClassPathXmlApplicationContext("com.lecture.spring-beans.xml"); is searching com.lecture.spring-beans.xml file in the class path which I believe is not the name of the file you have created. I believe you have created file spring-beans.xml
Make sure spring-beans.xml file is in class path. Else you can put the spring-beans.xml in resource folder as well. Then load the file as:
ApplicationContext context = new ClassPathXmlApplicationContext("spring-beans.xml");
I am currently trying to start up a server side of an application, built with Maven; the clean install is succesful, but then tomcat7:run fails with the following Spring error message
04:21:19,059 [localhost-startStop-1] ERROR org.springframework.web.context.ContextLoader -
Context initialization failed org.springframework.beans.factory.BeanCreationException:
Error creating bean with name 'properties' defined in class path resource [context.xml]:
Initialization of bean failed; nested exception is java.lang.NoSuchMethodError:
org.springframework.core.convert.Property.<init>(Ljava/lang/Class;Ljava/lang/reflect/Method;
Ljava/lang/reflect/Method;Ljava/lang/String;)V
The concerned bean definition in context.xml is as follows
<bean id="properties" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location" value="classpath:server.properties"/>
<property name="ignoreUnresolvablePlaceholders" value="true"/>
</bean>
Research shows that these kind of errors are usually related to incorrect JAR versions on the classpath. In this particular application, Spring version 3.2.4.RELEASE dependencies are defined in Maven pom.xml, and checking out the API of the concerned org.springframework.core.convert.Property class it seems that constructor Property(Class objectType, Method readMethod, Method writeMethod, String name) is actually available in this version. Any ideas are appreciated
Try to run your java with -verbose option.
It shows wich jar has been loaded class from.
Looks like there is a spring jar in the bootstrap classloader of your Tomcat - older version of Spring.