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工具及接口案例的更多相关文章

  1. 记录ABAP开发的日常——SAP_PO开发同步接口案例

    前言:在项目中遇到任务PO接口,需求是SRM发送采购订单信息给SAP,SAP根据信息调用BAPI同步数据,在此作为案例记录. 本次接口采用的协议是SOAP,当然也有其他的协议比如REST等等,在此不做 ...

  2. postman工具【接口自动化测试关于断言】

    在使用postman工具进行接口自动化时我们经常需要断言来进行判断,结果到底是成功还是失败. 但在collection runner/Newman里如果不加断言,跑完后都无法知道是成功还是失败 断言是 ...

  3. api接口测试工具和接口文档管理工具

    api接口测试工具和接口文档管理工具 1.postman(https://www.getpostman.com) Postman 是一个很强大的 API调试.Http请求的工具.她可是允许用户发送任何 ...

  4. 【接口工具】接口抓包工具之Charles

    上篇我们讲了Fiddler,Fiddler是用C#开发的,所以Fiddler不能在Mac系统中运行,没办法直接用Fiddler来截获MAC系统中的HTTP/HTTPS, Mac 用户怎么办呢? 1.F ...

  5. Yarn的Tool接口案例

    目录 Yarn的Tool接口案例 Tool接口环境准备 1 新建Maven项目YarnDemo 编写代码 打包jar上传到集群 Yarn的Tool接口案例 Tool接口环境准备 之前写wordcoun ...

  6. httprunner学习1-环境与登录接口案例

    前言 HttpRunner 是一款面向 HTTP(S) 协议的通用测试框架,只需编写维护一份 YAML/JSON 脚本,即可实现自动化测试. 具有以下优点: 继承 Requests 的全部特性,轻松实 ...

  7. postman工具测试接口

    本篇文章主要介绍怎么在postman工具中进行接口的测试? 从以下几个方面进行介绍: 1.先介绍下接口测试 2.不同类型的接口请求方式如何在postman中进行测试 1.1 接口 什么是接口? 接口一 ...

  8. postman接口案例

    接口测试 什么是接口(API) API全称Application Programming Interface,这里面我们其实不用去关注AP,只需要I上就可以.一个API就是一个Interface.我们 ...

  9. Spring-Boot ☞ ShapeFile文件读写工具类+接口调用

    一.项目目录结构树 二.项目启动 三.往指定的shp文件里写内容 (1) json数据[Post] { "name":"test", "path&qu ...

随机推荐

  1. jquery跨域方法

    $.ajax({ type: 'get', dataType: "jsonp",//支持跨域 jsonp: "callback", jsonpCallback: ...

  2. 判断是否为AVL树

    时间复杂度:O(n) // 判断是否为AVL树 public int isAVL(TreeNode node) { if (node == null) { return 0; } int left = ...

  3. 第80节:Java中的MVC设计模式

    第80节:Java中的MVC设计模式 前言 了解java中的mvc模式.复习以及回顾! 事务,设置自动连接提交关闭. setAutoCommit(false); conn.commit(); conn ...

  4. Python学习笔记【第四篇】:基本数据类型

    变量:处理数据的状态 变量名 = 状态值 类型 python中有以下基本数据类型: 1:整形 2:字符串类型 3:Bool类型 4:列表 5:元祖(不可变) 6:字典(无序) 7:集合 (无序.不重复 ...

  5. 简单读!tomcat源码(一)启动与监听

    tomcat 作为知名的web容器,很棒! 本文简单了从其应用命令开始拆解,让我们对他有清晰的了解,揭开神秘的面纱!(冗长的代码流水线,给你一目了然) 话分两头: 1. tomcat是如何启动的? 2 ...

  6. ubuntu系统给用户增加root权限

    在安装软件包的时候,会提示没有sudo权限. eg : sudo apt-get install golang 解决方法 进入有root权限的用户 eg: su root 并根据提示输入密码 sudo ...

  7. 2.matplotlib画散点图

    2.1.身高和体重实例 import matplotlib.pyplot as plt height = [161,162,163,164,165] weight = [50,60,70,80,90] ...

  8. 《JQuery技术内幕》读书笔记——自调用匿名函数剖析

    Javascript语言中的自调用匿名函数格式如下: (function(){ //do somethings })(); 它还有另外两种等价写法如下: //等价写法一 (function(){ // ...

  9. sql server 高可用故障转移(5)

    测试故障转移群集报告 在SQL-CL01(hsr 50)进行故障转移群集的创建,如图下图所示,在SQL-CL01和SQL-CL02的“服务器管理”中右键点击“功能”,选择“添加功能 勾选故障转移群集  ...

  10. sql server 索引阐述系列三 表的堆组织

    一.   概述 这一节来详细介绍堆组织,通过讲解堆的结构,堆与非聚集索引的关系,堆的应用场景,堆与聚集索引的存储空间占用,堆的页拆分现象,最后堆的使用建议 ,这几个维度来描述堆组织.在sqlserve ...