Reputation: 1575
I an using struts2 and hibernate in my application. I have added ContextLoaderListener to web.xml
.
In ApplicationContext.xml
, I have 2 beans like this:
<bean id="TestImpl"
class="Service.Impl.TestImpl">
</bean>
<bean id="testaction"
class="Action.TestAction">
<property name="TestIml" ref="TestImpl"/>
</bean>
In TestAction class I have an object and a method of this object like this:
private TestImpl test1;
String m = test1.testService();
It has get and set also. when I run this project, when it comes to
String m = test1.testService()
nullPointerException appears. I am not sure all configuration done correctly or something missed. please help.
Upvotes: 2
Views: 476
Reputation: 4732
Since you are already using Struts2 + Spring, Why not use their Plugin? which can be found here
By using the plugin, you get tighter integration into Struts 2. This gives you the ability to inject your beans into internal framework objects, not just into your own beans.
For instance, with the plugin Struts 2 will inspect all of your actions, when it creates them to service a request, and inject them. By default Struts2-spring plugin uses autowire by name. so for example in your action class you have this code.
public class LoginAction extends UserAction{
public String execute() {
return "success";
}
public AuthenticateLoginService getAuthenticateLoginService() {
return authenticateLoginService;
}
public void setAuthenticateLoginService(
AuthenticateLoginService authenticateLoginService) {
this.authenticateLoginService = authenticateLoginService;
}
private AuthenticateLoginService authenticateLoginService;
}
And In your applicationContext.xml you have this configuration.
<bean id="authenticateLoginService" class="services.AuthenticateLoginService"
scope="singleton">
<... some properties here
</bean>
All of the Action Classes that has a instance variable named "authenticateLoginService", spring will then inject the authenticateLoginService
bean to all classes that has an instance variable named authenticateLoginService
, providing that it has a setters and getters
Upvotes: 1
Reputation: 6158
your test1
is null
thatś why it is throwing null pointer exception
on this String m = test1.testService()
line.
I am not sure, but you can try this.
property name="test1"
instead of property name="TestIml"
Upvotes: 0