论坛首页 Java企业应用论坛

『讨论』运用Spring做项目1个月有余,过程中遇到一些问题,...

浏览 8534 次
精华帖 (0) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (0)
作者 正文
   发表时间:2005-05-09  
软件环境:
win2k
配置文件:
BaseAction.java
public abstract class BaseAction extends Action {

    private PetStoreFacade petStore;
    /** Logger for this class and subclasses */
    protected final Log logger = LogFactory.getLog(getClass(););;
  
	public void setServlet(ActionServlet actionServlet); {
		super.setServlet(actionServlet);;
		ServletContext servletContext = actionServlet.getServletContext();;
		WebApplicationContext wac = WebApplicationContextUtils.getRequiredWebApplicationContext(servletContext);;
		this.petStore = (PetStoreFacade); wac.getBean("petStore");;
	}

	protected PetStoreFacade getPetStore(); {
		return petStore;
	}

}



applicationContext.xml
<bean id="petStore" parent="baseTransactionProxy">
		<property name="target">
			<bean class="org.springframework.samples.jpetstore.domain.logic.PetStoreImpl">
				<property name="accountDao"><ref bean="accountDao"/></property>
				<property name="categoryDao"><ref bean="categoryDao"/></property>
				<property name="productDao"><ref bean="productDao"/></property>
				<property name="itemDao"><ref bean="itemDao"/></property>
				<property name="orderDao"><ref bean="orderDao"/></property>
			</bean>
		</property>
	</bean>

错误提示信息:

你的分析:
1。在Spring的例子中Action大多是直接extends BaseAction,
大家知道BaseAction extends Action. 因为这个事情问题就出来了,在Action中所有调用business layer没有任何问题,因为可以很方便的获取PetStoreFacade。
但是项目中一般会用到Servlet,而Servlet 一般都 extends HttpServlet,问题就出在这个地方,我在servlet中如何获取类似PetStoreFacade interface呢,不能获取就意味着不能调用business layer。同样在写JUnit TestCase的时候同样存在类似问题,如此这样那岂不是不能做事了??

2。在配置文件applicationContext.xml中一般都配置了所有实现Class及DAO,而这个时候如果某个配置段出了点小问题,那么就不能正常启动web container。而在现实编码过程中比较容易出现类似问题。这个问题也一直困扰着我,而我只能天天盯着下面的成员说他们说配置文件一定要写对啊云云...如此痛苦难道没有解决的办法??

上面的2个问题希望能得到大家的一点启发,谢谢
   发表时间:2005-05-09  
在servlet能取得到ServletContext,传人ServletContext给WebApplicationContext wac = WebApplicationContextUtils.getRequiredWebApplicationContext(servletContext)就OK。

配置文件中把default-autowire打开,例如:
<beans default-autowire="byName(或byType);" >

让其自动感知。这样以后如果还经常出错,你该考虑把员工辞了
0 请登录后投票
   发表时间:2005-05-10  
你完全可以将spring放在一个单独的类中init,使其不依赖于web环境呀,当然,使用baseAction的方法也无可厚非,在单元测试上,你们可以自己写一个基类来手工装入spring的配置文件,你所说的第一种情况,另外需要使用servlet的情况,我们很少用到,所有的和业务相关的都由一个ActionServlet负责了呀,如果有另外的servlet的情况,可能是一些图形或缓存或别的与业务无关的。
0 请登录后投票
   发表时间:2005-05-10  
ServletContext sc = request.getSession();.getServletContext();;
WebApplicationContext webAppCtx = WebApplicationContextUtils.getRequiredWebApplicationContext(sc);;
.......webAppCtx.getBean(".....");;
			
0 请登录后投票
   发表时间:2005-05-13  
cbcivp 写道
ServletContext sc = request.getSession();.getServletContext();;
WebApplicationContext webAppCtx = WebApplicationContextUtils.getRequiredWebApplicationContext(sc);;
.......webAppCtx.getBean(".....");;
			

可以参考http://forum.iteye.com/viewtopic.php?t=12975,通过ContainerManager得到相应的bean,测试环境和web部署环境完全一样,applicationContextxxx文件全部放在classes目录下
0 请登录后投票
   发表时间:2005-05-30  
建议参考appfuse,它里面的BaseAction封装的很好。
0 请登录后投票
   发表时间:2005-06-08  
我的做法是在应用程序启动时先执行一个初始化程序,如InitService.init(),里面进行spring的初始化,获取其上下文,以后就可以在servlet或其他地方来调用它了。

至于配置文件嘛,没办法,必须要写对,写的不对程序怎么能运行起来。你应该庆幸不能正常启动web container呢,否则配置出了错误你可能都查不出来,到时候会死的很惨。
0 请登录后投票
   发表时间:2005-06-15  
第一个问题你的BaseAction可以继承DispatchAction啊,DispathcAction运用了java的反射机制可以动态的根据传进的参数选择需要执行的action方法,
第二个问题只能仔细一些,多做单元测试了
0 请登录后投票
   发表时间:2005-06-28  
不太明白楼主的意思?
0 请登录后投票
论坛首页 Java企业应用版

跳转论坛:
Global site tag (gtag.js) - Google Analytics