非Controller中调用Service
1. 新增文件
|
package com.library.common; import org.springframework.beans.BeansException; import org.springframework.context.ApplicationContext; import org.springframework.context.ApplicationContextAware; public class ApplicationContextHelper implements ApplicationContextAware { private static ApplicationContext appCtx; @Override public void setApplicationContext( ApplicationContext applicationContext ) throws BeansException { appCtx = applicationContext; } public static Object getBean( String beanName ) { return appCtx.getBean( beanName ); } public static <T> T getBean(Class<T> clz) { return (T)appCtx.getBean(clz); } } |
2. 在spring.xml中的最后加入
|
<bean id="SpringApplicationContext" class="com.library.common.ApplicationContextHelper"></bean> |
3. 调用代码
|
UserService us=ApplicationContextHelper.getBean(UserService.class); |
非Controller中调用Service的更多相关文章
- 关于controller中调用多个service方法的问题
一般service方法是有事务的,把所有操作封装在一个service方法中是比较安全的. 如果在controller中调用多个service方法,只有查询的情况下是可以这样的.
- Spring MVC普通类或工具类中调用service报空空指针的解决办法(调用service报java.lang.NullPointerException)
当我们在非Controller类中应用service的方法是会报空指针,如图: 这是因为Spring MVC普通类或工具类中调用service报空null的解决办法(调用service报java.la ...
- 在Java filter中调用service层方法
在项目中遇到一个问题,在 Filter中注入 Serivce失败,注入的service始终为null.如下所示: public class WeiXinFilter implements Filter ...
- 非Controller类无法使用Service bean解决方案
尝试方案: 1 在Spring的配置文件springmvc.xml中,增加扫描项base-package="zxs.ssm.util",增加你需要使用service的类所在的包 ...
- 如何在yii的controller中调用外部action
问题: 在yii中,一个controller会包含若干个action.有时为了重用或代码管理等目的,我们希望这些action可以单独定义成一个类,然后在controller中使用.那么在yii中要如何 ...
- springMVC在普通方法中调用service方法
SpringContextUtil类 package com.common.util; import org.springframework.beans.BeansException;import o ...
- Spring框架中,在工具类或者普通Java类中调用service或dao
spring注解的作用: 1.spring作用在类上的注解有@Component.@Responsity.@Service以及@Controller:而@Autowired和@Resource是用来修 ...
- BroadcastReceiver中调用Service
首先是代码: package com.larry.msglighter; import android.content.BroadcastReceiver; import android.conten ...
- Spring @Autowired注解在非Controller中注入为null
问题描述 今天在写一个工具类,里面用了@Autowired注入了StringRedisTemplate以及RedisTemplate时,在template.opsForValue().set(key, ...
随机推荐
- CSS3 & SVG 制作钟表
在线演示 源码下载
- Mac os下安装brew
1.首先没下载xcode,请先安装xcode,安装的继续往下面看 2.安装brew /usr/bin/ruby -e "$(curl -fsSL https://raw.githubuser ...
- 实体框架(Entity Framework)
实体框架(Entity Framework) 实体框架(Entity Framework)简介 ADO.NET Entity Framework 是微软以 ADO.NET 为基础所发展出来的对象关系对 ...
- glassfish PWC6351: In TLD scanning 系统找不到指定的文件问题解决
[2017-04-25T21:26:09.391+0800] [glassfish 4.1] [WARNING] [] [org.apache.jasper.runtime.TldScanner] [ ...
- SpringMVC 国际化-中英文切换
项目结构 1.pom.xml <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http: ...
- 新手上路---Java学习知识梳理
我随笔 我相信也有那么一些人跟我一样,脱下军装后对未来感到迷茫,不知所措甚至怀疑起自己的能力,自身体会的经历过才能明白其中的辛酸!归咎一切:心态,信心,自信!再穷不过乞讨,不死终将出头.好了,闲言碎语 ...
- Linux安装yum以及更新yum版本
.使用RedHat系统不能正常使用yum安装 由于RedHat没有注册,所有不能使用它自身的资源更新,于是使用yum instatll gcc-c++的时候出现This system is not r ...
- Centos程序最小化后,窗口标签都消失找不到窗口的问题
我是用的centos版本是CentOs 7. 在“顶部面板”或者 “底部面板” 右击选择“添加组件”),如下图所示: 在搜索框里输入“窗口列表”(window list),选中“窗口列表”即可.如下图 ...
- background新增的N个强悍功能!!!
background在CSS3中新增样式: [ ] 首先我们先回顾下background的原有样式: background-color 背景颜色 相关属性值: 关键字:red,blue,yellow等 ...
- angular.js的ui-router总结
ui-route 先有个网址,再在这个网址下写路由 子路由的搭建 我们的布局/模板文件 index.html 我们通过建立一个主文件来引入我们所需要的所有资源以开始我们的项目 ,这里 ...