我的Spring之旅(三):重构业务逻辑
来源:互联网 发布:阿里云绑定域名教程 编辑:程序博客网 时间:2024/05/18 03:56
1、前言
增加业务逻辑层,将控制器中的业务逻辑转移到业务逻辑层。
2、HelloService
使用@Service注解,将类声明为服务。
将HelloController中的sayhi和sayhello函数转移到该类中:
package com.nextgame.service;import org.springframework.stereotype.Service;import net.sf.json.JSONObject;@Servicepublic class HelloService {/* * API:sayhello */ public JSONObject sayhello(String name) { JSONObject obj = new JSONObject(); obj.put("msg", "hello," + name +"!"+"from service"); return obj; } /* * API:sayhi */ public JSONObject sayhi(String name) { JSONObject obj = new JSONObject(); obj.put("msg", "hi," + name +"!"+"from service"); return obj; }}
3、调用服务
使用@Autowired进行自动装配:
@Autowiredprivate HelloService helloService;
使用helloService调用服务方法:
if(method.equals("sayhello")){obj = helloService.sayhello(name);}else if(method.equals("sayhi")){obj = helloService.sayhi(name);}
4、配置服务
在WEB-INF目录下新建applicationContext.xml:
<?xml version="1.0" encoding="UTF-8" ?><beans xmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"xmlns:context="http://www.springframework.org/schema/context"xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.1.xsd"> <!-- 扫描类包,将标注Spring注解的类自动转化Bean,同时完成Bean的注入 --> <context:component-scan base-package="com.nextgame.service"/></beans>
配置web.xml:
<context-param> <param-name>contextConfigLocation</param-name> <param-value>/WEB-INF/dispatcher-servlet.xml,/WEB-INF/applicationContext.xml</param-value> </context-param>
5、运行
0 0
- 我的Spring之旅(三):重构业务逻辑
- 你需要的不是重构,而是理清业务逻辑
- 你需要的不是重构,而是理清业务逻辑
- 你需要的不是重构,而是理清业务逻辑
- 你需要的不是重构,而是理清业务逻辑
- 你需要的不是重构,而是理清业务逻辑
- 你需要的不是重构,而是理清业务逻辑
- 你需要的不是重构,而是理清业务逻辑
- 网络五子棋的架构设计(三)--业务逻辑
- 编写你的业务逻辑与实现逻辑验证 (phpsa系列教程之三)
- 搭建你的Spring.Net+Nhibernate+Asp.Net Mvc 框架 (三)实现数据库接口层和业务逻辑层
- Django框架之views(业务逻辑)简单后台首页展示的业务逻辑详细描述
- 什么是业务逻辑层(业务层)及业务逻辑层(业务层)的功能
- Django框架之views(业务逻辑)简单后台登录与退出的业务逻辑详细描述
- 菜鸟级三层框架(EF+MVC)项目实战之 系列三 业务逻辑层封装
- 搭建你的Spring.Net+Nhibernate+Asp.Net Mvc 框架 (三)实现数据库接口层和业务逻辑层(转)
- csla之业务逻辑
- 【SSH进阶之路】一步步重构MVC实现Struts框架——封装业务逻辑和跳转路径(四)
- 火天大有
- 自定义String类
- 指针初步学习,运用指针进行冒泡排序,选择排序,插入排序和折半查找
- C#窗体之整人小程序
- keepalived双机热备实现故障时发送邮件通知
- 我的Spring之旅(三):重构业务逻辑
- java集合(ArrayList、vector、HashMap、HashTable)源码剖析
- HDU 2081 手机短号 java
- poj 2251 (bfs)
- 在windows上搭建C语言开发环境——借助eclipse和MinGW
- Scanf函数的一些小细节
- poj 2524 Ubiquitous Religions
- Android开发之MediaPlayer音频视频播放
- hdoj 1754 I Hate It(线段树)