组件扫描这种的是指bean,跟service没关系

service只能在Controller类中使用,如果别的类想使用,必须使用下面这种方法

内容来源:https://blog.csdn.net/u011242657/article/details/71123206

测试可用

使用Spring框架,我们不需要创建类的对象,都有Spring 容器创建,并通过注解来注入。注入的原理就是在程序启动的时候,Spring根据xml中配置的路径来扫描类,如果发现类的上方有类似@Service,@Controller,此时就会定位到当前类,然后来给当前类中标有注解的属性进行注入,从而我们可以使用该属性,调用方法。

那么普通类怎么使用@Service标记的方法呢?

1.如果你想用@autowired,那么这个类本身也应该是在spring的管理下 的,即你的UserLogUtil也要标注为一个component(或Service),这样spring才知道要注入依赖; 
2. 如果不标注为@Component的话,此时不能通过@autowired来注入依赖,只能通过ApplicationContext来取得标注为Service的类: 
UserLogService service = ApplicationContext.getBean(UserLogService.class);

那么Web项目中如何获取ApplicationContext

1.

ApplicationContext ac1 = WebApplicationContextUtils.getRequiredWebApplicationContext(ServletContext sc)

2.

ApplicationContext ac2 = WebApplicationContextUtils.getWebApplicationContext(ServletContext sc)

注:至于获取ServletContext对象,可以从request,session中获取,他们都有getServletContext()方法

3 写一个工具类实现ApplicationContextAware接口,并将这个加入到spring的容器(推荐)

package com.ncut.ssm.util;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
/**
* @author Frank Yuan
* @create 2017-05-02-下午 10:32
**/
public class SpringContextUtil implements ApplicationContextAware {
private static ApplicationContext applicationContext = null; @Override
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
this.applicationContext = applicationContext;
} public static ApplicationContext getApplicationContext(){
return applicationContext;
} public static Object getBean(String beanName){
return applicationContext.getBean(beanName);
} public static Object getBean(Class c){
return applicationContext.getBean(c);
}

然后将此bean注册到spring 的容器中,在spring的配置文件添加如下代码

<bean id="springContextUtil" class="com.ncut.ssm.util.SpringContextUtil"></bean>

最后在普通类就可以这样调用

ApplicationContext appCtx = SpringContextUtil.getApplicationContext();
UserService bean = (UserService)SpringContextUtil.getBean("UserService");

或者

ApplicationContext appCtx = SpringContextUtil.getApplicationContext();
UserService bean = (UserService)SpringContextUtil.getBean(UserService.class);

第一种情况适用于在@Service(“”UserService” “)标注了bean的名字

@Service("UserService")
public class UserServiceImpl implements UserService {

第二种情况适用于在@Service后面什么也没有

@Service
public class UserServiceImpl implements UserService {

这种情况下不能使用UserServcieImpl.class,而是要用其接口类UserService.class,因为UserServiceImpl 没有被其他类注入过,会报找不到这个class

非Contorller类使用@Service中的方法的更多相关文章

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

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

  2. android 中activity调用本地service中的方法。

    1.自定义一个接口,暴露服务中的方法 public interface IService {    /**服务中对外暴露的方法 */    void methodInService();} 2.自定一 ...

  3. spring非controller类获取service方法

    ApplicationContext ctx = new ClassPathXmlApplicationContext("spring.xml"); pushMessageServ ...

  4. android 中activity调用远程service中的方法之 aidl的使用

