zoukankan      html  css  js  c++  java
  • springMVC在普通方法中调用service方法

    SpringContextUtil类

    package com.common.util;

    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);
    }
    }

    在spring.xml中配置bean

     <bean id="SpringContextUtil" class="com.common.util.SpringContextUtil" scope="singleton"></bean> 

    在普通方法类中定义

    GdshContactPersonService personService = (GdshContactPersonService) SpringContextUtil.getApplicationContext().getBean(GdshContactPersonService.class);

    然后service方法就能在普通方法中调用了

  • 相关阅读:
    IOS总结_无需自己定义UITabbar也可改变UITabbarController的背景和点击和的颜色
    破解中国电信华为无线猫路由(HG522-C)自己主动拨号+不限电脑数+iTV
    HDUJ 2074 叠筐 模拟
    CSRF——攻击与防御
    Ant命令行操作
    C#软件开发实例.私人订制自己的屏幕截图工具(七)加入放大镜的功能
    qemu-kvm-1.1.0源代码中关于迁移的代码分析
    FileSystemWatcher使用方法具体解释
    configure交叉编译
    海量图片存储策略
  • 原文地址:https://www.cnblogs.com/lijiahong/p/5442245.html
Copyright © 2011-2022 走看看