Spring的主要功能是控制反转和面向切面编程,下面我们就来编写第一个spring的程序来体验一下控制反转

首先是加载配置文件

<?xml version="1.0" encoding="UTF-8"?>
<!--
- Middle tier application context definition for the image database.
-->
<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"> </beans>

下面我们在程序中加载配置文件

ApplicationContext ac = new ClassPathXmlApplicationContext("spring.xml");

然后新建类

package com.service.impl;

import com.service.Service;

public class ServiceBean implements Service {

@Override
public void save(){
System.out.println("save()");
}
}

抽取出类的接口 refactor----->extract interface

然后在测试方法中控制反转

package junit.test;

import static org.junit.Assert.*;

import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; import com.service.Service; public class SpringTest { @Test
public void test() {
ApplicationContext ac = new ClassPathXmlApplicationContext("spring.xml");
Service service = (Service)ac.getBean("service");
service.save();
} }

下面我们来看看是怎么控制反转的吧

首先新建类来解析配置文件,然后把配置文件中的bean中的属性提取出来创建对象,通过相应的方法刚返回给页面

 package com.dom4j.test;

 import java.io.File;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set; import org.dom4j.Attribute;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.dom4j.io.SAXReader; import com.service.Service;
import com.sun.beans.decoder.DocumentHandler; public class DomeStuTest { private Map<String,String> map = new HashMap<String,String>();
private Map<String,Object> objMap = new HashMap<String,Object>(); public DomeStuTest(String xml){
String xmlPath = getClass().getClassLoader().getResource(xml).getPath();
Document document = null;
SAXReader reader = new SAXReader(); try {
document = reader.read(new File(xmlPath));
} catch (DocumentException e) {
e.printStackTrace();
} //获取根节点
Element root = document.getRootElement(); //获取根节点下的子节点
Iterator it = root.elementIterator("bean");
while(it.hasNext()){
Element ele = (Element)it.next();
Attribute attr_name = ele.attribute("name");
Attribute attr_class = ele.attribute("class"); map.put(attr_name.getValue(), attr_class.getValue());
} //实例化容器内的对象并存储
Set<String> set = map.keySet();
Iterator its = set.iterator();
while(its.hasNext()){
String key = (String)its.next();
String value = map.get(key);
try {
Class clazz = Class.forName(value);
Object obj = clazz.newInstance();
objMap.put(key, obj);
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (InstantiationException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} } } //取得对象
public Object get(String name){
Object obj = null;
Set<String> set = objMap.keySet();
Iterator it = set.iterator();
while(it.hasNext()){
String key = (String)it.next();
if(key.equals(name)){
obj = objMap.get(key);
}
}
return obj;
} }

然后在页面这样就可以获得对象

package junit.test;

import org.junit.Test;

import com.dom4j.test.DomeStuTest;
import com.service.Person;
import com.service.Service; public class SpringTest { @Test
public void test() {
// ApplicationContext ac = new ClassPathXmlApplicationContext("spring.xml");
// Service service = (Service)ac.getBean("service");
// service.save(); DomeStuTest dst = new DomeStuTest("spring.xml");
Service service = (Service)dst.get("service");
service.save();
Person person = (Person)dst.get("person");
person.showMessage();
} }

Spring入门学习(一)的更多相关文章

  1. Spring入门学习(一)

    SpringMVC基础平台补充(2016.03.03) 如果想要开发SpringMVC,那么前期依次安装好:JDK(jdk-8u74-windows-x64,安装后配置环境变量JAVA_HOME和CL ...

  2. [Spring入门学习笔记][静态资源]

    遗留问题 在上一节课的作业中,我们一定遇到了一点问题——虽然将页面内容正确的返回给了浏览器,但是浏览器显示的样式却是不正确的,这是因为在HTML的\标签中我们这样引入了CSS资源: <link ...

  3. [Spring入门学习笔记][创建网站URL]

    设计网站的URL 现代的Web站点都会设计一套拥有明确意义,方便用户记忆的URL,不论是域名还是路径,以天码营为例: http://tianmaying.com/courses表示网站下所有的课程列表 ...

  4. [spring入门学习笔记][spring的IoC原理]

    什么叫IoC 控制反转(Inversion of Control,缩写为IoC),是面向对象编程中的一种设计原则,可以用来减低计算机代码之间的耦合度.其中最常见的方式叫做依赖注入(Dependency ...

  5. Spring入门学习推荐

    该作者的 spring 博客很好,作为学习用 简介 https://blog.csdn.net/column/details/15088.html?&page=1 https://blog.c ...

  6. [Spring入门学习笔记][Spring的AOP原理]

    AOP是什么? 面向切面编程 软件工程有一个基本原则叫做“关注点分离”(Concern Separation),通俗的理解就是不同的问题交给不同的部分去解决,每部分专注于解决自己的问题.这年头互联网也 ...

  7. [Spring入门学习笔记][Spring Boot]

    什么是Spring Boot Spring Boot正是在这样的一个背景下被抽象出来的开发框架,它本身并不提供Spring框架的核心特性以及扩展功能,只是用于快速.敏捷地开发新一代基于Spring框架 ...

  8. Spring入门学习(二)三种实例化bean的方法

    前面的哪一种就是通过构造函数来实例化对象 下面我们可能用到工厂方法来视力话对象,这样我们的配置文件又该怎么配置呢 <bean name="service2" class=&q ...

  9. Spring入门学习笔记(4)——JDBC的使用

    目录 Spring JDBC框架概览 JdbcTemplate类 配置数据源 数据访问对象(Data Access Object,DAO) 执行SQL命令 Spring JDBC框架概览 使用传统的J ...

随机推荐

  1. [MFC美化] SkinSharp使用详解1-使用方法

    需要注意的是: 加载换肤后,程序关闭时,无需调用卸载皮肤接口,SkinSharp换肤库会自己完成清理工作.换肤时无需卸载皮肤,直接调用即可. 其DLL使用方法与前面几种皮肤库用法类似.如下: 1. 将 ...

  2. 用ES6语法和方式写gulp

    安装依赖模块 npm i -g gulp npm i gulp babel-core babel-preset-es2015 --save-dev 在创建文件 .babelrc(文件名) : (文件内 ...

  3. FaceBook页面加载技术

    1. 技术背景 FaceBook页面加载技术 试想这样一个场景,一个经常访问的网站,每次打开它的页面都要要花费6 秒:同时另外一个网站提供了相似的服务,但响应时间只需3 秒,那么你会如何选择呢?数据表 ...

  4. CodeForces 670E Correct Bracket Sequence Editor

    链表,模拟. 写一个双向链表模拟一下过程. #pragma comment(linker, "/STACK:1024000000,1024000000") #include< ...

  5. CodeForces 689E Mike and Geometry Problem

    离散化,树状数组,组合数学. 这题的大致思路和$HDU$ $5700$一样.都是求区间交的问题.可以用树状数组维护一下. 这题的话只要计算每一个$i$被统计了几次,假设第$i$点被统计了$ans[i] ...

  6. etcd 集群搭建

    现有三台机器 CentOS7 node1 10.2.0.10 node2 10.2.0.11 node3 10.2.0.12  1 源码解压命令行方式 node1 ./etcd --name infr ...

  7. 《HTML5权威指南》

    <HTML5权威指南> HTML元素: html字符实体 html全局属性 html base标签 用元数据元素说明文档 标记文字(第八章) 标记文字.组织内容.文档分节 表格元素 表单元 ...

  8. window下安装apache---使用wamp

    01 wamp-server-wamp5-2-5-multi-win.exe 02 wamp报错时,需要的补丁(vcredist_x64.exe) 无法启动此程序,因为计算机中丢失MSVCR110.d ...

  9. linux kernel module

    #include <linux/init.h>#include <linux/module.h>#include <linux/kernel.h> static i ...

  10. ubuntu 14.04—解决软件中心进度条卡死的问题

    软件中心下载安装软件进度条卡住了,这时候解决方法为: 先解锁: sudo rm -rf /var/lib/dpkg/lock 如果此时开启软件中心,发现进度还在, 那么我们需要找到相关的进程关闭他,使 ...