    服务端:只有服务,没有界面 1.编写interface文件,复制到 .aidl 文件中,并去掉其中的public 等修饰符.系统会自动在gen目录下生成对应的java文件  (对应本地调用中的接口文件 ...

  5. spring @Service()中初始化方法

    @Service(value = "xxxServiceImpl" xxxxxxxx) public class XXXSerivceImpl { public void init ...

  6. 在Service中抛出异常事务未回滚问题分析与解决

    1.问题提出:在service中写方法时,抛出了一个Exception, 本来目的是为了让事务回滚, 但事实上没有回滚,产生了脏数据.代码如下:@Override@Transactionalpubli ...

  7. android service中stub作用是什么?

    AIDL(android 接口描述语言)是一个IDL语言,它可以生成一段代码,可以使在一个android设备上运行的两个进程使用内部通信进程进行交互.如果你需要在一个进程中(例如:在一个Activit ...

  8. Service中事务不能回滚的解决方式(转)

    1.在service方法里面如果对异常进行了捕获的话,该事务是不会进行回滚的        默认spring事务只在发生未被捕获的 runtimeexcetpion时才回滚.          spr ...

  9. 已经mock类中引用的其它service类,但是在invoke私有方法的时候,该service类是空值

    错误原因:没有在开始测试用例的时候,初始化类的所有注解方法. 解决方法: 使用mock方法创建mock对象时,需要在测试用例执行前执行以下代码.通常, 这句代码可以放在测试基类或者@Before 中. ...

随机推荐

  1. 【CSS】iconfont的使用

    说到浏览器对@font-face的兼容问题,这里涉及到一个字体format的问题,因为不同的浏览器对字体格式支持是不一致的,这样大家有必要了解一下,各种版本的浏览器支持什么样的字体,前面也简单带到了有 ...

  2. 【Git】本地与GitHub同步

    按步骤一步一步来,成功啦~ 以管理员身份运行Git-bash 要求输入用户名,密码 成功推入github~~加油加油 补充: 将仓库中的改动同步到本地 在git-bash中进入项目目录下,使用git ...

  3. WEB安全 魔术引号及注入类型

    一.魔术引号 1. magic_quotes_gpc 变量 什么是魔术引号 Warning本特性已自 PHP 5.3.0 起废弃并将自 PHP 5.4.0 起移除.当打开时,所有的 '(单引号),&q ...

  4. [转]墨卡托投影坐标系(Mercator Projection)原理及实现C代码

    墨卡托投影是一种“等角正切圆柱投影”,荷兰地图学家墨卡托(Mercator)在1569年拟定:假设地球被围在一个中空的圆柱里,其赤道与圆柱相接触,然后再假想地球中心有一盏灯,把球面上的图形投影到圆柱体 ...

  5. mark DOwm

    https://github.com/summerscar/live2dDemo {% cq %} 人生乃是一面镜子, 从镜子里认识自己, 我要称之为头等大事, 也只是我们追求的目的! {% endc ...

  6. 解决java log4j 配置log4jCaused by: java.lang.ClassNotFoundException: org.apache.logging.log4j.LogManager

    前提安装http://mirror.bit.edu.cn/apache/logging/log4j/2.11.2/apache-log4j-2.11.2-bin.zip Buildpath 配置add ...

  7. 1<=portNo<=4竟然在keil4.71里面不报错

    1.if( 1<=portNo<=4 ) {  CardIn2_CS_L; //pull low  CardIn1_CS_H;  CardOut1_CS_H;  CardOut2_CS_H ...

  8. linux crontab 计划任务设置 (简结)

    命令: crontab  -l  查看当前运行的计划任务 crontab  -e  编辑当前运行计划任务 修改或添加 VIM编辑器用法:按 i 键进入编辑文本状态, esc 结束编辑状态 , :wq ...

  9. Oracle 11G 隐含参数“_controlfile_autobackup_delay”

    RMAN设置控制文件自动备份,当发生数据库备份时,或建表空间,删除log文件等物理结构发生改变时,oracle会自动备份控制文件. Oracle 10g会立刻备份,Oracle 11g会有几分钟的延迟 ...

  10. es6 入坑笔记(四)---异步处理

    promise 用于js的异步处理 形式: 1.申明一个promise的对象 let p = new Promise(function(成功时的参数,失败时的参数){ if(....){ 成功时的参数 ...