gwt+spring+hibernate的整合

主要思想:在系统中读取applicationContext.xml文件,但如果放在service的方法中,每次使用该方法时都会去读该文件,则速度会慢的惊人。改进方法是把所有配置文件设置在web.xml的context参数中.

<context-param> 
<param-name>contextConfigLocation</param-name> 
<param-value> 
/WEB-INF/applicationContext-*.xml,/WEB-INF/applicationContext-*-*.xml,classpath*:applicationContext-*.xml,classpath*:applicationContext-*-*.xml 
</param-value> 
</context-param> 

<listener> 
<listener-class> 
org.springframework.web.context.ContextLoaderListener 
</listener-class> 
</listener> 

 

listener一定要有,用于初始化context参数

写个BaseService继承于RemoteServiceServlet于接口,并在init方法中写相应的读取文件方法(由于文件读取已经在context中设置好,相当于资源已经存于内存中,每次读取时速度就快很多)

public class BaseService extends RemoteServiceServlet{ 
/** 
* 
*/ 
private static final long serialVersionUID = 1L; 
protected WebApplicationContext springContext;   
  
    @Override  
    public void init(ServletConfig config) throws ServletException {   
        super.init(config);   
        springContext = WebApplicationContextUtils.getWebApplicationContext(   
                config.getServletContext());  //读取配置文件
    }   

}
gwt的server端要各实现类都要继承BaseService
public class HelloServiceImpl extends BaseService implements HelloService {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	public String sayHello(int id) {
		UserManageService um = (UserManageService) springContext
				.getBean("userManageService");
		User user = um.findUserByUserID(id);
		return user.getLoginName();
	}

}
  
这里就可以使用spring端方法了
UserManageService um = (UserManageService) springContext
				.getBean("userManageService");
User user = um.findUserByUserID(id);

 具体见上传的代码(jar包是精简版的,日记有点问题,但不影响),由于只能上传10M,想要的联系我

  

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值