Spring MVC中一般类使用service
来源:互联网 发布:工商信息查询网大数据 编辑:程序博客网 时间:2024/04/30 19:31
在Spring MVC中,Controller中使用service只需使用注解@Resource就行,但是一般类(即不使用@Controller注解的类)要用到service时,可用如下方法:
1、SpringContextUtil
package com.test.framework.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接口是必须实现的方法@Overridepublic 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.framework.utils.SpringContextUtil" scope="singleton"/>
3、使用
DictService dictService = (DictService) SpringContextUtil.getBean("dictService");List<Dict> dict = (List<Dict>) dictService.findByHQL(hql);
0 0
- Spring MVC中一般类使用service
- Spring MVC中一般类使用service
- 在Spring MVC中使用工具类调用Service层时,Service类为null如何解决
- 在Spring MVC中使用工具类调用Service层时,Service类为null如何解决
- (亲测有效。)Spring MVC中,Controller中使用service只需使用注解,但是普通类获取 service或 dao 都是null空的。
- Spring MVC中@Repository、@Service、@Controller、@Compon的使用及区别
- spring mvc+ mybaties+ehcache(maven工程),在service层中使用
- Spring MVC学习—切面的使用(统计service中方法执行的时间)
- spring mvc 中使用spring的validator
- spring mvc 中使用spring的validator
- spring mvc 中使用spring的validator
- spring mvc 中使用spring的validator
- spring mvc在非Controller类无法使用Service bean解决方案
- spring mvc在非Controller类无法使用Service bean解决方案
- spring mvc 无法扫描jar中controller和Service
- 【SpringMVC】<context:include-filter>和<context:exclude-filter>使用时要注意的地方 在Spring MVC中的配置中一般会遇到这两个标签,作
- 【SpringMVC】<context:include-filter>和<context:exclude-filter>使用时要注意的地方 在Spring MVC中的配置中一般会遇到这两个标签,作为<
- Servlet中使用Spring注入Service层
- android串口通信
- python 装逼指南(一)
- Eucalyptus管理页面密码设置
- linux使用autotools生成可执行文件、静态库、动态库Makefile的流程介绍
- 用超链接a来实现邮件、拨号、发送短信功能
- Spring MVC中一般类使用service
- SecureCRT上传/下载linux文件
- APP不通过AppStore下载,APP应用内完成升级(不需要重新上传、审核)---他人文章链接,自己留着
- [动态规划] 蛇行序列(snake sequence)
- IOS NSPredicate 及正则表达式
- VC++6.0中MFC图片控件无法利用类向导添加成员变量的解决
- value stack contents 和 stack context(http://www.cnblogs.com/bukudekong/archive/2012/04/08/2437398.h)
- 行为设计模式---观察者模式(Observer)
- Word Break