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 ...
随机推荐
- Activiti工作流入门
Activiti简介 Activiti是一个开源的工作流引擎,它实现了BPMN 2.0规范,可以发布设计好的流程定义,并通过api进行流程调度. Activiti 作为一个遵从 Apache 许可的工 ...
- ap143 添加复位和重启按钮
1.修改匹配的文件mach-ap143.c 定义按键对应的GPIO(根据原理图来) #define AP143_GPIO_BTN_RESET 12 添加按钮的初始化消息 注册定时轮询按钮动作的函数 2 ...
- SQL语句获取数据库中的表主键,自增列,所有列
SQL语句获取数据库中的表主键,自增列,所有列 获取表主键 1:SELECT TABLE_NAME,COLUMN_NAME FROM INFORMATION_SCHEMA.KEY_COLUMN_U ...
- php相关书籍视频
虽然如今web领域,PHP JSP .NET 并驾齐驱,但PHP用的最广,原因不用我多说. 首先发一个PHP手册,方便查询,这个肯定是学PHP必备的. 下载地址:http://u.115.com/f ...
- Mysql的转义字符
Mysql的转义字符是"\",即反斜杠,在INSERT语句中,如果被插入的文本中包含反斜杠,那么反斜杠会被吃掉.例如: INSERT INTO tb (id,json) VALUE ...
- 【Python】Python中对象管理与垃圾回收中两个很有意思的问题
再Python中是利用引用计数来实现对象管理和垃圾回收的,即其他对象引用该对象时候,其引用计数加1,反之减1,当引用计数为0时候,被垃圾收集器回收. Python解释器对对象以及计数器的管理分为以下两 ...
- joda-time的一个DEMO
Date activeDate = person.getActiveTime(); if(activeDate==null){ modelMap.put("expireDate", ...
- react学习笔记-02
1.组件嵌套 React允许将代码封装成一个component,然后像html标签一样,插入网页中中. var HelloMessage = React.createClass({ render: f ...
- android studio的lib和jniLibs
在android studio 中添加jar和so时,将jar文件直接拷贝到 项目目录\app\libs下即可,将so文件按照平台分类目录放到 项目目录\app\src\main\jniLibs\平台 ...
- QT自绘标题和边框
在QT中如果想要自绘标题和边框,一般步骤是: 1) 在创建窗口前设置Qt::FramelessWindowHint标志,设置该标志后会创建一个无标题.无边框的窗口. 2)在客户区域的顶部创建一个自绘标 ...