Spring, Tomcat, Java - java

When I run .war file in tomcat, the logs show
ERROR [com.configleon.configurer.WebPropertyConfigurer] - The 'configLocation' variable is not specified in the JVM settings!
ERROR [org.springframework.web.context.ContextLoader] - Context initialization failed
And this my code :
<!-- configlion property configurator -->
<bean class="com.configleon.configurer.WebPropertyConfigurer">
<property name="propertyResources">
<bean class="com.configleon.resource.WebPropertyResources"/>
</property>
</bean>
Anyone can help me please ?

for first one ERROR [com.configleon.configurer.WebPropertyConfigurer]
see the here
and for second one
ERROR [org.springframework.web.context.ContextLoader] - Context initialization failed
in deployment environment, just make sure your server classpath has included the Spring jar library (e.g spring-2.5.6.jar).
For Spring3, ContextLoaderListener is moved to spring-web.jar, you can get the library from Maven central repository.
Markup
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>3.0.5.RELEASE</version>
</dependency>

Related

Tomcat classpath loader javax.mail.Session conflict with spring-boot-starter-mail

Trying to deploy a Spring Boot app on Tomcat (not the embedded Tomcat). I have configured a Java mail session on the Tomcat server config, and I'm trying to access it as a JNDI value in my app. For some reason, my app gets an error and shows this:
Caused by: java.lang.IllegalArgumentException: The local resource link [support] that refers to global resource [mail/support] was expected to return an instance of [javax.mail.Session] but returned an instance of [javax.mail.Session]
at org.apache.naming.factory.ResourceLinkFactory.getObjectInstance(ResourceLinkFactory.java:163)
at javax.naming.spi.NamingManager.getObjectInstance(NamingManager.java:321)
at org.apache.naming.NamingContext.lookup(NamingContext.java:840)
I have included javax.mail.jar in the Tomcat /lib folder. I also have spring-boot-starter-mail included in my pom.xml
I've tried removing the javax.mail.jar from Tomcat's lib, but that causes an error on Tomcat start because it can't create the mail session. I've also tried removing spring-boot-starter-mail, but that interferes with some of my code that requires JavaMailSender and other mail components. I've tried messing with the JNDI import and stuff like that, but to no avail. I've also tried checking the version of the mail jar included by spring-boot-starter-mail, and updating the jar in Tomcat to match. I've also checked my transitive dependencies in Maven to see if a different mail implementation is being pulled in, and there's nothing. So I'm kind of all out of ideas.
Here's where I'm getting the JNDI value in my web.xml:
<resource-ref>
<description>The mail session configured in Tomcat</description>
<res-ref-name>mail/support</res-ref-name>
<res-type>javax.mail.Session</res-type>
<res-auth>Container</res-auth>
</resource-ref>
Here's what I have configured in Tomcat's server.xml:
<GlobalNamingResources>
<Resource name="mail/support"
auth="Container"
type="javax.mail.Session"
mail.smtp.host="smtp.XXX.XXX"
mail.smtp.user="support"
mail.smtp.from="support#XXX.org" />
</GlobalNamingResources>
And here's what's in context.xml:
<Context>
<ResourceLink global="mail/support" name="mail/support" type="javax.mail.Session" />
</Context>
I'd like to be able to use spring-boot-starter-mail, and use a globally configured JNDI mail session. I don't know if those are just incompatible wishes, but I don't see why they should be.
Ok, so the solution was indeed what #Bill Shannon suggested. I had to include the com.sun.mail dependency with the Maven provided scope. My particular issue was that the project was already using the spring-boot-starter-mail dependency, which includes the com.sun.mail jar. So I had to exclude that from my Maven dependency. So the complete Maven dependency related to mail stuff looks like this:
<dependency>
<groupId>com.sun.mail</groupId>
<artifactId>javax.mail</artifactId>
<version>1.5.6</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-mail</artifactId>
<exclusions>
<exclusion>
<groupId>com.sun.mail</groupId>
<artifactId>javax.mail</artifactId>
</exclusion>
</exclusions>
</dependency>
That seems a little obvious now. I think my main source of confusion was the weird Tomcat error message. Thanks for anyone who took a look at this, hopefully this resolves the issue for someone else!

java.lang.NoClassDefFoundError: org/apache/commons/fileupload/FileUploadException

