strtus the injection action spring bean


Action injected Spring managed Bean

Action is a ProductAction, the page request process, wherein the save () method used to ProductService business layer objects, the object management Spring, the problems related to the bean injected Spring Action management.

Remember introducing a jar spring integrated structs2 struts2-spring-plugin-2.3.31.jar
Described below are two ways injection

package com.jxust.ssh.action;
..
public class ProductAction extends ActionSupport implements ModelDriven<Product> {
    .
    .
    private ProductService produceService;

    public void setProduceService(ProductService produceService) {
        this.produceService = produceService;
    }

    public String save(){
        //这里需要用到ProductService对象
        produceService.save(product);
        return "index";
    }
}
  
  
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17

An injection method (Action by the management Structs)

struts.xmlFile, the Action class that the Action class path, without the need to add the bean configuration ProductAction Spring configuration file. When we use the Action class, as produceServicehave configured the relevant bean, it will automatically assemble.

structs2 configuration

Action structs2 configuration file

struts.xml

<action name="product_*" class="com.jxust.ssh.action.ProductAction" method="{1}">
            <result name="index">index.jsp</result>
        </action > 
  
  
  • 1
  • 2
  • 3

Use the full path name of the classclass="com.jxust.ssh.action.ProductAction"

spring configuration

spring bean configuration files

applicationContext.xml

    <!-- 配置业务层的类 -->
    <bean id="produceService" class="com.jxust.ssh.service.ProductService">
        ...
    </bean>
  
  
  • 1
  • 2
  • 3
  • 4

id value of the bean must and Action in the same variable name

private ProductService produceService;
//两者的名称要一一对应
<bean id="produceService".....
  
  
  • 1
  • 2
  • 3

Two injection method (Action managed by Spring)

struts.xmlWhen configuring ProductAction, Id's class name of the bean and ProductAction Spring configuration file corresponding to the necessary names consistent , the system can be assembled and managed by Spring Action.

This is the recommended way, easy to use AOP management

structs2 configuration

Action structs2 configuration file

struts.xml

    <action name="product_*" class="productActionBean" method="{1}">
            <result name="index">index.jsp</result>
        </action > 
  
  
  • 1
  • 2
  • 3

The path to the name class spring configuration of the beanclass="productActionBean"

struts.xml in the class action should spring in the bean id written correspondence. Only this spring container will automatically inject the papermanager

spring configuration

spring bean configuration files

applicationContext.xml

    <!-- 配置Action 的类 -->
    <bean id="productActionBean" class="com.jxust.ssh.action.ProductAction" scope="prototype">
        <property name="produceService" ref="productServiceBean"></property>
    </bean>

    <!-- 配置业务层的类 -->
    <bean id="productServiceBean" class="com.jxust.ssh.service.ProductService">
        ..
    </bean>
  
  
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9

Manually configure the injection ProductAction ProductService object, named as bean name productActionBean
Spring bean in default of a single embodiment, should be changed scope = "prototype" more cases

reference

sun_zhicheng's blog http://blog.csdn.net/sun_zhicheng/article/details/24232129

Guess you like

Origin blog.csdn.net/qq_38949960/article/details/86301045