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. VS2015如何新建C++或者C语言版的lib文件

    当我们不想公开我们的代码的时候,可以把我们的代码封装成静态数据连接库,即lib文件.下面介绍下如何生成lib文件. 以VS2015为例,一种是C++版的lib文件,一种是C语言版的lib文件. 一.按 ...

  2. PowerDesigner 12.5破解方法

    PowerDesigner 12.5破解方法 创建于 2017-05-07 22:18:04   一.下载   1 . PowerDesigner 12.5 官方下载地址  http://downlo ...

  3. springboot(十一):Spring boot中mongodb的使用

    mongodb是最早热门非关系数据库的之一,使用也比较普遍,一般会用做离线数据分析来使用,放到内网的居多.由于很多公司使用了云服务,服务器默认都开放了外网地址,导致前一阵子大批 MongoDB 因配置 ...

  4. static 及 extern

    1.static 与 extern 对局部变量的作用 static 和extern 都是用来修饰变量(局部的static 实际也是全局的) static 修饰的变量 只有你的包含那个变量定义的源代码文 ...

  5. mysql主从复制-异步复制

    一.创建复制账号:      在Master的数据库中建立一个备份帐户:每个Slave使用标准的MySQL用户名和密码连接master.      进行复制操作的用户会授予REPLICATION SL ...

  6. Day3 Pyhon的六大数据类型

    Python3 中有六个标准的数据类型: Number(数字) String(字符串) List(列表) Tuple(元组) Sets(集合) Dictionary(字典) Number(数字) Py ...

  7. python 小程序—三级菜单—循环和字典练习

    程序中利用多级字典来存储三级菜单, 通过一系列while循环和for循环,实现了三级菜单的查询,选择,退回上级菜单,退出程序几个功能. 缺点:程序语句过于重复,效率低. #-*-coding:utf- ...

  8. linux下fdisk分区管理、文件系统管理、挂载文件系统等

    分区管理工具有:fdisk, parted, sfdisk fdisk:对于一块硬盘来讲,最多只能管理15分区: # fdisk -l [-u] [device...]  查看硬盘设备分区信息 # f ...

  9. Python冒泡算法和修改配置文件

    先学习之前未完成的冒泡算法 li = [13,22,6,99,11] 从小到大 从第一个数字比较把大的往后移位 for m in range(4): num1 = li[m] num2 = li[m+ ...

  10. C#超简单方法实现两个richtextbox控件滚动条同步滚动

    此文章属于作者原创,转载请注明,谢谢 有时候我们需要实现对照文章等,往往将文本放到两个richtextbox控件中,但是,如果我们需要同步滚动查看,来达到更好的观看效果. 当然,传统的方法重载控件或者 ...