WebService对象调用spring注解

本文介绍了解决WebService类中无法直接使用Spring注解属性的问题的方法。通过创建单例类和服务启动时的初始化操作,实现了在WebService中访问Spring管理的对象。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

前言:前面一文写完了webservice的通信,然后接下来在实际项目中发现了webservice类中无法直接使用spring注解过的属性,又经过将近一天的调查,脑子几乎僵硬了,自己差点放弃,在领导的帮助下最终找到解决方案,就赶紧总结下来,人贵有思,好记忆不如烂笔头,记录下来就是资源。

 

首先,如果你是这样直接使用的话,对象是空的,如下图,moneyuserDAO是无法直接使用的,即使spring已经对其进行加载,但是在webservice中是不能直接使用的,但是spring加载的访问数据库持久层的DAO对象等会存储在WebApplicationContext中,那么我有了以下的方式

 

第一步:写好以下的单例内容

package com.ebiz.cms.webservice;

import org.springframework.web.context.WebApplicationContext;

public class ServicesSingleton {
	private WebApplicationContext servletContext;

	public WebApplicationContext getServletContext() {
		return servletContext;
	}

	public void setServletContext(WebApplicationContext servletContext) {
		this.servletContext = servletContext;
	}

	private volatile static ServicesSingleton singleton;

	private ServicesSingleton() {
	}

	public static ServicesSingleton getInstance() {
		if (singleton == null) {
			synchronized (ServicesSingleton.class) {
				if (singleton == null) {
					singleton = new ServicesSingleton();
				}
			}
		}
		return singleton;
	}
	
	public Object getDAO(Class<?> classNameOfDAO) {
		String implName = classNameOfDAO.getSimpleName();
		String firstChar = implName.substring(0, 1);
		implName = firstChar.toLowerCase() + implName.substring(1) + "Impl";
		return singleton.getServletContext().getBeansOfType(classNameOfDAO).get(implName);
	}
	
}

注意里面最重要的getDAO方法和单例的实现方式,通过WebApplicationContext的getBeansOfType方法找到对应的DAO类。

 

 

第二步:编写tomcat启动时加载WebApplicationContext的类和xml配置

package com.ebiz.cms.webservice;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;

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

public class InitServlet extends HttpServlet {
	/**
	 * 
	 */
	private static final long serialVersionUID = -3962535683227715257L;

	@Override
	public void init() throws ServletException {
		WebApplicationContext ctx = WebApplicationContextUtils.getRequiredWebApplicationContext(getServletContext());
		ServicesSingleton.getInstance().setServletContext(ctx);
	}
}

 

 <servlet>
      <servlet-name>initServlet</servlet-name>
      <servlet-class>com.ebiz.cms.webservice.InitServlet</servlet-class>
      <load-on-startup>0</load-on-startup>
</servlet>

 以上,就会将对应的WebApplicationContext对象加载到单例中,然后我们再在webservice对象中通过getDAO方法获取对应的DAO对象对数据库进行持久层操作

public String createMemPxy(String username, String password, String mobile, String email, String type) {
		try {
			ServicesSingleton single = ServicesSingleton.getInstance();
			MembersDAO memberDAO =  (MembersDAOImpl)single.getDAO(MembersDAO.class);

 以上只写出service对象的service方法片段

 

总结:我在此过程中卡到了一个点就是,我没有通过webservice之间的访问方式去获取对象的单例,而是通过在service类中写main方法去测试调用单例,却发现在main方法中怎么都不会获取tomcat启动时初始化好的单例,这是一个低级错误,因为经过一天的调查,脑子基本上以及僵硬了,不过经过领导的指点后,顺利的解决了该问题。这样,整体的Java调用webservice以及关联spring的持久层操作就搞定了,舒心啊!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值