I have created a Spring MVC Maven project in which I have added a commons-fileupload 1.4 dependency.
<dependency>
<groupId>commons-fileupload</groupId>
<artifactId>commons-fileupload</artifactId>
<version>1.4</version>
</dependency>
Moreover I have added the bean reference for Multipart resolver into my Spring configuration file:
<bean name="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<property name="maxUploadSize" value="5252880"></property>
</bean>
I've checked into my local repository for corresponding jar files and they are present however when I deploy my project into Websphere Liberty server I'm getting the error:
Caused by: java.lang.ClassNotFoundException: org.apache.commons.fileupload.FileItemFactory
What am I doing wrong in adding the missing dependency?

Velocity 2.0: NoClassDefFoundError: org/apache/velocity/runtime/log/CommonsLogLogChute

On starting up my Web app with Velocity 2.0 I'm getting the following error:
Caused by: java.lang.NoClassDefFoundError:
org/apache/velocity/runtime/log/CommonsLogLogChute
at org.springframework.ui.velocity.VelocityEngineFactory.createVelocityEngine(VelocityEngineFactory.java:240)
at org.springframework.ui.velocity.VelocityEngineFactoryBean.afterPropertiesSet(VelocityEngineFactoryBean.java:60)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1687)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1624)
... 34 more
All dependencies have been satisfied. We are using
slf4j-api-1.7.25.jar
slf4j-simple-1.7.25.jar
commons-lang3-3.5.jar
commons-io-2.5.jar
commons-logging-1.2.jar (yes we do have Commons-Logging)
In applicationContext.xml the velocityEngine bean is defined as follows
<bean id="velocityEngine"
class="org.springframework.ui.velocity.VelocityEngineFactoryBean"/>
Any thoughts on this?
My file velocity-engine-core-2.0.jar only contains the following .runtime subpackages:
defaults, directive, parser, resource, visitor
but no log .
UPDATE The following overrideLogging = FALSE in the Spring Velocity Engine bean declaration solved the problem. But why?
<bean id="velocityEngine" class="org.springframework.ui.velocity.VelocityEngineFactoryBean">
<property name="overrideLogging" value="false" />
</bean>
I just followed a tip on
https://docs.spring.io/spring-framework/docs/current/javadoc-api/org/springframework/ui/velocity/VelocityEngineFactory.html
but not sure what happened.
When overrideLogging is true, the class org.apache.velocity.runtime.log.CommonsLogLogChute is still required by Spring while it has disappeared in Velocity 2.0, since Velocity now uses the slf4j logging framework.
You'll need to wait for an update of the Spring Velocity classes if you need overrideLogging to be true.
Edit on June 21st, 2022 - Since version 2.3, Velocity does provide Spring support.
Velocity made a change in logging:
Make Velocity use the base logger namespace 'org.apache.velocity'
unless specified with runtime.log.name in the configuration, and have
the runtime instance log with this base namespace, and other modules
log with children namespaces
CommonsLogLogChute added in before major version velocity 1.7:
Add a CommonsLogLogChute that allows logging through commons-logging.
So you probably have an old jar or configuration in your runtime environment.
Alibaba implemented a support context package for that case: https://github.com/alibaba/spring-velocity-support
Just add to maven:
<!-- Spring Framework -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context-support</artifactId>
<version>${spring.framework.version}</version>
</dependency>
<!-- Spring Context Velocity -->
<dependency>
<groupId>com.alibaba.spring</groupId>
<artifactId>spring-context-velocity</artifactId>
<version>1.4.3.18.RELEASE</version>
</dependency>
But beware that your project now uses Velocity 2.0.

Unable to disable logging messages

