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的更多相关文章

  1. 关于controller中调用多个service方法的问题

    一般service方法是有事务的,把所有操作封装在一个service方法中是比较安全的. 如果在controller中调用多个service方法,只有查询的情况下是可以这样的.

  2. Spring MVC普通类或工具类中调用service报空空指针的解决办法(调用service报java.lang.NullPointerException)

    当我们在非Controller类中应用service的方法是会报空指针,如图: 这是因为Spring MVC普通类或工具类中调用service报空null的解决办法(调用service报java.la ...

  3. 在Java filter中调用service层方法

    在项目中遇到一个问题,在 Filter中注入 Serivce失败,注入的service始终为null.如下所示: public class WeiXinFilter implements Filter ...

  4. 非Controller类无法使用Service bean解决方案

      尝试方案: 1 在Spring的配置文件springmvc.xml中,增加扫描项base-package="zxs.ssm.util",增加你需要使用service的类所在的包 ...

  5. 如何在yii的controller中调用外部action

    问题: 在yii中,一个controller会包含若干个action.有时为了重用或代码管理等目的,我们希望这些action可以单独定义成一个类,然后在controller中使用.那么在yii中要如何 ...

  6. springMVC在普通方法中调用service方法

    SpringContextUtil类 package com.common.util; import org.springframework.beans.BeansException;import o ...

  7. Spring框架中,在工具类或者普通Java类中调用service或dao

    spring注解的作用: 1.spring作用在类上的注解有@Component.@Responsity.@Service以及@Controller:而@Autowired和@Resource是用来修 ...

  8. BroadcastReceiver中调用Service

    首先是代码: package com.larry.msglighter; import android.content.BroadcastReceiver; import android.conten ...

  9. Spring @Autowired注解在非Controller中注入为null

    问题描述 今天在写一个工具类,里面用了@Autowired注入了StringRedisTemplate以及RedisTemplate时,在template.opsForValue().set(key, ...

随机推荐

  1. waiting for spring......

    世间哪得双全法,不负如来不负卿....<仓央嘉措>

  2. Swift、Objective-C 单例模式 (Singleton)

    Swift.Objective-C 单例模式 (Singleton) 本文的单例模式分为严格单例模式和不严格单例模式.单例模式要求一个类有一个实例,有公开接口可以访问这个实例.严格单例模式,要求一个类 ...

  3. Python资源汇总

    Python 目录: 管理面板 算法和设计模式 反垃圾邮件 资产管理 音频 验证 构建工具 缓存 ChatOps工具 CMS 代码分析和Linter 命令行工具 兼容性 计算机视觉 并发和并行性 组态 ...

  4. CodeSmith生成实体的分页读取规则

    首先.我得向咱们博客园提个意见,能不能我写的东西就给预保存下呢?刚才我写半天,只因为这个不给力的IE浏览器死了,导致我白写了,如果这要是那个大神直接在这上面写的非常有技术含量的贴着会因此而丢失实在是有 ...

  5. app性能测试【通过loadrunner录制】

    随着智能手机近年来的快速增长,从游戏娱乐到移动办公的各式各样的手机APP软件渗透到我们的生活中,对于大型的手机APP测试不仅要关注它的功能性.易用性还要关注它的性能,最近发现LoadRunner12可 ...

  6. Spring boot 默认静态资源路径与手动配置访问路径

    在application.propertis中配置 ##端口号server.port=8081 ##默认前缀spring.mvc.view.prefix=/## 响应页面默认后缀spring.mvc. ...

  7. ST-2

    1.第一个程序没有覆盖到下表为0的数.第二个程序找到的是x中第一个等于0的数的下标. 2.对于第一个程序:x = [2,3,5], y = 3 对于第二个程序:X = [2,0,6] 3.对于两个程序 ...

  8. LeetCode4. Median of Two Sorted Arrays---vector实现O(log(m+n)--- findkth

    这道题目和PAT上的1029是同一题.但是PAT1029用O(m+n)的时间复杂度(题解)就可以,这道题要求是O(log(m+n)). 这道题花费了我一个工作日的时间来思考.因为是log因而一直思考如 ...

  9. ThreadGroup详解

     ①定义线程组 ThreadGroup类中有 2个构造方法,它们用来定义线程组.这 2个构造方法的使用格 式如下: public ThreadGroup(String name); public Th ...

  10. 一个使用openGL渲染的炫丽Android动画库二(碎片化曲面动画)

    续一个使用openGL渲染的炫丽Android动画库 MagicSurfaceView v1.1.0发布, 新增碎片化曲面动画 地址:https://github.com/gplibs/android ...