3

I'm learning Struts 2 framework (just for fun :)) and I would like to hook up my web app to a MongoDB back-end using the Spring Data framework. Now usually I just create a Repository that extends MongoRepository, and I then Autowire it into my application and that is it.

However, doing it this time around doesn't work too well. I keep getting NullPointerExceptions which means my Repository is not autowired. Does anyone have an idea what is going wrong?

EDIT: I created a simple GitHub example explaining the idea https://github.com/jseminck/starter-kits/tree/master/struts2.spring

In my pom.xml I added Struts-Spring-Plugin:

<dependency>
    <groupId>org.apache.struts</groupId>
    <artifactId>struts2-spring-plugin</artifactId>
    <version>2.3.4.1</version>
</dependency>

I added applicationContext.xml and referred to it from my web.xml

Web.xml

<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>classpath:applicationContext.xml</param-value>
</context-param>
<listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>

applicationContext.xml

<context:component-scan base-package="com.jseminck.hello.world" />

<!-- Register Mongo Instance -->
<mongo:mongo id="mongo" host="localhost" port="27017" />
<!--  for defining mongo template -->

<bean id="mongoTemplate" class="org.springframework.data.mongodb.core.MongoTemplate">
    <constructor-arg ref="mongo" />
    <constructor-arg name="databaseName" value="nortal" />
</bean>

<!-- For defining mongo repository -->
<mongo:repositories base-package="com.jseminck.hello.world.repository" />

HelloWorldRepository

@Repository
public interface HelloWorldRepository extends MongoRepository<HelloWorld, String> {

}

Am I missing anything to get this working? I get a NullPointerException when calling a method on the Bean that is supposed to get instantiated.

java.lang.NullPointerException
    com.jseminck.struts2.HelloAction.execute(HelloAction.java:20)
    sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    java.lang.reflect.Method.invoke(Method.java:606)
    com.opensymphony.xwork2.DefaultActionInvocation.invokeAction(DefaultActionInvocation.java:453)
    com.opensymphony.xwork2.DefaultActionInvocation.invokeActionOnly(DefaultActionInvocation.java:292)
    com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:255)
    org.apache.struts2.interceptor.debugging.DebuggingInterceptor.intercept(DebuggingInterceptor.java:256)
    com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:249)
    com.opensymphony.xwork2.interceptor.DefaultWorkflowInterceptor.doIntercept(DefaultWorkflowInterceptor.java:176)
    com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:98)
    com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:249)
    com.opensymphony.xwork2.validator.ValidationInterceptor.doIntercept(ValidationInterceptor.java:265)
    org.apache.struts2.interceptor.validation.AnnotationValidationInterceptor.doIntercept(AnnotationValidationInterceptor.java:68)
    com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:98)
    com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:249)
    com.opensymphony.xwork2.interceptor.ConversionErrorInterceptor.intercept(ConversionErrorInterceptor.java:138)
    com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:249)
    com.opensymphony.xwork2.interceptor.ParametersInterceptor.doIntercept(ParametersInterceptor.java:236)
    com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:98)
    com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:249)
    com.opensymphony.xwork2.interceptor.ParametersInterceptor.doIntercept(ParametersInterceptor.java:236)
    com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:98)
    com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:249)
    com.opensymphony.xwork2.interceptor.StaticParametersInterceptor.intercept(StaticParametersInterceptor.java:190)
    com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:249)
    org.apache.struts2.interceptor.MultiselectInterceptor.intercept(MultiselectInterceptor.java:75)
    com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:249)
    org.apache.struts2.interceptor.CheckboxInterceptor.intercept(CheckboxInterceptor.java:90)
    com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:249)
    org.apache.struts2.interceptor.FileUploadInterceptor.intercept(FileUploadInterceptor.java:243)
    com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:249)
    com.opensymphony.xwork2.interceptor.ModelDrivenInterceptor.intercept(ModelDrivenInterceptor.java:100)
    com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:249)
    com.opensymphony.xwork2.interceptor.ScopedModelDrivenInterceptor.intercept(ScopedModelDrivenInterceptor.java:141)
    com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:249)
    com.opensymphony.xwork2.interceptor.ChainingInterceptor.intercept(ChainingInterceptor.java:145)
    com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:249)
    com.opensymphony.xwork2.interceptor.PrepareInterceptor.doIntercept(PrepareInterceptor.java:171)
    com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:98)
    com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:249)
    com.opensymphony.xwork2.interceptor.I18nInterceptor.intercept(I18nInterceptor.java:176)
    com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:249)
    org.apache.struts2.interceptor.ServletConfigInterceptor.intercept(ServletConfigInterceptor.java:164)
    com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:249)
    com.opensymphony.xwork2.interceptor.AliasInterceptor.intercept(AliasInterceptor.java:192)
    com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:249)
    com.opensymphony.xwork2.interceptor.ExceptionMappingInterceptor.intercept(ExceptionMappingInterceptor.java:187)
    com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:249)
    org.apache.struts2.impl.StrutsActionProxy.execute(StrutsActionProxy.java:54)
    org.apache.struts2.dispatcher.Dispatcher.serviceAction(Dispatcher.java:511)
    org.apache.struts2.dispatcher.ng.ExecuteOperations.executeAction(ExecuteOperations.java:77)
    org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter.doFilter(StrutsPrepareAndExecuteFilter.java:91)

