how to configure the application context to get the spring bean in struts2 -
i have maven struts2 spring hibernate integration project. imported eclipse , run correctly. when reconfigured project step step adding predefined dependency project. can't spring bean object application context file. throw null pointer exception. how configure spring object.
note : don't foget add together listener in web.xml
<listener> <listener-class> org.springframework.web.context.contextloaderlistener </listener-class> </listener>
alternative 1 : utilize spring's webapplicationcontext
in action in execute method
import org.springframework.web.context.webapplicationcontext; import org.springframework.web.context.support.webapplicationcontextutils; public class youraction extends actionsupport{ public string execute() throws exception { webapplicationcontext context = webapplicationcontextutils.getrequiredwebapplicationcontext(servletactioncontext.getservletcontext()); userbo userbo1 = (userbo)context.getbean("userbo"); } }
alternative 2 : can specify di in applicationcontext.xml
<bean id="userbo" class="yourpackagename.userboimpl" /> <bean id="userspringaction" class="yourpackagename.userspringaction"> <property name="userbo" ref="userbo" /> </bean>
in struts.xml
//struts.xml <action name="userspringaction" class="userspringaction" > <result name="success">pages/user.jsp</result> </action>
in action
public class youraction extends actionsupport{ //di via spring userbo userbo; }
alternative 3 : can utilize struts naive dependency injection
step 1 : in struts.xml
<struts> <bean class="yourpackagename.userboimpl" name="userbo" /> <package name="mydefault" namespace="/" extends="struts-default"> .... </package> </struts>
step 2: in action
public class youraction extends actionsupport{ // struts di @inject("userbo") private userbo userbo; }
struts2
No comments:
Post a Comment