First Spring4 MVC Attempt not loading with Tomcat 7 - java

Im fairly new to spring mvc and Im trying to launch a extremely simple project that will just display a greeting on a blank page but for some reason I cannot seem to get it to run on the tomcat server, I have gone over every little piece of code and I do not believe there are any mistakes as well as made sure that the tomcat server is configured correctly, any help anyone can give me would be appreciated (This isn't any kind of assignment for anything , this is purely for me trying to learn Spring MVC and learning the proper configuration, because after researching for hours I cannot find anything wrong with it)
I also did this purely in java, no xml. Did not use any dependency managers either ( No Maven or Gradle ) but I do have all necessary jars
I first start by configuring the dispatcher servlet and pass it the webConfig so that the application context can load beans via component scan, since this is a simple project I have no RootConfig class
In my WebConfig I enable Spring MVC ,I give ComponentScan the base package that will search for all Controller classes to create beans for the application context and I create a simple view resolver that will wrap view names with the specified prefix and suffix
My Controller again is really simple I annotate the class as the default handler for "/" requests and map the sayHello method to handle requests for "/home" where I attach a simple message to the model(map) and return the view name
And finally my view which displays the message, The tags in the header of the jsp I know are useless since I'm not using any tags in the form they are just something left over from a previous attempt
When I run this application on the tomcat server I get the following, Im running Tomcat 7 which does support spring mvc java configuration
The tomcat server is running properly
Project is in the webapps directory of tomcat server
And to the best of my knowledge the server configurations are correct
Console output
- Solution -
As mentioned below in the comments the warning I got was
SpringProjLibs is a user library I created and placed all my spring jars inside
This comment along with some research on this warning led me to realize I had failed to add the library to the deployment assembly like below (the entry highlighted in blue was not there , I added that) after adding the line in blue it solved that problem
IMPORTANT
Im adding a follow up to this post since this still pertains to my original question. After completing the above step it resolved THAT issue but another issue presented itself.
I got the following error and tomcat failed to launch
Jun 18, 2018 5:57:37 PM
org.springframework.web.servlet.DispatcherServlet initServletBean
SEVERE: Context initialization failed
org.springframework.beans.factory.BeanCreationException: Error
creating bean with name 'requestMappingHandlerAdapter' defined in
class
org.springframework.web.servlet.config.annotation.DelegatingWebMvcConfiguration:
Instantiation of bean failed; nested exception is
org.springframework.beans.factory.BeanDefinitionStoreException:
Factory method [public
org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter
org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport.requestMappingHandlerAdapter()]
threw exception; nested exception is
org.springframework.beans.factory.BeanCreationException: Error
creating bean with name 'mvcValidator' defined in class
org.springframework.web.servlet.config.annotation.DelegatingWebMvcConfiguration:
Invocation of init method failed; nested exception is
java.lang.NoClassDefFoundError: com/fasterxml/classmate/Filter
After doing some research I discovered that contrary to what ive studied and seen in online material the annotations #EnableWebMvc and #ComponentScan together in the WebConfig class were causing this issue. I resolved this issue by moving the #EnableWebMvc annotation to the Initializer class , now my classes look like the following
This resolved all my issues and my project launched successfully

Related

How do I properly restart a Spring boot application?

I'm using the code from here to restart my Spring Boot application:
Thread restartThread = new Thread(() -> restartEndpoint.restart());
restartThread.setDaemon(false);
restartThread.start();
However, the restarted application fails to create beans, ultimately due to a RejectedExecutionException thrown when trying to schedule #Scheduled-annotated methods:
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'myBean' defined in URL [insert-path-here]: Initializetion of bean failed; nested exception is org.springframework.core.TaskRejectedException: Executor [java.util.concurrent.ScheduledThreadPoolExecutor#284a4a89[Terminated, pool size = 0, active threads = 0, queued tasks = 0, completed tasks = 21508]] did not accept task: my.package.MyBean.scheduledMethod
The logs immediately after the doRestart() invocation indicate that the ExecutorService taskScheduleris being shut down, which would explain why this is happening. What I'm not entirely sure of is why the ExecutorService isn't recreated. Embarrassingly enough, it turns out that taskScheduler was a bean defined by my application and was not annotated #RefreshScope. Now the error I'm getting is
org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'anotherBean' definen in URL[...]: Unsatisfied dependency expressed through constructor parameter 0 ... nested exception is java.lang.IllegalStateException: org.springframework.boot.servlet.context.AnnotationConfigServletWebServerApplicationContext#2a95076c has been closed already
Scrolling up a bit in the logs reveals that there's an ApplicationContextException thrown with the stacktrace containing doRestart:
org.springframework.ApplicationContextException: Unable to start web server; nested exception is java.lang.IllegalStateException: Filters cannot be added to context [/myapp] as the context has been initialized
at org.springframework.context.ServletWebServerApplicationContext.onRefresh(ServletWebServerApplicationContext.java:157)
...
at org.springframework.boot.SpringApplication.run(SpringApplication.java:316)
at org.springframework.cloud.context.restart.RestartEndpoint(RestartEndpoint.java:160)
at my.package.MyBean.lambda$doThingAndRestart$2 (MyBean.java: 118)
Googling this error seems to result in a single page that isn't relevant, although I do note that this application is running on tomcat as well.
I suspect that this is related to the application creating a wrong sort of application context, which is a AnnotationConfigServletWebServerApplicationContext, which doesn't extend AbstractRefreshableAplicationContext.
What do I need to do to get rid of this error?
Please bear with me when I only copy single lines from the stack trace. I'm on a two-computer set-up, so I can't copy-and paste and am no allowed to otherwise move the logs to a computer connected to the Internet.
It turns out that RestartEndpoint only works with Spring's internal Tomcat, not an external one. So, another solution is required. The ones I discovered are
Using Tomcat's WatchedResource to re-deploy the server when a configuration file is overwritten. This has the downside of needing to maintain the Tomcat configuration when maintaining the doThingAndRestart functionality.
Using Tomcat's Manager App. I noticed that the reload request blocks until the app is done reloading, which overflowed the heap space.
Using Spring's RefreshEndpoint. For whatever reason, this didn't refresh the datasources.

Glassfish replaces managed bean with fully qualified name with just class name

I'm currently adding functionality to a web platform written in Java and using GlassFish 3.1 as the server. This is a highly complex platform in terms of maintenance and my efforts with googling and reading through documentation were fruitless.
When deploying I get the following warning:
WARNING: JSF1074: Managed bean named 'SomeClass' has already been registered.
Replacing existing managed bean class type com.example.package.SomeClass with SomeClass.
That leads to the exception below when the web page is opened. My guess is this happens because the managed bean was registered without the package name.
java.lang.NoClassDefFoundError: Could not initialize class SomeClass
The bean is annotated the following way
#ManagedBean(name="SomeClass")
#SessionScoped
public class SomeClass { ... }
If I undeploy, restart Glassfish and deploy again, sometimes the fully qualified managed bean is registered and the problem (temporarily) solved. This just adds to my confusion.
So, my question is: what could be causing GlassFish to replace a managed bean with the fully qualified name with another using just the class name?

Web service client: org.jboss.ws.core.jaxws.spi.ProviderImpl cannot be cast to javax.xml.ws.spi.Provider

we're having a pretty baffling issue here and I wondered if anyone else might know the solution. On Wednesday, a Spring project containing a web service client deployed onto our JBoss application server without incident, and then got invoked without incident as well.
This morning, we tried to deploy a more recent version of the same project, and received this error:
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'customerServicePort' defined in ServletContext resource [/WEB-INF/applicationContext.xml]: Invocation of init method failed; nested exception is javax.xml.ws.WebServiceException: Unable to create Provider: org.jboss.ws.core.jaxws.spi.ProviderImpl cannot be cast to javax.xml.ws.spi.Provider
My first thought was that there was some kind of change in the server, since I've seen that error before and it had to do with jboss's compatibility or lack thereof with something in java6 I believe. But when we revert to Wednesday's revision in svn, it deploys and runs successfully again. But when we look at the version history, we don't see any changes that could do something like this. There are only two classes that have changed at all that reference the client, and every reference to the client is just how it was before. The application-context.xml and build paths are identical. Has anyone seen anything like this before?

Integrating EJB3 with Spring

I'm building application with EJB and Spring 3. I have three maven modules - Spring jars, EJB jar and web part. In the web part I want to call my EJB session bean. Here comes the code of it:
#Controller
public class IndexController {
#EJB
PaymentRemote paymentRemote;
}
I have also an application context file, with the content:
<jee:local-slsb id="paymentRemote" jndi-name="ejb/myBean"
business-interface="net.learntechnology.ejb.PaymentRemote"/>
and
<bean class="org.springframework.context.annotation.CommonAnnotationBeanPostProcessor">
<property name="alwaysUseJndiLookup" value="true"/>
In my ejb module I have an interface:
#Local
public interface PaymentRemote {
}
Unfortunately when I deploy it on JBoss as 5 i get the following error:
Error creating bean with name 'paymentRemote': Invocation of init method failed; nested exception is javax.naming.NameNotFoundException: ejb not bound.
I saw a lot of examples in net and all are configured like this. I'm stuck with it...
Could any one help me with this? I would be more than grateful!
For JBOSS the proper pattern is:
application-name/bean/remote or local
Since my comment turned out to be the answer, I'll rephrase it....
Make sure that the EJB is actually bound to JNDI under the name you're expecting. The error message suggests that it's not.
In JBoss, to the JMX Console, look for the JNDIView object, and invoke its list method. If your EJB is present on ejb/myBean, it should appear here. If not, look for its under a different name, and bind to that.

Weblogic 10.3.1 gives 'Dependency injection failure' warning when publishing code

Weblogic 10.3.1 gives me a "Dependency injection failure" when I publish my code; the publish itself succeeds. I am working via Eclipse.
Basically I publish an ear with a web service aaa.MyWebServicePort that has a bean bbb.MyBean declared as local variable with the ejb 3.0 #EJB annotation. bbb.MyBean is also in the ear as well as a client project with the interfaces defined.
After publishing the web service works on the appserver and calls bbb.MyBean. So why the warning?
Details of the warning in the console window of Eclipse: Error creating bean with name 'aaa.MyWebServicePort': Initialization of bean failed; nested exception is [...] BeanCreationException: Dependency injection failure: can't find the bean definition about class interface bbb.MyBean; nested exception is [...] NoSuchBeanDefinitionException: No unique bean of type bbb.MyBean is defined: No beans of type bbb.MyBean;
There may be a couple of issues at work here. I'm getting a very similar error in just a straight servlet that is trying to load an ejb that isn't packaged in the same ear, I'm still trying to track that down. I also know that there is a difficulty when loading EJB v2.x that is supposed to be fixed in Weblogic 10.3.3.
If you find anything please let me know, and I'll do the same. One thing I have noticed is that if I load the EJB via initialContext.lookup, it works fine.
YMMV

Categories