李顺利
随笔-50  评论-170  文章-0  trackbacks-0

 

特殊情况(ActionFormServlet Filter Listener)下Spring如何注入对象

 

         Spring一段时间,理解了一些Spring的注入对象的原理和实例,自己也写过一些用Spring进行注入对象,特别在SSH整合中这种Spring的注入才体现的淋漓尽致(个人认为),在学习的过程中,难免有些不习惯,原先通过new实例化的对象现在要使用Spring进行bean注入,不过这也应该是Spring的统一管理的细度和好处。(个人理解Spring还不是太深,如果有高人愿意指点一下,非常愿意向您请教)

         Spring的注入过程中,有些特殊情况下市不能使用常用的方法进行注入的。最近做个小东西,就发现了一些特殊情况下不能使用平常我们注入的方法进行注入。

特殊情况一:ActionForm

思考源泉:最近在写一个程序,要用到Struts1.x中的ActionForm进行一定的验证,而验证要使用到数据库中的数据库,而我也是和平常一样使用setXXX来进行注入,但是这个时候就出现了异常,空指针异常。上网搜了很多,虽然没有具体的,但还是有点思路的。

思路有两种:

第一种:将你需要注入的参数设置为static的,然后将设值方法setXXX的返回值设置为非void型,比如你的

protected IDaoService daoService;  

改为

protected static IDaoService daoService;  

设值方法setXXX,为:

public static boolean setDaoService(IDaoService daoService) {

    LookuserForm.daoService = daoService;

    return true;

}

然后在spring配置文件中设值:

 

<bean id="BaseBoolean"

class="org.shan.student.form.LookuserForm"

factory-method="setDaoService"

depends-on="daoService">

<constructor-arg ref="daoService"></constructor-arg>

</bean>

这样就实现了daoService的注射。

 

我的实例代码如下:

//下面的是ActionForm里面注入对象的写法

private  static UsersManager um;//静态,一定

 

public static boolean setUm(UsersManager um)//void型的,建议是boolean

{

         RegisterForm.um = um;

         return true;

}

//下面是通过Spring bean的注入,其中id就随便了,class就是你的ActionForm的类,factory-method就是你的非void型注入对象的方法,depends-on就是你要注入对象使用的beanconstructor-arg ref="XXX"也是一样了。

<bean id="RegisterForm" class="com.usc.struts.form.RegisterForm" factory-method="setUm" depends-on="UserManager">

         <constructor-arg ref="UserManager"></constructor-arg>

</bean>

 

这样配置后应该是可以的。我的ActionForm进行用户验证的代码也贴给大家了

/*

 * Generated by MyEclipse Struts

 * Template path: templates/java/JavaClass.vtl

 */

package com.usc.struts.form;

 

import javax.servlet.ServletContext;

import javax.servlet.http.HttpServletRequest;

import javax.servlet.http.HttpServletResponse;

 

import org.apache.struts.action.ActionErrors;

import org.apache.struts.action.ActionForm;

import org.apache.struts.action.ActionMapping;

import org.apache.struts.action.ActionMessage;

import org.apache.struts.action.ActionServlet;

import org.springframework.context.ApplicationContext;

import org.springframework.web.context.support.WebApplicationContextUtils;

 

import com.usc.dao.User;

import com.usc.service.UsersManager;

 

/**

 * MyEclipse Struts Creation date: 06-26-2009

 *

 * XDoclet definition:

 *

 * @struts.form name="registerForm"

 */

public class RegisterForm extends ActionForm

{

    /*

     * Generated fields

     */

    private  static UsersManager um;

 

    public static boolean setUm(UsersManager um)

    {

       RegisterForm.um = um;

       return true;

    }

 

//  public UsersManager getUm()

//  {

//     return um;

//  }

 

//  @Override

//  public void setServlet(ActionServlet servlet)

//  {

//     ServletContext context = servlet.getServletContext();

//     ApplicationContext ctx = WebApplicationContextUtils

//            .getWebApplicationContext(context);

//     this.um = ((RegisterForm) ctx.getBean("RegisterForm")).getUm();

//     super.setServlet(servlet);

//  }

 

    /** password property */

    private String password;

 

    /** username property */

    private String username;

 

    /** repassword property */

    private String repassword;

 

    /*

     * Generated Methods

     */

 

    /**

     * 注册验证 Method validate

     *

     * @param mapping

     * @param request

     * @return ActionErrors

     */

    public ActionErrors validate(ActionMapping mapping,

           HttpServletRequest request)

    {

       ActionErrors errors = new ActionErrors();

       // 用户名不能为空或者空格组成

       if (null == username || "".equals(username.trim())

              || username.trim().length() == 0)

       {

           errors.add("username", new ActionMessage("username.required"));

           // System.out.println("username is required");

       } else if (username.trim().length() < 5

              || username.trim().length() > 12)

       {

           errors.add("username", new ActionMessage("username.error"));

       } else if (!um.checkUserName(username))

       {

           errors.add("username", new ActionMessage("username.error.exist"));

       }

//     else

//     {

//         errors.clear();

//      }

       if (null == password || "".equals(password.trim())

              || password.trim().length() == 0)

       {

           errors.add("password", new ActionMessage("password.required"));

       } else if (!password.trim().equals(repassword))

       {

           errors.add("repassword", new ActionMessage("repassword.error"));

       }

      

       return errors;

    }

 

    /**

     * Method reset

     *

     * @param mapping

     * @param request

     */

    public void reset(ActionMapping mapping, HttpServletRequest request)

