8 -- 深入使用Spring -- 7...4 使用自动装配

      8.7.4 使用自动装配

        在自动装配策略下,Action还是由Spring插件创建,Spring 插件在创建Action实例时,利用Spring的自动装配策略,将对应的业务逻辑组件注入Action实例中。这种整合策略的配置文件简单,但控制器和业务逻辑组件耦合又提升到了代码层次,耦合较高。

        如果不指定自动装配,则系统默认使用按byName自动装配。前面的整合策略并没有指定任何自动装配策略。

        所谓自动装配,即让Spring自动管理Bean与Bean之间的依赖关系,无须使用ref显示指定依赖Bean。Spring容器会自动检查XML配置文件的内容,为主调Bean注入依赖Bean。自动装配可以减少配置文件的工作量,但会降低依赖关系的透明性和清晰性。

        通过使用自动装配,可以让Spring插件自动将业务逻辑组件注入Struts 2 的Action实例中。

        通过设置struts.objectFactory.spring.autoWire常量可以改变Spring插件的自动装配策略,该常量可以接受如下几个值:

          ⊙ name : 使用byName自动装配。

          ⊙ type : 使用byType自动装配。

          ⊙ auto : Spring插件会自动检测需要使用那种自动装配方式。

          ⊙ constructor : 与type类似,区别是constructor使用构造器来构造注入所需的参数,而不是使用设值注入方式。

        如果使用按byName来完成自动装配,则无须设置任何Struts 2 常量。

        在这种整合策略下,还采用传统的方式来配置Struts 2 的Action,配置Action时一样指定具体的实现类。

        因为使用了自动装配,Spring插件创建Action实例时,是根据配置Action的class 属性指定实现类来创建Action实例的。

        XML : Struts 2

<?xml version="1.0" encoding="UTF-8" ?>
<!-- 指定Struts 2 配置文件的DTD信息 -->
<!DOCTYPE struts PUBLIC
        "-//Apache Software Foundation//DTD Struts Configuration 2.5//EN"
        "http://struts.apache.org/dtds/struts-2.5.dtd">

<!-- Struts 2 配置文件的根元素 -->
<struts>
    <!-- 配置了一系列常量 -->
    <constant name="struts.i18n.encoding" value="UTF-8"/>
    <constant name="struts.devMode" value="true"/>
    <constant name="struts.enable.DynamicMethodInvocation" value="false"/>

    <package name="edu.pri.lime.account.act" extends="struts-default">
    <!-- 定义处理用户请求的Action,该Action的class属性不是实际处理类,而是Spring容器中的Bean实例的ID -->
        <action name="login" class="edu.pri.lime.account.act.LoginAction">
            <!-- 为两个逻辑视图配置页面 -->
            <result name="error">/WEB-INF/content/error.jsp</result>
            <result name="success">/WEB-INF/content/welcome.jsp</result>
        </action>
        <!-- 让用户直接访问该应用时列出所有的视图页面 -->
        <action name="*">
            <result>/WEB-INF/content/{1}.jsp</result>
        </action>
    </package>
</struts>

        Class : LoginAction

package edu.pri.lime.account.act;

import com.opensymphony.xwork2.Action;
import com.opensymphony.xwork2.ActionSupport;

import edu.pri.lime.account.service.MyService;

public class LoginAction extends ActionSupport{

    /**
     * 
     */
    private static final long serialVersionUID = 1L;
    
//    封装用户请求参数的两个成员变量
    private String username;
    private String password;
    
//    系统所用的业务逻辑组件
    private MyService ms;
//    设置注入业务逻辑组件所必需的setter方法
    public void setMs(MyService ms){
        this.ms = ms;
    }
//    处理用户请求的execute方法
    public String execute() throws Exception{
//        调用业务逻辑组件的validLogin()方法
//        验证用户名和密码
        if(ms.validLogin(getUsername(),getPassword()) > 0){
            addActionMessage("Congratulation!,Integer Succeed !");
            return Action.SUCCESS;
        }
        return Action.ERROR;
    }
    public String getUsername() {
        return username;
    }
    public void setUsername(String username) {
        this.username = username;
    }
    public String getPassword() {
        return password;
    }
    public void setPassword(String password) {
        this.password = password;
    }
    

}

        XML : applicationContext

<?xml version="1.0" encoding="UTF-8"?>
<!-- Spring 配置文件的根元素,使用Spring-beans-4.0.xsd语义约束 -->
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:util="http://www.springframework.org/schema/util"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
http://www.springframework.org/schema/util
http://www.springframework.org/schema/util/spring-util-4.0.xsd">

    <!-- 部署一个业务逻辑组件 -->
    <bean id="ms" class="edu.pri.lime.account.service.impl.MyServiceImpl"/>

</beans>

        Action所需的业务逻辑组件的id必须为ms,因此配置业务逻辑组件时,必须指定其id属性为ms。

        在这种整合策略下,Spring插件负责为Action自动装配业务逻辑组件,从而可以简化配置文件的配置。

        这种方式也存在如下两个缺点:

          ⊙ Action与业务逻辑组件的耦合降低到代码层次,必须在配置文件中配置与Action所需控制器同名的业务逻辑组件。这不利于高层次解耦。

          ⊙ Action 接收Spring容器的自动装配,代码的可读性较差。

啦啦啦

啦啦啦

啦啦啦

原文地址:https://www.cnblogs.com/ClassNotFoundException/p/6663481.html