Spring入门学习(一)
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入门学习(一)的更多相关文章
- Spring入门学习(一)
SpringMVC基础平台补充(2016.03.03) 如果想要开发SpringMVC,那么前期依次安装好:JDK(jdk-8u74-windows-x64,安装后配置环境变量JAVA_HOME和CL ...
- [Spring入门学习笔记][静态资源]
遗留问题 在上一节课的作业中,我们一定遇到了一点问题——虽然将页面内容正确的返回给了浏览器,但是浏览器显示的样式却是不正确的,这是因为在HTML的\标签中我们这样引入了CSS资源: <link ...
- [Spring入门学习笔记][创建网站URL]
设计网站的URL 现代的Web站点都会设计一套拥有明确意义,方便用户记忆的URL,不论是域名还是路径,以天码营为例: http://tianmaying.com/courses表示网站下所有的课程列表 ...
- [spring入门学习笔记][spring的IoC原理]
什么叫IoC 控制反转(Inversion of Control,缩写为IoC),是面向对象编程中的一种设计原则,可以用来减低计算机代码之间的耦合度.其中最常见的方式叫做依赖注入(Dependency ...
- Spring入门学习推荐
该作者的 spring 博客很好,作为学习用 简介 https://blog.csdn.net/column/details/15088.html?&page=1 https://blog.c ...
- [Spring入门学习笔记][Spring的AOP原理]
AOP是什么? 面向切面编程 软件工程有一个基本原则叫做“关注点分离”(Concern Separation),通俗的理解就是不同的问题交给不同的部分去解决,每部分专注于解决自己的问题.这年头互联网也 ...
- [Spring入门学习笔记][Spring Boot]
什么是Spring Boot Spring Boot正是在这样的一个背景下被抽象出来的开发框架,它本身并不提供Spring框架的核心特性以及扩展功能,只是用于快速.敏捷地开发新一代基于Spring框架 ...
- Spring入门学习(二)三种实例化bean的方法
前面的哪一种就是通过构造函数来实例化对象 下面我们可能用到工厂方法来视力话对象,这样我们的配置文件又该怎么配置呢 <bean name="service2" class=&q ...
- Spring入门学习笔记(4)——JDBC的使用
目录 Spring JDBC框架概览 JdbcTemplate类 配置数据源 数据访问对象(Data Access Object,DAO) 执行SQL命令 Spring JDBC框架概览 使用传统的J ...
随机推荐
- kmp//呵呵!看毛片算法
以前刚学的时候迷迷糊糊的,一看就懵圈,前几天捡起来的时候 发现还不会 于是研究了两天,自尊心严重受挫,今天的时候 突然一道灵光迸发,居然 感觉好像懂了,于是又琢磨起来 终于 我懂了 呵呵! ...
- android app安全问题设置
1.应用签名未校验风险:检测 App 程序启动时是否校验签名证书. 2.应用数据任意备份风险 Android 2.1 以上的系统可为 App 提供应用程序数据的备份和恢复功能,该 由 AndroidM ...
- sphinx cmd command
D:\iso\gaoqiao\app\sphinx\bin\indexer.exe -c D:\iso\gaoqiao\app\sphinx\bin\sphinx.conf --all --rotat ...
- shell实现四则运算简单方法
在刚刚学习写shell 批处理时候,进行逻辑运算中,少不了需要进行基础的:四则运算,这里说说在linux shell 里面简单的实现方法.1.简单方法$ b=$((5*5+5-3/2)) $ echo ...
- 【IE6的疯狂之十二】一个display:none引起的3像素的BUG
今天同事给我看了一个display:none引起的3像素的BUG,非常奇怪!从来没碰到过display:none还能引起这种bug. 看代码: <div style="width: ...
- live555—VS2010/VS2013 下live555编译、使用及测试(转载)
Ⅰ live555简介 Live555 是一个为流媒体提供解决方案的跨平台的C++开源项目,它实现了对标准流媒体传输协议如RTP/RTCP.RTSP.SIP等 的支持.Live555实现了对多种音视频 ...
- eclipse 安卓运行一直跳转到调试模式
最直接的办法,换手机试试,如果在其他手机上能run,那就重启手机即可 感觉写这么一点点似乎不足以发表一篇博客,给个链接吧:http://www.itnose.net/detail/6103213.ht ...
- [Q]将图纸转换为JPG、PNG、plt、DWF、DWFx,XPS等格式文件
如要将图纸打印为图片,请选择“PublishToWeb JPG.pc3”或“PublishToWeb PNG.pc3”打印机. 如要将图纸打印为plt格式文件,请选择“Windows Default ...
- UltraISO PE(软碟通) V9.5.5.2960 官方中文版
软件名称: UltraISO PE(软碟通)软件语言: 简体中文授权方式: 免费试用运行环境: Win7 / Vista / Win2003 / WinXP 软件大小: 1.9MB图片预览: 软件简介 ...
- B/S和C/S【转载Jane的博客 http://blog.sina.com.cn/liaojane】
什么是C/S和B/S结构? C/S又称Client/Server或客户/服务器模式.服务器通常采用高性能的PC.工作站或小型机,并采用大型数据库系统,如Oracle.Sybase.In ...