A few days ago a colleague ran into a problem: normally according to Spring's official configuration, when struts2 and spring are integrated, the class attribute in the struts configuration file points to the Bean ID of the spring configuration, but when class points to the Classpath, can still inject service.
public class Loginaction extends actionsupport{
Private Loginservice Loginservice;
public void Setloginservice (Loginservice loginservice) {
System.out.println ("Init Service ...");
This.loginservice = Loginservice;
}
Spring Configuration
<bean id= "Loginservice" class= "Org.xxxxx.services.impl.LoginServiceImpl" ></bean>
<bean id= "loginaction" class= "Org.xxxxx.action.LoginAction" >
</bean>
Struts Configuration
<action name= "Login" class= "Org.xxxxx.action.LoginAction" >
<result name= "Success" >/result.jsp</result>
<result name= "Error" >/login.jsp</result>
</action>
1. Take note of the above two red line sections, in Struts.xml, the class specified in the action specifies the full classpath name in this way, regardless of the <bean id= "loginaction" class= "in the Spring configuration file Org.xxxxx.action.LoginAction "></bean> no configuration <property name=" Loginservice "ref=" Loginservice "/> , as long as there is <bean id= "Loginservice" .../> exists, and the name of the ID is consistent with the name of the member Bean in action, when the action class is instantiated, the Loginservice instance is injected together.
And there is also a problem when Struts.xml is configured in this way, if this action instance is configured in spring, and the scope= "prototype" multiple properties are added, the access times are wrong. It is possible that STRUTS2 itself is a conflict between multiple cases and the spring instantiation mechanism. So when configured in this way, the action class configuration in spring can be discarded.
2. If <action name= "login" class= "Loginaction" > The class here specifies the Bean ID in the spring configuration file, the Loginservice auto-injection problem does not occur, but is based on <bean id= "loginaction" class= "org.xxxxx.action.LoginAction" ></bean> no configuration <property name= specified Loginservice "ref=" Loginservice "/> To decide, there is <property name=" Loginservice "ref=" Loginservice "/> The designation, When the action class is instantiated, the Loginservice instance is injected together, and the Property,loginservice is empty without configuration
So there are two kinds of struts and spring configuration scenarios:
(1) configuration scheme one:
When configuring an action, you need to keep the class attribute consistent with the ID of the corresponding action bean in the spring configuration file, and the system can assemble and manage the action through spring.
If the action contains the object of the service layer:
Private Studentinfoserviceimpl Studentinfoservice;
You need to add a set method before you can use Spring Dependency injection:
public void Setstudentinfoservice (Studentinfoserviceimpl studentinfoservice) {
This.studentinfoservice = Studentinfoservice;
}
If the action is configured as follows in Struts.xml:
<action name= "Studentregister" class= "Studentregisteraction" >
<result name= "Result" >/web-inf/exam/result.jsp
</result>
<result name= "Input" >/web-inf/exam/error.jsp
</result>
<interceptor-ref name= "Excludeparamsstack"/>
</action>
The action in the Applicationcontext.xml file is configured as follows:
<bean id= "studentregisteraction" class= "Com.exam.actions.StudentRegisterAction" scope= "prototype" ><!-- Specify multiple cases-
<property name= "Studentinfoservice" >
<ref bean= "Studentinfoservice"/>
</property>
</bean>
STRUTS2 and Spring integration Scenario two:
The same as the previous two steps and programme one;
When you configure the Struts.xml file, the action class is the classpath of the action, and you do not need to add the bean configuration of the action in the Applicationcontext.xml configuration file. Thus, when we use the action class, because Studentinfoservice has already configured the relevant bean, it is automatically assembled, and the action is still instantiated by spring.
Configuration of the Studentinfoservice:
<bean id= "Studentinfoservice" class= "Com.exam.service.StudentInfoServiceImpl" >
<constructor-arg>
<ref bean= "Studentinfodao"/>
</constructor-arg>
</bean>
Focus Studentinfoservice the same as the properties in action:
The action is configured in Struts.xml as follows:
<action name= "Studentregister" class= "com.exam.actions.StudentRegisterAction" ><!--struts2 default multi-
<result name= "Result" >/web-inf/exam/result.jsp
</result>
<result name= "Input" >/web-inf/exam/error.jsp
</result>
<interceptor-ref name= "Excludeparamsstack"/>
</action>