SSM项目中,普通类中调用Service
2017-09-15 13:20
351 查看
一开始在普通类中调用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
4000
找的的解决方法如下:
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
4000
相关文章推荐
- SSM环境下普通工具类中调用@Autowired注入的service
- 解决:SSM框架中普通类调用Service的问题 (转)
- Spring + Shiro 项目 + HttpSessionListener 【调用springService问题】&【Session失效问题】
- 项目启动后开启定时任务方法-->TimerTask中如何调用service
- ssm框架调用存储过程(mysql数据库),maven管理项目
- SSM下普通类(例如工具类)调用service时报NPE(NullPointException)错误
- Spring普通类获取并调用Spring service方法
- 02 整合IDEA+Maven+SSM框架的高并发的商品秒杀项目之Service层
- JavaWeb框架-SSM-普通Web项目整合流程!
- 在普通类中调用Service,Dao的方法
- 项目中js调用service和procedure的办法
- 解决:spring中普通类调用service
- SSM框架项目调用查询的方法时 报空指针异常
- 在spring项目的普通java类中获取service或dao
- Jbpm+WebService实践项目1——流程化调用“数加”“数减”两个服务(CallNumberPlusSubtract)
- 项目笔记---Windows Service调用Windows API问题
- java中SSM整合shiro dubbo service层相互调用
- 发布Silverlight项目自动生成模板,自动生成Silverlight调用RIA Service实现数据增删查改
- 微信项目,调用相册相机,上传图像Angularjs Service
- 商城项目实战07:SSM框架整合之Service层整合