Thanks, Joachim

Joachim Seminck
  • 721
  • 1
  • 8
  • 10
  • Are you trying to autowire an action or some other bean? – Roman C Jan 30 '15 at 15:35
  • Autowire some other bean (in this case: a MongoRepository - but I cannot get any bean to work that is defined in my applicationContext). – Joachim Seminck Jan 30 '15 at 15:43
  • What do you mean to work? – Roman C Jan 30 '15 at 15:45
  • I keep getting a nullpointerexception when I call a method on the bean which has the @Autowire annotation. So it was not getting instantiated./ injected. – Joachim Seminck Jan 30 '15 at 15:53
  • Could you post a stacktrace? – Roman C Jan 30 '15 at 15:55
  • java.lang.NullPointerException com.jseminck.struts2.HelloAction.execute(HelloAction.java:20) sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57) sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) java.lang.reflect.Method.invoke(Method.java:606) – Joachim Seminck Jan 30 '15 at 17:50
  • Edit a question and add details that is relevant in the body of the post. – Roman C Jan 30 '15 at 19:01
  • I edited my post and added a link to a github repository which contains a super simple basic example: 2 classes (one service, one action) and the struts+spring configuration files (web.xml applicationcontext.xml and struts.xml). Also added full stacktrace though I don't see how it is relevant. – Joachim Seminck Jan 30 '15 at 20:01

1 Answers1

0

You forgot to instruct Struts2 about using Spring as ObjectFactory:

Put the following constant in your struts.xml configuration:

<constant name="struts.objectFactory" value="spring" />

<package ...>

More info on this answer, though I guess you already know them because all the rest of your configuration seems fine.

Community
  • 1
  • 1
Andrea Ligios
  • 49,480
  • 26
  • 114
  • 243
  • Yeah, I had this option already (but my value org.apache.struts2.spring.StrutsSpringObjectFactory). I'm sure my beans from my applicationContext are being recognized (when I give a wrong package/class name I get a ClassNotFoundError). I just cannot manage to @Autowire them in my application. – Joachim Seminck Jan 30 '15 at 15:24
  • You don't know what are you answering. – Roman C Jan 30 '15 at 15:39
  • 1
    You don't know that *you are* answering. BTW isn't he trying to autowire a bean defined in applicationContext into an Action (or an Interceptor) ? – Andrea Ligios Jan 30 '15 at 15:47
  • How could you know that? – Roman C Jan 30 '15 at 15:51
  • 1
    Intelligent Guess, like jQuery. – Andrea Ligios Jan 30 '15 at 15:53
  • 2
    Yes. I am trying to autowire the HelloWorldRepository into my HelloAction. – Joachim Seminck Jan 30 '15 at 15:54
  • 1
    Please post your FULL applicationContext.xml and your FULL web.xml – Andrea Ligios Jan 30 '15 at 15:54
  • I'll do it when I get home. Currently travelling. Thanks in advance for the help, much appreciated. – Joachim Seminck Jan 30 '15 at 15:58
  • Hi - I created a very simple example and posted it on GitHub. It has 2 classes: HelloAction and TestService. The idea is to get the TestService class injected into the HelloAction class to be able to use it's methods there. Then there's the web / struts and applicationConfig XML files. This is the easiest example I could think of and even that I can't get to work... :( https://github.com/jseminck/starter-kits/tree/master/struts2.spring ) – Joachim Seminck Jan 30 '15 at 17:49
  • You've moved it... I'm watching it now, assuming it is "Struts Hello World". Did it work in the end ? If yes, feel free to post your answer here and accept it to help future visitors – Andrea Ligios Feb 02 '15 at 13:19