webservice类中spring注解无法注入资源

webservice服务中spring注解无法注入资源

在一个项目中需要提供webservice服务,又要在服务中调用Service层的业务逻辑,于是在webservice中用注解注入资源,结果调用webservice一直报空指针,Debug程序时发现要用到的Bean没有成功注入,然后发现注解似乎没起作用


applicationContext.xml:

我的配置文件已经开启扫描了

<context:component-scan base-package="com.wcz" />

context:annotation-config:注解扫描是针对已经在Spring容器里注册过的Bean
context:component-scan:不仅具备context:annotation-config的所有功能,还可以在指定扫描的package以及下面的子包

Service类:

webservice类中注解无效:

注解不起作用,只好用代码初始化资源,从类路径ClassPath中寻找指定的XML配置文件,找到并装载完成ApplicationContext的实例化工作

解决方案:

@WebService
public class User {
	private static DataServicesImpl dataServices;

	static {
		//装载单个配置文件实例化ApplicationContext容器
		ApplicationContext ctx = new ClassPathXmlApplicationContext("classpath:applicationContext.xml");
		dataServices = (DataServicesImpl) ctx.getBean("DataServicesImpl");
	}
	@WebMethod
	public String getModifyUserToken(){
		//业务逻辑
	}
}

这里附上ApplicationContext接口的常用实现类介绍

上面使用的ClassPathXmlApplicationContext加载多个配置文件

//装载多个配置文件实例化ApplicationContext容器
String[] configs = {"bean1.xml","bean2.xml","bean3.xml"};
ApplicationContext cxt = new ClassPathXmlApplicationContext(configs);

FileSystemXmlApplicationContext
从指定的文件系统路径中寻找指定的XML配置文件,找到并装载完成ApplicationContext的实例化工作。

//装载单个配置文件实例化ApplicationContext容器
ApplicationContext cxt = new FileSystemXMLApplicationContext("beans.xml");

//装载多个配置文件实例化ApplicationContext容器
String[] configs = {"c:/beans1.xml","c:/beans2.xml"};
ApplicationContext cxt = new FileSystemXmlApplicationContext(configs);

XmlWebApplicationContext
从Web应用中寻找指定的XML配置文件,找到并装载完成ApplicationContext的实例化工作。这是为Web工程量身定制的,使用WebApplicationContextUtils类的getRequiredWebApplicationContext方法可在JSP与Servlet中取得IoC容器的引用

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

 ApplicationContext ctx = WebApplicationContextUtils.getWebApplicationContext(servletContext);
  • 3
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 10
    评论
评论 10
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

無间行者

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值