SSM项目中,普通类中调用Service
来源:互联网 发布:羽毛球鞋 推荐 知乎 编辑:程序博客网 时间:2024/06/05 22:45
一开始在普通类中调用Service,报的空指针异常.
找的的解决方法如下:
1.写一个SpringInit辅助类,代码如下:
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import org.springframework.context.ApplicationContext;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;
public class SpingInit implements ServletContextListener{
private static WebApplicationContext springContext;
public SpingInit(){
super();
}
@Override
public void contextDestroyed(ServletContextEvent arg0) {
// TODO Auto-generated method stub
}
@Override
public void contextInitialized(ServletContextEvent event) {
springContext = WebApplicationContextUtils.getWebApplicationContext(event.getServletContext());
}
public static ApplicationContext getApplicationContext() {
return springContext;
}
}
2.把该类所在的包要被spring扫描
<context:component-scan base-package="com.test.utils" />
3.在web.xml中加入该类的监听事件
<listener>
<listener-class>com.test.utils.SpingInit</listener-class>
</listener>
4.在普通类中调用:
NotifyManagementInfoService notifyManagementInfoService =
(NotifyManagementInfoService) SpingInit.getApplicationContext().getBean("notifyManagementInfoServiceImpl");
若不知道bean的被命名成什么,可在xml中加入
<bean id=" testService" class="com.yzx.crbt.service.impl.CustomerServiceImpl" />
会报expected single matching bean but found 2: testService,customerServiceImpl异常,可查看bean被命名成什么....
我小白一个,在这里的时候还吃亏了...
好了,终于可以在普通类中进行操作了
notifyManagementInfoService.add(notifyManagementInfo);
参考博客:http://www.cnblogs.com/chongerlishan/p/5942033.html
- SSM项目中,普通类中调用Service
- SSM环境下普通工具类中调用@Autowired注入的service
- 解决:spring中普通类调用service
- springMVC中普通类调用注解service方法
- ssm项目中Dao,Service抽取
- Servlet中调用ssm的service层的方法
- java中SSM整合shiro dubbo service层相互调用
- Spring框架中普通的类如何调用注解Service中的方法
- 在spring项目的普通java类中获取service或dao
- grails项目中的java类中调用service方法
- spring 在普通类中获取dao,service
- SSM项目中配置问题
- SSM中整合Web Service服务端开发
- service中调用dialog
- 项目中Service使用
- spring 在普通类中调用注入的对象
- Silverlight中调用Web Service
- 非Controller中调用Service
- 最大连续子数组和与最大连续子矩阵和
- Android状态栏渐变色的实现
- openstack常用检查命令杂烩
- Android项目重构-架构篇
- Mysql学习整理(五)
- SSM项目中,普通类中调用Service
- 使用朴素贝叶斯分类器对新闻文本数据进行类别预测
- Java 类 文件 编译
- Boolean.valueOf的用法(字符串转boolean时注意)
- CSDN博客初次动作!有关于 Windows磁盘100%的解决方法。
- 137. Single Number II
- redis实现分布式锁
- 1315:游程编码
- Redis的数据结构(一)