webservice注入的 bean 为NULL 的解决办法

tech2023-05-30  122

1.使用SpringUtil

package com.cn.util; import org.springframework.beans.BeansException; import org.springframework.context.ApplicationContext; import org.springframework.context.ApplicationContextAware; public class SpringUtil implements ApplicationContextAware { private static ApplicationContext applicationContext; // Spring应用上下文环境 public void setApplicationContext(ApplicationContext applicationContext) throws BeansException { SpringUtil.applicationContext = applicationContext; } public static ApplicationContext getApplicationContext() { return applicationContext; } @SuppressWarnings("unchecked") public static <T> T getBean(String name) throws BeansException { return (T) applicationContext.getBean(name); } @SuppressWarnings("unchecked") public static <T> T getBean(Class<?> clz) throws BeansException { return (T) applicationContext.getBean(clz); } }

在xml中配置你要调用的bean

<bean name="user" class="com.cn.User" > </bean>

在你的webservice类中调用

User user=SpringUtil.getBean("user"); //取bean id

至此就可以调用了。

最新回复(0)