在Spring MVC中,Controller中使用service只需使用注解@Resource就行,但是一般类(即不使用@Controller注解的类)要用到service时,可用如下方法:

1、SpringContextUtil

package com.test.utils;

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.NoSuchBeanDefinitionException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware; public class SpringContextUtil implements ApplicationContextAware { private static ApplicationContext applicationContext; // Spring应用上下文环境 // 下面的这个方法上加了@Override注解,原因是继承ApplicationContextAware接口是必须实现的方法
@Override
public void setApplicationContext(ApplicationContext applicationContext)
throws BeansException {
SpringContextUtil.applicationContext = applicationContext;
} public static ApplicationContext getApplicationContext() {
return applicationContext;
} public static Object getBean(String name) throws BeansException {
return applicationContext.getBean(name);
} public static Object getBean(String name, Class requiredType)
throws BeansException {
return applicationContext.getBean(name, requiredType);
} public static boolean containsBean(String name) {
return applicationContext.containsBean(name);
} public static boolean isSingleton(String name) throws NoSuchBeanDefinitionException {
return applicationContext.isSingleton(name);
} public static Class getType(String name) throws NoSuchBeanDefinitionException {
return applicationContext.getType(name);
} public static String[] getAliases(String name) throws NoSuchBeanDefinitionException {
return applicationContext.getAliases(name);
}
}

2、Spring的配置文件application.xml中进行如下配置

<bean id="SpringContextUtil" class="com.test.utils.SpringContextUtil"  scope="singleton"/>

3、使用

static SysUserService userService = (SysUserService) SpringContextUtil.getBean("userService");
List<SysUser> list = userService.getAll();

Spring MVC中一般类使用service的更多相关文章

  1. Spring MVC中一般 普通类调用service

    在Spring MVC中,Controller中使用service只需使用注解@Resource就行,但是一般类(即不使用@Controller注解的类)要用到service时,可用如下方法: 1.S ...

  2. Spring MVC 中的基于注解的 Controller【转】

    原文地址:http://my.oschina.net/abian/blog/128028 终于来到了基于注解的 Spring MVC 了.之前我们所讲到的 handler,需要根据 url 并通过 H ...

  3. spring mvc中的valid

    当你希望在spring mvc中直接校验表单参数时,你可以采用如下操作: 声明Validator的方式: 1.为每一个Controller声明一个Validator @Controller publi ...

  4. Spring MVC中基于注解的 Controller

         终于来到了基于注解的 Spring MVC 了.之前我们所讲到的 handler,需要根据 url 并通过 HandlerMapping 来映射出相应的 handler 并调用相应的方法以响 ...

  5. Spring MVC 中获取session的几种方法

    Spring MVC 中使用session是一种常见的操作,但是大家上网搜索一下可以看到获取session的方式方法五花八门,最近,自己终结了一下,将获取session的方法记录下来,以便大家共同学习 ...

  6. Servlet的生命周期以及在Spring MVC中调用流程

    接触Web时间比较久,虽然知道Servlet的生命周期但是理解却还是不够,今天刚好debug代码涉及这块就利用余下时间研究了一下. Servlet的生命周期以及处理浏览器请求的过程.Servlet接口 ...

  7. Spring MVC 中的基于注解的 Controller(转载)

           终于来到了基于注解的 Spring MVC 了.之前我们所讲到的 handler,需要根据 url 并通过 HandlerMapping 来映射出相应的 handler 并调用相应的方法 ...

  8. Spring MVC:DispatchServlet类

    Spring MVC架构 Spring Web MVC是基于Servlet API构建的原始Web框架,从一开始就已包含在Spring框架中.传统的模型层被拆分为了业务层(Service)和数据访问层 ...

  9. spring mvc中使用freemark的一点心得

    参考文档: FreeMarker标签与使用 连接http://blog.csdn.net/nengyu/article/details/6829244 freemarker学习笔记--指令参考: ht ...

随机推荐

  1. 三、Linux的目录结构:

    root管理员的home目录root 其他用户的home目录home目录中

  2. Oracle 11g 测试ogg中断之后,重新同步操作

    测试ogg中断之后,重新同步操作 2018-06-07 17:11 779 1 原创 GoldenGate 本文链接:https://www.cndba.cn/leo1990/article/2839 ...

  3. Ubuntu18.04安装常用软件

    一.VMwareWorkstation 1.到官网下载VmwareWorkstation,选择Linux版本 2.将下载下来的安装包放到桌面给予x权限,通过命令行进入到桌面的目录sudo ./执行安装 ...

  4. C# [LINQ] Linq Expression 获取多格式文件

    using System; using System.IO; using System.Linq; using System.Linq.Expressions; internal static str ...

  5. 理解什么是适配器(adapter)和接口(interface)

    ● 适配器(adapter) In computing, adapter is a hardware device or software component that converts transm ...

  6. 分布式大数据系统离线分析技术解决方案(spark2.x)

    一.sark2.x新型的架构系统

  7. python cmd的各种实现方法及优劣

    Python_cmd的各种实现方法及优劣(subprocess.Popen, os.system和commands.getstatusoutput)   目前我使用到的python中执行cmd的方式有 ...

  8. 1011. World Cup Betting (20)

    生词(在文中的意思) tie 平局 lay a bet 打赌 putting their money where their mouths were 把他们的钱用在刀刃上 manner of 的方式 ...

  9. jquery tmpl生成导航

    引入<script src="jquery.tmpl.min.js"></script> html<ul class="nav" ...

  10. 北大poj- 1034

    The dog task Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 3272   Accepted: 1313   Sp ...