热度最大的框架,它也称为业务层框架。Spring这个框架的诞生,给程序员揭示了两个主要的思想:Ioc,Aop;

  最近的网页架构可以分为这样。

  

  传统结构中,每个层都得new出依赖层的类进行一些本层操作,而Spring的出现使得这些层与层的依赖关系变得不那么紧致。所有的需要新建的类都由Spring的bean工厂提供。控制权限被反转了,所以称为控制反转(Inversion of Control)。

  而在业务层有很多模块,为了使每个模块更专注于自己的工作,并且不用考虑模块间的耦合,Spring提出了Aop的思想。类与类间的关系除了继承多态,没有定义平行的关系,而Aop弥补了类间关系,使得切面实现业务又丰富又简单。

  Ioc范例

  1. package IoC_AOP;
  2.  
  3. public interface Person {
  4. public void speak();
  5. }
  1. package IoC_AOP;
  2.  
  3. public class Chinese implements Person{
  4. private String name;
  5. private String age;
  6. public Chinese(){
  7.  
  8. }
  9. public Chinese(String name, String age) {
  10. super();
  11. this.name = name;
  12. this.age = age;
  13. }
  14.  
  15. public String getName() {
  16. return name;
  17. }
  18.  
  19. public void setName(String name) {
  20. this.name = name;
  21. }
  22.  
  23. public String getAge() {
  24. return age;
  25. }
  26.  
  27. public void setAge(String age) {
  28. this.age = age;
  29. }
  30.  
  31. @Override
  32. public void speak() {
  33. // TODO Auto-generated method stub
  34. System.out.println("I'm Chinese,my name is "+this.name+" ,my age is "+this.age);
  35.  
  36. }
  37.  
  38. }
  1. package IoC_AOP;
  2.  
  3. public class American implements Person{
  4. private String name;
  5. private String age;
  6. public American(){
  7.  
  8. }
  9. public American(String name, String age) {
  10. super();
  11. this.name = name;
  12. this.age = age;
  13. }
  14.  
  15. public String getName() {
  16. return name;
  17. }
  18. public void setName(String name) {
  19. this.name = name;
  20. }
  21. public String getAge() {
  22. return age;
  23. }
  24. public void setAge(String age) {
  25. this.age = age;
  26. }
  27. @Override
  28. public void speak() {
  29. // TODO Auto-generated method stub
  30. System.out.println("I'm American,my name is "+this.name+" ,my age is "+this.age);
  31. }
  32.  
  33. }
  1. public void IocTest(){
  2. Injection in = new Injection();
  3. Person p1 = in.proxy1("zhangsan", "18");
  4. Person p2 = in.proxy2("tom", "19");
  5. p1.speak();
  6. p2.speak();
  7. }

   Aop思想原理,通过使用动态代理来实现。

  1. package IoC_AOP;
  2.  
  3. public interface interface1 {
  4. public void doSomething();
  5. }
  1. package IoC_AOP;
  2.  
  3. public class subject implements interface1{
  4. public void doSomething(){
  5. System.out.println("hello i'm doing my jobs");
  6. }
  7. }
  1. package IoC_AOP;
  2.  
  3. import java.lang.reflect.InvocationHandler;
  4. import java.lang.reflect.Method;
  5.  
  6. public class proxySubject implements InvocationHandler{
  7.  
  8. private Object proxied;
  9. public proxySubject(Object proxied){
  10. this.proxied = proxied;
  11. }
  12. public void setProxied(Object proxied) {
  13. this.proxied = proxied;
  14. }
  15. @Override
  16. public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
  17. // TODO Auto-generated method stub
  18. //执行方法之前可以做一些操作
  19.  
  20. //执行目标方法
  21. method.invoke(proxied, args);
  22. //之后可以执行一些操作
  23. return null;
  24. }
  25.  
  26. }
  1. subject real = new subject();
  2. interface1 proxySubject = (interface1)Proxy.newProxyInstance(interface1.class.getClassLoader(),
  3. new Class[]{interface1.class},
  4. new proxySubject(real));
  5. proxySubject.doSomething();
  1. proxy:  指代我们所代理的那个真实对象
  2. method:  指代的是我们所要调用真实对象的某个方法的Method对象
  3. args:  指代的是调用真实对象某个方法时接受的参数

  Spring的配置

    下载spring-framework-3.2.0.RELEASE,在官网下载,然后再下载commons-logging-1.2。

    添加到buildpath。下载下来的包的lib下,同一个jar包有三份。

    

    只要导入jar就行。

    

    然后导入commons-loggin的jar包。

    src下新建applicationContext.xml配置文件。配置文件的bean代表加载一个类,通过Spring的工厂生产出来并注入到应用的类。    

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xmlns:context="http://www.springframework.org/schema/context"
  5. xmlns:tx="http://www.springframework.org/schema/tx"
  6. xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
  7. http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd
  8. http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd"
  9. default-autowire="byName" default-lazy-init="true">
  10. <bean id="chinese" class="Beans.Chinese">
  11. <property name="name">
  12. <value>sss</value>
  13. </property>
  14. <property name="age">
  15. <value>18</value>
  16. </property>
  17. </bean>
  18. <bean id="america" class="Beans.American">
  19. <property name="name">
  20. <value>xxx</value>
  21. </property>
  22. <property name="age">
  23. <value>19</value>
  24. </property>
  25. </bean>
  26.  
  27. </beans>

  书写范例 

  1. package Beans;
  2.  
  3. public interface Person {
  4. public void speak();
  5. }
  1. package Beans;
  2.  
  3. public class Chinese implements Person{
  4. private String name;
  5. private int age;
  6.  
  7. public String getName() {
  8. return name;
  9. }
  10.  
  11. public void setName(String name) {
  12. this.name = name;
  13. }
  14.  
  15. public int getAge() {
  16. return age;
  17. }
  18.  
  19. public void setAge(int age) {
  20. this.age = age;
  21. }
  22.  
  23. @Override
  24. public void speak() {
  25. // TODO Auto-generated method stub
  26. System.out.println("I'm Chinese,my name "+this.name+",my age "+this.age);
  27.  
  28. }
  29.  
  30. }
  1. package Beans;
  2.  
  3. public class American implements Person{
  4. private String name;
  5. private String age;
  6. public String getName() {
  7. return name;
  8. }
  9. public void setName(String name) {
  10. this.name = name;
  11. }
  12. public String getAge() {
  13. return age;
  14. }
  15. public void setAge(String age) {
  16. this.age = age;
  17. }
  18. @Override
  19. public void speak() {
  20. // TODO Auto-generated method stub
  21. System.out.println("I'm American,my name "+this.name+",my age "+this.age);
  22. }
  23.  
  24. }

  使用Junit包进行测试。  

  1. package JunitTest;
  2.  
  3. import org.junit.Test;
  4. import org.springframework.context.*;
  5. import org.springframework.context.support.ClassPathXmlApplicationContext;
  6.  
  7. import Beans.Person;
  8.  
  9. public class Test1 {
  10. @Test
  11. public void TestSpringMethod(){
  12. ApplicationContext context=new ClassPathXmlApplicationContext("applicationContext.xml");
  13. Person p1=(Person)context.getBean("chinese");
  14. p1.speak();
  15. p1=(Person)context.getBean("america");
  16. p1.speak();
  17.  
  18. }
  19. }

  时日尚浅,没有经验,日后加倍努力。

