springMVC中controller层调用service层的方式
来源:互联网 发布:人工智能 04 哈工大 编辑:程序博客网 时间:2024/05/21 11:18
springmvc 中普通类调用注解service层 的方式除了
1.自动注入外:
@AutowiredUserService userService;
2.还可以这样:
工具类:
/** * <p> * 普通类调用Spring注解方式的Service层bean * </p> * @author zhengdong 2016年10月18日 下午3:54:29 * @version V1.0 */public class SpringBeanFactoryUtils implements ApplicationContextAware {private static ApplicationContext appCtx;/** * 此方法可以把ApplicationContext对象inject到当前类中作为一个静态成员变量。 * * @param applicationContext ApplicationContext 对象. * @throws BeansException * @author hzc */public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {appCtx = applicationContext;}/** * 获取ApplicationContext * * @return * @author hzc */public static ApplicationContext getApplicationContext() {return appCtx;}/** * 这是一个便利的方法,帮助我们快速得到一个BEAN * * @param beanName bean的名字 * @return 返回一个bean对象 * @author hzc */public static Object getBean(String beanName) {return appCtx.getBean(beanName);}/** * 这是一个便利的方法,帮助我们快速得到一个BEAN * * @param Class 服务bean * @return 返回一个bean对象 * @author hzc */public static Object getBean(Class c) {return appCtx.getBean(c);}}
在配置文件中增加一个bean:
<bean id="springBeanFactoryUtils" class="com.zd.common.SpringBeanFactoryUtils"/>
现在就可以进行调用了,demo:
/** * <p> * 用户 * </p> * @author zhengdong 2016年10月11日 下午4:59:22 * @version V1.0 */public interface UserService {/** * 获取用户信息 * @author zhengdong 2016年10月12日 上午9:42:32 * @return */public Map<String, String> getUserMap();}
/** * <p> * 用户 * </p> * @author zhengdong 2016年10月11日 下午4:59:57 * @version V1.0 */@Transactional@Service("userService")public class UserServiceimpl implements UserService {private static final Logger logger = Logger.getLogger(UserServiceimpl.class);public Map<String, String> getUserMap() {logger.warn("准备从数据库获取用户信息了...");Map<String, String> userMap = new HashMap<String, String>();userMap.put("name", "小强");userMap.put("password", "123456");logger.warn("从数据库获取用户信息完毕...");return userMap;}}
controller中获取bean,并调用接口:
UserService userService2 = (UserService)SpringBeanFactoryUtils.getBean(UserService.class);Map<String, String> userMap2 = userService2.getUserMap();
0 0
- springMVC中controller层调用service层的方式
- SpringMVC的层:DAO、Service、Controller、View
- junit 测试springMVC的Controller、Service层代码
- controller或者service层调用配置文件里的属性
- springmvc 中dao层和service层的区别
- [SSM]springmvc 中dao层和service层的区别
- 第二次报错:在spring+springMVC+hibernate中,controller调用service层方法或者dao层方法,报错java.lang.NullPointerException
- springmvc想在filter中调用service层
- SpringMVC容器中Servlet如何调用service层接口
- SpringMVC容器中Servlet如何调用service层接口
- SpringMVC的四个基本注解annotation(控制层,业务层,持久层) -- @Component、@Repository @Service、@Controller
- SpringMVC的四个基本注解annotation(控制层,业务层,持久层) -- @Component、@Repository @Service、@Controller
- SpringMVC的四个基本注解annotation(控制层,业务层,持久层) -- @Component、@Repository @Service、@Controller
- SpringMVC的四个基本注解annotation(控制层,业务层,持久层) -- @Component、@Repository @Service、@Controller
- SpringMVC的四个基本注解annotation(控制层,业务层,持久层) -- @Component、@Repository @Service、@Controller
- SpringMVC的四个基本注解annotation(控制层,业务层,持久层) -- @Component、@Repository @Service、@Controller
- SpringMVC中web层controller的单元测试小demo
- SpringMVC在Controller层中注入request的坑
- 解决vs2015新建Android项目时,提示“未将对象引用设置到对象的实例”的问题
- 分析器错误消息: 未能加载文件或程序集“MySql.Web, Version=6.9.8.0, Culture=neutral, PublicKeyToken=c5687fc88969c44d”或..
- Swift UIViewController页面跳转汇总
- 面向接口编程与面向实现编程
- Caravel数据可视化平台环境搭建与使用
- springMVC中controller层调用service层的方式
- iServer性能提升介绍
- The document "(null)"requires Xcode 8.0问题的解决办法
- 商品秒杀功能思路实现
- 前端技术简介
- Leetcode 387 First Unique Character in a String
- 支付宝声波支付咻咻咻--原理简介
- 框架采用s2sm架构 ,处理apk上传下载, 处理图片上传下载,核心代码.
- 06上机练习3