spring_02工具及接口案例
1.spring工具类:ApplicationContextUtil.java,可以返回加载配置文件的容器对象
package com.ahd.utils; import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; public class ApplicationContextUtil {
static ApplicationContext ac=null; //创建私有无参构造方法
private ApplicationContextUtil(){} static{
ac=new ClassPathXmlApplicationContext("beans.xml");
}
public static ApplicationContext getApplicationContext(){
return ac;
}
}
2.spring开发提倡接口编程,配合di技术可以层与层的解耦,通过接口, 配置文件可以很容易把一个类信息改变成另一个类
3.思路
1).创建一个接口ValidateUser
2).两个类实现接口,
3).把对象配置到spring容器中
4).使用(通过接口来获取getBean获得的对象)
4.具体实现
接口ValidateUser
package com.ahd.service;
public interface ValidateUser {
public void check();
}
实现接口的类CheckUser1
package com.ahd.serviceImpl;
import com.ahd.service.ValidateUser;
public class CheckUser1 implements ValidateUser{
private String username;
private String password;
@Override
public void check() {
System.out.println(username+password);
System.out.println("通过xml验证成功");
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}
实现接口的类CheckUser2
package com.ahd.serviceImpl;
import com.ahd.service.ValidateUser;
public class CheckUser2 implements ValidateUser{
private String username;
private String password;
@Override
public void check() {
System.out.println(username+password);
System.out.println("通过数据库验证成功");
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}
配置文件beans.xml,配置的两个bean对象可以相互替换(注释的和没有注释的)
<?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:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">
<!-- 在容器文件中配置数据源(bean/entity/service/dao/pojo) -->
<!-- bean元素的作用是当spring框架加载的时候,spring会自动为bean类 UserService类的属性设置值 id就是创建对象的对象名 -->
<!--
<bean id="validateUser" class="com.ahd.serviceImpl.CheckUser1">
<property name="username">
<value>爱华顿g</value>
</property>
<property name="password" value="123456"></property>
</bean>
-->
<bean id="validateUser" class="com.ahd.serviceImpl.CheckUser2">
<property name="username">
<value>爱华顿g</value>
</property>
<property name="password" value="123456"></property>
</bean> </beans>
测试代码Test
package com.ahd.test; import static org.junit.Assert.*; import com.ahd.service.ValidateUser;
import com.ahd.utils.ApplicationContextUtil; public class Test { @org.junit.Test
public void test() {
//使用接口实现
ValidateUser vu=(ValidateUser) ApplicationContextUtil.getApplicationContext().getBean("validateUser");
vu.check();
} }
spring_02工具及接口案例的更多相关文章
- 记录ABAP开发的日常——SAP_PO开发同步接口案例
前言:在项目中遇到任务PO接口,需求是SRM发送采购订单信息给SAP,SAP根据信息调用BAPI同步数据,在此作为案例记录. 本次接口采用的协议是SOAP,当然也有其他的协议比如REST等等,在此不做 ...
- postman工具【接口自动化测试关于断言】
在使用postman工具进行接口自动化时我们经常需要断言来进行判断,结果到底是成功还是失败. 但在collection runner/Newman里如果不加断言,跑完后都无法知道是成功还是失败 断言是 ...
- api接口测试工具和接口文档管理工具
api接口测试工具和接口文档管理工具 1.postman(https://www.getpostman.com) Postman 是一个很强大的 API调试.Http请求的工具.她可是允许用户发送任何 ...
- 【接口工具】接口抓包工具之Charles
上篇我们讲了Fiddler,Fiddler是用C#开发的,所以Fiddler不能在Mac系统中运行,没办法直接用Fiddler来截获MAC系统中的HTTP/HTTPS, Mac 用户怎么办呢? 1.F ...
- Yarn的Tool接口案例
目录 Yarn的Tool接口案例 Tool接口环境准备 1 新建Maven项目YarnDemo 编写代码 打包jar上传到集群 Yarn的Tool接口案例 Tool接口环境准备 之前写wordcoun ...
- httprunner学习1-环境与登录接口案例
前言 HttpRunner 是一款面向 HTTP(S) 协议的通用测试框架,只需编写维护一份 YAML/JSON 脚本,即可实现自动化测试. 具有以下优点: 继承 Requests 的全部特性,轻松实 ...
- postman工具测试接口
本篇文章主要介绍怎么在postman工具中进行接口的测试? 从以下几个方面进行介绍: 1.先介绍下接口测试 2.不同类型的接口请求方式如何在postman中进行测试 1.1 接口 什么是接口? 接口一 ...
- postman接口案例
接口测试 什么是接口(API) API全称Application Programming Interface,这里面我们其实不用去关注AP,只需要I上就可以.一个API就是一个Interface.我们 ...
- Spring-Boot ☞ ShapeFile文件读写工具类+接口调用
一.项目目录结构树 二.项目启动 三.往指定的shp文件里写内容 (1) json数据[Post] { "name":"test", "path&qu ...
随机推荐
- st表(poj3264)
st表很像线段树,但线段树既能查询和修改,而st表只能查询. 首先我们先用二维数组建立一个表,st[i][j]表内存的是从第i位开始1<<j范围内的best(st[i][j-1],st[i ...
- [转] Vmware vs Virtualbox vs KVM vs XEN: virtual machines performance comparison
http://www.ilsistemista.net/index.php/virtualization/1-virtual-machines-performance-comparison.html? ...
- Javascript高级编程学习笔记(35)—— DOM(1)节点
DOM JS由三部分组成 1.BOM 2.DOM 3.ECMAScript ES和BOM在前面的文章已经介绍过了 今天开始JS组成的最后一部分DOM(文档对象模型) 我们知道,JS中的这三个部分实际上 ...
- Python爬虫3-parse编码与利用parse模拟post请求
GitHub代码练习地址:①利用parse模拟post请求:https://github.com/Neo-ML/PythonPractice/blob/master/SpiderPrac04_pars ...
- Docker学习笔记-Docker for Windows 安装
前言: 环境:windows10专业版 64位 正文: 官方下载地址:https://hub.docker.com/editions/community/docker-ce-desktop-windo ...
- Ubuntu16.04.1 安装Nginx
Nginx ("engine x") 是一个高性能的 HTTP 和 反向代理 服务器,也是一个 IMAP/POP3/SMTP 代理服务器. Nginx 是由 Igor Sysoev ...
- 第75节:Java的中的JSP,EL和JSTL
第75节:Java中的JSP,EL和JSTL 哭吧看不完的!!! Cookie和`Session 请求转发和重定向的区别: 地址不一样 请求次数也不一样 数据无法传递 4.跳转范围有限制 效率 请求转 ...
- jQuery文档操作
jQuery文档操作 1.jq文档结构 var $sup = $('.sup'); $sup.children(); // sup所有的子级们 $sup.parent(); // sup的父级(一个, ...
- editormd实现文章详情页面预览
继之前博客写了editmd.js(国内开源的一款前端Markdown框架)实现的写文章功能之后,本博客介绍使用editormd实现文章预览功能,之前博客链接:https://blog.csdn.net ...
- Python中的算数运算
算数运算符 计算机,顾名思义就是负责进行 数学计算 并且 存储计算结果 的电子设备 目标 算术运算符的基本使用 01. 算数运算符 算数运算符是 运算符的一种 是完成基本的算术运算使用的符号,用来处理 ...