Spring浅探的更多相关文章

  1. OCR技术浅探:基于深度学习和语言模型的印刷文字OCR系统

    作者: 苏剑林 系列博文: 科学空间 OCR技术浅探:1. 全文简述 OCR技术浅探:2. 背景与假设 OCR技术浅探:3. 特征提取(1) OCR技术浅探:3. 特征提取(2) OCR技术浅探:4. ...

  2. OCR技术浅探(转)

    网址:https://spaces.ac.cn/archives/3785 OCR技术浅探 作为OCR系统的第一步,特征提取是希望找出图像中候选的文字区域特征,以便我们在第二步进行文字定位和第三步进行 ...

  3. font and face, 浅探Emacs字体选择机制及部分记录

    缘起 最近因为仰慕org-mode,从vim迁移到了Emacs.偶然发现org-mode中调出的calendar第一行居然没有对齐,排查一下发现是字体的问题.刚好也想改改Emacs的字体,于是我就开始 ...

  4. Spring浅入浅出——不吹牛逼不装逼

    Spring浅入浅出——不吹牛逼不装逼 前言: 今天决定要开始总结框架了,虽然以前总结过两篇,但是思维是变化的,而且也没有什么规定说总结过的东西就不能再总结了,是吧.这次总结我命名为浅入浅出,主要在于 ...

  5. Spring Boot2.X整合消息中间件RabbitMQ原理简浅探析

    目录 1.简单概述RabbitMQ重要作用 2.简单概述RabbitMQ重要概念 3.Spring Boot整合RabbitMQ 前言 RabbitMQ是一个消息队列,主要是用来实现应用程序的异步和解 ...

  6. 浅探SpringMVC中HandlerExecutionChain之handler、interceptor

    讲解HandlerExecutionChain之前,先大致了解下SpringMVC的核心开发步骤: 在web.xml中部署DispaterServlet,并配置springmvc.xml等文件; 将映 ...

  7. Java虚拟机浅探

    简介 对于java开发人员了来说,对java虚拟机肯定有着或多或少的了解.因为有了虚拟机的存在,才会使得java的内存管理变得那么方便,不再像C++那样使用new/delete来直接管理内存.知名的j ...

  8. C++虚函数浅探

    C++中和虚函数(Virtual Function)密切相关的概念是"动态绑定"(Dynamic Binding),与之相对的概念是"静态绑定"(Static ...

  9. 浅探委托(delegate)和事件(event)

    .NET Framework通过委托提供了一种回调函数机制. internal delegate void FeedBack(Int32 value); 内部委托FeedBack的声明,一个委托要指定 ...

随机推荐

  1. MapReduce编程示例

    1.将hadoop插件放入eclipse/plugins目录中 2.eclipse配置hadoop 依赖包目录 Window—Preferences 3.新建Map/Reduce Project项目 ...

  2. background-image 和 img

    一:解决div里面的img图像宽度不变,高度不变!   超出div部分设置隐藏! 图片:1920x526 div容器: 1423x526 1. background-image:样式实现 img: 标 ...

  3. 《Javascript DOM编程艺术》 读书笔记 —— 好书,通俗易懂!!!!! 相当的严谨!!!!

    1.javascript弱类型语言,解释性语言. 2.javascript数据类型:字符串(String).数字(Number).布尔(Boolean).数组(Array).对象(Object).空( ...

  4. 把php上传sae问题要使用IO

    应用移植指南 一,为什么要移植应用 SAE禁止IO写操作,代码目录不能写入.这意味着普通程序的上传图片.生成缓存等操作都不能在SAE上正常运行,这时候你需要对这些代码进行修改后才能让你的程序运行在SA ...

  5. stringBuffer拼接有规律字符串

    1. 拼接结果如下的字符串 1,2,3,4,5,6,7,8,9,10,11,12,12,12,12,34,234,2134,1234,1324,1234,123 2. 以前是这样想的,但是从效率,速度 ...

  6. Django - 获取请求方式

    //获取请过来得得请求类型 method = request.method 通过Django 的form 判断用户输入是否通过验证 check = forms.LoginFrom(request.PO ...

  7. git常用命令学习

    Git commands 1. start a working area clone Clone a repository into a new directory init Create an em ...

  8. 超级详细的iptable教程文档

    Iptabels是与Linux内核集成的包过滤防火墙系统,几乎所有的linux发行版本都会包含Iptables的功能.如果 Linux 系统连接到因特网或 LAN.服务器或连接 LAN 和因特网的代理 ...

  9. 【LintCode】链表求和

    问题分析: 我们通过遍历两个链表拿到每个位的值,两个值加上前一位进位值(0或者1)模10就是该位的值,除以10就是向高位的进位值(0或者1). 由于两个链表可以不一样长,所以要及时判断,一旦为null ...

  10. bzoj4402: Claris的剑

    首先,对于本质相同的构造,我们只计算字典序最小的序列 假设序列中最大的元素为top 我们很容易发现这样的序列一定是1,2,..,1,2,3,2,3,...,2,3,4,3,4.........,top ...