I have some trouble getting rid of debug messages generated by Spring (similiar to the following ones; there are thousands of those entries):
19:58:08.380 [main] DEBUG o.s.b.f.s.DefaultListableBeanFactory - Creating shared instance of singleton bean 'propertyPlaceholderConfigurer'
19:58:08.380 [main] DEBUG o.s.b.f.s.DefaultListableBeanFactory - Creating instance of bean 'propertyPlaceholderConfigurer'
19:58:08.383 [main] DEBUG o.s.b.f.s.DefaultListableBeanFactory - Creating shared instance of singleton bean 'appConfig'
19:58:08.383 [main] DEBUG o.s.b.f.s.DefaultListableBeanFactory - Creating instance of bean 'appConfig'
19:58:08.383 [main] DEBUG o.s.b.f.s.DefaultListableBeanFactory - Eagerly caching bean 'appConfig' to allow for resolving potential circular references
19:58:08.384 [main] DEBUG o.s.b.f.s.DefaultListableBeanFactory - Finished creating instance of bean 'appConfig'
In related questions, there were many suggestions involving log4j, web.xml, ....
However, I am not using any of those - I simply instantiate an AnnotationConfigApplicationContext and start creating beans.
In my pom.xml file, there are no references to any logging framework - I only include the spring dependencies:
<!-- Spring and Transactions -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${spring-framework.version}</version>
</dependency>
<!-- ... -->
<artifactId>spring-tx</artifactId>
<!-- ... -->
<artifactId>spring-boot-starter</artifactId>
<!-- ... -->
<artifactId>spring-web</artifactId>
<!-- ... -->
I read somewhere that Spring seems to use "Commons logging" by default, which I unsuccessfully tried to disable using (as shown in Turn Off Apache Common Logging ):
System.setProperty("org.apache.commons.logging.Log", "org.apache.commons.logging.impl.NoOpLog");
In addition, I tried to exclude the commons logging in my pom.xml by adding:
<exclusions>
<!-- Exclude Commons Logging in favor of SLF4j -->
<exclusion>
<groupId>commons-logging</groupId>
<artifactId>commons-logging</artifactId>
</exclusion>
</exclusions>
Still no luck, however.
Next, I tried including a dependency to log4j, hoping this would override the default logging. As the format of the messages stayed the same, it seems that this attempt was also not successfull.
What could I try next?
First: Is slf4j on classpath?
SLF4J is another log abstraction for Java, which also could be used together with Spring framework. Many libraries / products have switched to slf4j.
Are there any dependencies with 'slf4j' in its name? Try mvn dependency:tree -Dverbose=true, and look if slf4j appears. If so, look at slf4j website for more information about its setup.
Second: which log4j config file is used?
Hint to detect if log4j is used, and if a log4j config file is somewhere on the classpath:
Try to set property log4j.debug to 'true'.
When using mvn exec:java, simply add -Dlog4j.debug=true to command line.
If this is a Junit test with the maven surefire plugin, try set systemProperties in Surefire plugin itself: http://maven.apache.org/surefire/maven-surefire-plugin/examples/system-properties.html

Error loading DispatcherServlet's default strategy class

I m getting the following exception while trying to deploy the war file on Weblogic 10.3.6 server.
The application is using SPring mvc.
Error loading DispatcherServlet's default strategy class [org.springframework.web.servlet.mvc.support.Def
aultHandlerExceptionResolver] for interface [org.springframework.web.servlet.HandlerExceptionResolver]: problem with class file or dependent class; nested exception is
java.lang.NoClassDefFoundError: org/springframework/web/bind/MethodArgumentNotValidException
at org.springframework.web.servlet.DispatcherServlet.getDefaultStrategies(DispatcherServlet.java:766)
at org.springframework.web.servlet.DispatcherServlet.initHandlerExceptionResolvers(DispatcherServlet.java:604)
at org.springframework.web.servlet.DispatcherServlet.initStrategies(DispatcherServlet.java:423)
at org.springframework.web.servlet.DispatcherServlet.onRefresh(DispatcherServlet.java:410)
at org.springframework.web.servlet.FrameworkServlet.onApplicationEvent(FrameworkServlet.java:752)
at org.springframework.web.servlet.FrameworkServlet$ContextRefreshListener.onApplicationEvent(FrameworkServlet.java:989)
at org.springframework.web.servlet.FrameworkServlet$ContextRefreshListener.onApplicationEvent(FrameworkServlet.java:1)
at org.springframework.context.event.GenericApplicationListenerAdapter.onApplicationEvent(GenericApplicationListenerAdapter.java:51)
at org.springframework.context.event.SourceFilteringListener.onApplicationEventInternal(SourceFilteringListener.java:97)
at org.springframework.context.event.SourceFilteringListener.onApplicationEvent(SourceFilteringListener.java:68)
at org.springframework.context.event.SimpleApplicationEventMulticaster.multicastEvent(SimpleApplicationEventMulticaster.java:97)
at org.springframework.context.support.AbstractApplicationContext.publishEvent(AbstractApplicationContext.java:324)
at org.springframework.context.support.AbstractApplicationContext.finishRefresh(AbstractApplicationContext.java:929)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:467)
Your classpath doesn't have spring-web-x.x.x.jar, that's why the exception is. Please use Maven build tool for building your Spring MVC project. Add all the necessary dependencies in pom.xml. For spring-web 3.0.4 version, it would be
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>3.0.4.RELEASE</version>
</dependency>

Categories