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.
Related
Sometimes I get a BeanCreationException when starting a Java Spring Boot single-JAR application built by Gradle. My investigations show that it depends on the JAR file and how it has been built by Gradle. Most often (but not always) I see the exception when I build the project on a Linux system. But I have never reproduced the issue with a JAR built on Windows or when running from IntelliJ Idea.
I tried to compare the content of a working JAR with the JAR throwing the exception - all the *.class files (including meta and resources) were binary equal, the only difference was in the order the files were stored in the JAR/ZIP archive. I also tried to unpack the failing JAR on Windows and just repack it into a new JAR file (using 7-zip) - the application started without any exceptions. This weird workaround solved the issue, but it's not something I'd like to do after each build on a linux machine.
The exception suggests to check circular references, so I tried replacing #Autowired properties with #Autowired bean constructors to help me to find the problem but that didn't help. The stacktrace does not mention any of my classes, so I don't know what bean could be responsible for the issue. And because of the fact the issue happens only sometimes and is solvable by repacking the JAR file, I'm not sure there are any circular references anyway.
Could you please help me? Any advice or suggestion is welcomed.
JDK: openjdk 8u262
Gradle version 5.6
Spring boot 2.3.1
Exception message:
BeanCreationException: Error creating bean with name 'webConfig':
Invocation of init method failed; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException:
Error creating bean with name 'requestMappingHandlerAdapter' defined in class path resource
[org/springframework/boot/autoconfigure/web/servlet/WebMvcAutoConfiguration$EnableWebMvcConfiguration.class]:
Unsatisfied dependency expressed through method 'requestMappingHandlerAdapter' parameter 0;
nested exception is org.springframework.beans.factory.BeanCurrentlyInCreationException:
Error creating bean with name 'mvcContentNegotiationManager':
Requested bean is currently in creation: Is there an unresolvable circular reference?
By default, Spring manages itself bean's lifecycle and then, arranges their initialization order during the startup.
Here a full example of usage #DependsOn annotation to order the bean instanciation.
You can also found more detail in the official Spring documentation
I am developing a simple CRUD application with spring-boot.
I have most of the project completed, although I get this error when I try to run the project.
Description:
Field userDBOP in com.application.crud.GreetingController required a
bean of type 'com.application.crud.myoperation.JdbcUserDAO' that could
not be found.
Action:
Consider defining a bean of type
'com.application.crud.myoperation.JdbcUserDAO' in your configuration.
In IntelliJ when I hover over the line that causes the error, the following message shows up
"Could not autowire. No beans of 'JdbcUserDAO' type found.
Even though I have in my 'Beans.xml' file (located below the 'src' directory:
<bean id="customerDAO" class="com.application.crud.myoperation.JdbcUserDAO">
<property name="dataSource" ref="dataSource" />
</bean>
Can anyone tell me how to fix this error?
This seems to be a problem with configuring a Spring Boot Application with an existing Spring Context. There is a section in the Spring documentation about this.
By default, you need to specify the location of your application context using the #ImportResource annotation. An example would be:
#SpringBootApplication
#ImportResource("applicationContext.xml")
public class ExampleApplication {
public static void main(String[] args) throws Exception {
SpringApplication.run(ExampleApplication.class, args);
}
}
Note that if the file is located elsewhere in the classpath, then you need to reference it properly for spring to pick it up (e.g #ImportResource({"classpath*:applicationContext.xml"}) )
In my Spring application, I have a simple properties file located in folder WEB-INF\classes so that it, the DispatcherServlet and various other config files are in the classpath.
The props file is defined in the DispatcherServlet as:
<bean id="propertiesFactory" class="org.springframework.beans.factory.config.PropertiesFactoryBean">
<property name="location">
<value>/WEB-INF/classes/library.properties</value>
</property>
</bean>
The propertiesFactory bean is injected into a controller:
#Autowired
private Properties propertiesFactory;
And used in a one of the controller's methods as:
if (adminPassword.equals(propertiesFactory.getProperty("adminPassword"))) {
This all works perfectly, except for a test program as follows which has line:
ConfigurableApplicationContext context = new ClassPathXmlApplicationContext("library-servlet.xml");
Which throws a BeanCreationException:
Injection of autowired dependencies failed
Because of:
java.io.FileNotFoundException: class path resource [WEB-INF/classes/library.properties] cannot be opened because it does not exist
But if the whole application can see the props file, why not this one program?
Everything in WEB-INF/classes is added to the root of the classpath. As such, you need to refer to your resource simply as
library.properties
or better yet
classpath:library.properties
in
<property name="location">
<value>classpath:library.properties</value>
</property>
You may find it useful to run
System.out.println(System.getProperty("java.class.path"));
and see what was used as classpath entries.
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
I have a simple Spring MVC application that looks up some user details from an LDAP server and prints out a simple HTML page using a JSP. The application works fine on Tomcat 6. It uses Spring LDAP 1.3.1 and LDAPTemplate to do the LDAP lookups.
However, when I deploy this application WAR to Websphere 7, the app doesn't run -- Websphere returns a 500 Internal server error. Looking at Websphere's log file, I see
[14/12/10 14:50:09:169 GMT] 00000022 DispatcherSer E org.springframework.web.servlet.FrameworkServlet initServletBean Context initialization failed
org.springframework.beans.factory.CannotLoadBeanClassException: Error loading class [org.springframework.ldap.core.support.LdapContextSource] for bean with name 'contextSource' defined in ServletContext resource [/WEB-INF/spring-servlet.xml]: problem with class file or dependent class; nested exception is java.lang.NoClassDefFoundError: org.springframework.beans.factory.InitializingBean
at org.springframework.beans.factory.support.AbstractBeanFactory.resolveBeanClass(AbstractBeanFactory.java:1253)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.predictBeanType(AbstractAutowireCapableBeanFactory.java:576)
at org.springframework.beans.factory.support.AbstractBeanFactory.isFactoryBean(AbstractBeanFactory.java:1319)
at org.springframework.beans.factory.support.AbstractBeanFactory.isFactoryBean(AbstractBeanFactory.java:885)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:562)
at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:895)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:425)
My web-inf\lib directory has all the JAR files, including org.springframework.beans-3.0.5.RELEASE.jar, which contains InitializingBean. I'm therefore not sure why Websphere is reporting the class as missing.
Contents of my web-inf\lib:
aopalliance.jar
com.springsource.org.apache.commons.logging-1.1.1.jar
com.springsource.org.apache.log4j-1.2.15.jar
commons-lang-2.5.jar
commons-logging-1.1.1.jar
commons-pool-1.5.4.jar
jstl-api-1.2.jar
jstl-impl-1.2.jar
ldapbp-1.0.jar
org.springframework.aop-3.0.5.RELEASE.jar
org.springframework.asm-3.0.5.RELEASE.jar
org.springframework.beans-3.0.5.RELEASE.jar
org.springframework.context-3.0.5.RELEASE.jar
org.springframework.core-3.0.5.RELEASE.jar
org.springframework.expression-3.0.5.RELEASE.jar
org.springframework.jdbc-3.0.5.RELEASE.jar
org.springframework.oxm-3.0.5.RELEASE.jar
org.springframework.transaction-3.0.5.RELEASE.jar
org.springframework.web-3.0.5.RELEASE.jar
org.springframework.web.servlet-3.0.5.RELEASE.jar
spring-ldap-1.3.1.RELEASE-all.jar
And here's the definition of the contextSource bean which Websphere is having trouble loading (the username/password is valid and works with Tomcat):
<bean id="contextSource" class="org.springframework.ldap.core.support.LdapContextSource">
<property name="url" value="ldaps://moo.example.com:1300/" />
<property name="userDn" value="CN=foo,OU=baz,DC=bar,DC=blat,DC=org" />
<property name="password" value="*******" />
</bean>
I'd be very glad if someone could point out why this isn't working on Websphere. I'm not too sure about class-loading rules in Websphere and would appreciate any advice on this.
Turn on Class Loading trace. This would tell you what classes are loaded and from which Jar. As Sajan mentioned chances are that there are duplicate jars in the classpath and the classloader that you expect has not loaded this class (and a parent class loader has likely loaded a class).
NoClassDefFoundError (NCDFE) does mean it could not locate the class that it is looking for. Errors in static initialization would explicitly turn out as a "java.lang.ExceptionInInitializerError" (EIIE)
"
Both NCDFE and EIIE extend from Linkage error.
In general all these errors can easily be troubleshooted by turning on the class loading for the Server. Also use the Class Loader Viewer available in the admin console to aid in your troubleshooting activities.
HTH
Manglu
This is a sticky and common exception. Remember, that NoClassDefFoundError doesn't mean the class was not found, rather it means:
NoClassDefFoundError: The given class
could be found, but something went
wrong when initializing it (an
interface it implemented could not be
found, something went wrong in a
static initializer etc.).
From here.
Please check that you don't have the same class in any other possible jar
Please check if you have added the dependencies in pom.xml file.