主要思想:在系统中读取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,想要的联系我