SSH框架中关于Spring自动为Struts2的Action装配参数的误解

在SSH框架的项目中,Action一般由Spring创建,并且Spring会使用自动装配的策略,将对应的组件注入,如果这里没有指定自动装配的类型 (autowire=”byName”),则使用byName的自动装配。
问题在于这里的自动装配实际上是调用set方法来实现的,之前看到这段代码:

public class MgrBaseAction extends ActionSupport
{
    protected MgrManager mgr;

    public void setMgrManager(MgrManager mgr)
    {
        this.mgr = mgr;
    }
}

在Spring的配置中并没有找到mgr为id的bean配置,但是后来发现这里使用的是setMgrManager方法,也就是说,只要存在mgrManager为id的bean就能注入到mgr的变量。

说到底,理解Spring通过set方法利用反射机制为变量赋值这一点很重要。

猜你喜欢

转载自blog.csdn.net/u013146766/article/details/78584087
今日推荐