    {

       // TODO Auto-generated method stub

    }

 

    /**

     * Returns the password.

     *

     * @return String

     */

    public String getPassword()

    {

       return password;

    }

 

    /**

     * Set the password.

     *

     * @param password

     *            The password to set

     */

    public void setPassword(String password)

    {

       this.password = password;

    }

 

    /**

     * Returns the username.

     *

     * @return String

     */

    public String getUsername()

    {

       return username;

    }

 

    /**

     * Set the username.

     *

     * @param username

     *            The username to set

     */

    public void setUsername(String username)

    {

       this.username = username;

    }

 

    /**

     * Returns the repassword.

     *

     * @return String

     */

    public String getRepassword()

    {

       return repassword;

    }

 

    /**

     * Set the repassword.

     *

     * @param repassword

     *            The repassword to set

     */

    public void setRepassword(String repassword)

    {

       this.repassword = repassword;

    }

}

 

 

第二种:重写ActionFormpublic void setServlet(ActionServlet servlet)方法。

public void setServlet(ActionServlet servlet) {

ServletContext context = servlet.getServletContext();

ApplicationContext ctx = WebApplicationContextUtils

.getWebApplicationContext(context);

LookuserFormtemp = (LookuserForm) ctx.getBean("LookuserForm");

this.daoService = temp.getDaoService ();

 

super.setServlet(servlet);

}

这样,每次产生一个LookuserForm实例,都会向spring维护的实例请求注入参数。

我的代码实例如下:

private  static UsersManager um;

 

public static boolean setUm(UsersManager um)

{

         RegisterForm.um = um;

         return true;

}

 

<bean id="RegisterForm" class="com.usc.struts.form.RegisterForm" factory-method="setUm" depends-on="UserManager">

         <constructor-arg ref="UserManager"></constructor-arg>

</bean>

 

 

综合上面的的注入方法,我个人建议使用第一种,第一种还是和我们以前注入对象的习惯有点类似了。

 

 

特殊情况二:服务器启动以后,(Servlet容器启动)创建了许多对象,如 servlet filter listenerspring等等 那么如何使用这些对象呢

思考源泉:也是在这个程序中用到的,我要使用AJAX技术进行异步验证,其中有些是要和数据库进行交互的,网上一般这种情况有两种验证思路

思路一:在Javascript中使用JQuery来创建一个XMLHttpRequest对象,再在Action中进行验证,我使用这个思路做了好久,就是没有结果,网上也给了很多类似的代码,但是感觉就没有一个能够真正的运行,没办法,考虑使用思路二了。

思路二:使用Servlet进行验证,这种思路很适合AJAX验证的思路,不过问题出现了,还是空指针异常,Spring注入的对象根本没有实例化。一开始还以为和Servlet的生命周期有点关系,后来在网上搜了一些东西,还是感觉可以通过Spring进行注入的。

下面介绍在Servlet(或者Filter,或者Listener)中使用springIOC容器默认情况下Servlet容器创建spring容器对象,注入到servletContext中,servletContext对象又是注入到session对象中,session对象又是注入到request对象中,request对象又是注入到servlet对象中,(其实不是很标准的注入,是传参数,或者对属性直接付值)。层层依赖可以得到spring容器对象。

代码如下:

ServletContext   servletContext   =   request.getSession().getServletContext();          

                   ApplicationContext   ctx   =   WebApplicationContextUtils.getWebApplicationContext(servletContext   );

                   UsersManager   um   =   (UsersManager)ctx.getBean( "UserManager");

 

这个时候的验证我在Mozilla Foxfire Google Chrome中都通过验证了,但是在遨游里没有通过,我不知道是什么原因,望高手指点,谢谢。

 

上面是我自己写一个程序中遇到到一些小问题,这应该对理解Spring的依赖注入有很大的好处,通过这个程序,至少让我们知道了差不多Spring注入对象的一些情况,以后有什么问题了,就非常容易解决。

 

这里面的代码都可以给大家了,大家如果需要的话,请下载

http://usc.googlecode.com/files/UsersManagerWithAJAX.rar),

本博文原word文档下载地址请移步CSDN

http://download.csdn.net/source/1540536

 

如果有高人指点一下或者相互交流学习的,请联系QQ:506817493,谢谢。

 

 

木子写于200982



博客中的一些下载已经放到了百度云了,请根据需要下载。【点我去百度云下载】

最后弱弱地说一下,如果可以的话,转载请提供出处( ),谢谢。
posted on 2010-01-07 21:11 李顺利 阅读(3227) 评论(2)  编辑  收藏

评论:
# re: 特殊情况(ActionForm,Servlet, Filter, Listener)下 Spring如何注入对象 2011-05-24 16:47 | liangwu
thx!   回复  更多评论
  
# re: 特殊情况(ActionForm,Servlet, Filter, Listener)下 Spring如何注入对象 2011-05-24 18:08 | liangwu
讲得很好很深刻,谢谢您,大赞技术贴。
例如:用javax.servlet.Filter过滤时有一些不需要过滤的属性(如网站登陆的url不需要而后台页面url需要),如果在web.xml里面用init-param来指定会很麻烦,且直接用spring给filter注入因为容器产生的先后关系会使属性为空。而用这种“层层依赖”的原理可以在filter的init方法里获得系统context,context得到spring配置的bean可以注入List或是Map或是其他Bean等复杂数据类型,再传递这个filter,对于servlet功能的帮助极大又可以解耦合,小小的技巧但我觉得很实用!  回复  更多评论
  

只有注册用户登录后才能发表评论。


网站导航: