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

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

  

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

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

  Ioc范例

package IoC_AOP;

public interface Person {
public void speak();
}
package IoC_AOP;

public class Chinese implements Person{
private String name;
private String age;
public Chinese(){ }
public Chinese(String name, String age) {
super();
this.name = name;
this.age = age;
} public String getName() {
return name;
} public void setName(String name) {
this.name = name;
} public String getAge() {
return age;
} public void setAge(String age) {
this.age = age;
} @Override
public void speak() {
// TODO Auto-generated method stub
System.out.println("I'm Chinese,my name is "+this.name+" ,my age is "+this.age); } }
package IoC_AOP;

public class American implements Person{
private String name;
private String age;
public American(){ }
public American(String name, String age) {
super();
this.name = name;
this.age = age;
} public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAge() {
return age;
}
public void setAge(String age) {
this.age = age;
}
@Override
public void speak() {
// TODO Auto-generated method stub
System.out.println("I'm American,my name is "+this.name+" ,my age is "+this.age);
} }
    public void IocTest(){
Injection in = new Injection();
Person p1 = in.proxy1("zhangsan", "18");
Person p2 = in.proxy2("tom", "19");
p1.speak();
p2.speak();
}

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

package IoC_AOP;

public interface interface1 {
public void doSomething();
}
package IoC_AOP;

public class subject implements interface1{
public void doSomething(){
System.out.println("hello i'm doing my jobs");
}
}
package IoC_AOP;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method; public class proxySubject implements InvocationHandler{ private Object proxied;
public proxySubject(Object proxied){
this.proxied = proxied;
}
public void setProxied(Object proxied) {
this.proxied = proxied;
}
@Override
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
// TODO Auto-generated method stub
//执行方法之前可以做一些操作 //执行目标方法
method.invoke(proxied, args);
//之后可以执行一些操作
return null;
} }
        subject real = new subject();
interface1 proxySubject = (interface1)Proxy.newProxyInstance(interface1.class.getClassLoader(),
new Class[]{interface1.class},
new proxySubject(real));
proxySubject.doSomething();
proxy:  指代我们所代理的那个真实对象
method:  指代的是我们所要调用真实对象的某个方法的Method对象
args:  指代的是调用真实对象某个方法时接受的参数

  Spring的配置

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

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

    

    只要导入jar就行。

    

    然后导入commons-loggin的jar包。

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

<?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"
default-autowire="byName" default-lazy-init="true">
<bean id="chinese" class="Beans.Chinese">
<property name="name">
<value>sss</value>
</property>
<property name="age">
<value>18</value>
</property>
</bean>
<bean id="america" class="Beans.American">
<property name="name">
<value>xxx</value>
</property>
<property name="age">
<value>19</value>
</property>
</bean> </beans>

  书写范例 

package Beans;

public interface Person {
public void speak();
}
package Beans;

public class Chinese  implements Person{
private String name;
private int age; public String getName() {
return name;
} public void setName(String name) {
this.name = name;
} public int getAge() {
return age;
} public void setAge(int age) {
this.age = age;
} @Override
public void speak() {
// TODO Auto-generated method stub
System.out.println("I'm Chinese,my name "+this.name+",my age "+this.age); } }
package Beans;

public class American implements Person{
private String name;
private String age;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAge() {
return age;
}
public void setAge(String age) {
this.age = age;
}
@Override
public void speak() {
// TODO Auto-generated method stub
System.out.println("I'm American,my name "+this.name+",my age "+this.age);
} }

  使用Junit包进行测试。  

package JunitTest;

import org.junit.Test;
import org.springframework.context.*;
import org.springframework.context.support.ClassPathXmlApplicationContext; import Beans.Person; public class Test1 {
@Test
public void TestSpringMethod(){
ApplicationContext context=new ClassPathXmlApplicationContext("applicationContext.xml");
Person p1=(Person)context.getBean("chinese");
p1.speak();
p1=(Person)context.getBean("america");
p1.speak(); }
}

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

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. XML-RPC远程方法调用

    一.简介 XML-RPC的全称是XML Remote Procedure Call,即XML远程方法调用. 它是一套允许运行在不同操作系统.不同环境的程序实现基于Internet过程调用的规范和一系列 ...

  2. 安装win10

    1.百度win10,看到的大都是雨林木风,ghost等江湖杂牌非原版系统.百度”msdn,我告诉你“进入微软MSDN下载中心(原来还有这么个好地方,以后就从这里下了),下载链接是ed2k格式的链接(e ...

  3. 1121高性能MySQL之运行机制

    本文来自于拜读<高性能MySQL(第三版)>时的读书笔记作者:安明哲转载时请注明部分内容来自<高性能MySQL(第三版)> MySQL的逻辑构架 MySQL服务器逻辑架构 最上 ...

  4. 《Android性能优化》学习笔记链接<转载>

    今天找到一博文汇总了 Android性能优化 比较好的文章 ,本计划全看完,自己再精简下,因篇幅太长,先收藏了,等有时间 再仔细拜读,总结自己的看法:  第一季: http://www.csdn.ne ...

  5. 开发错误记录1:解决:Only the original thread that created a view hierarchy can touch its views.

    今天在项目中要使用圆角头像,导入开源 CircleImageView ,然后setImageBitmap()时 运行时就会发现,它会报一个致命性的异常:: · ERROR/AndroidRuntime ...

  6. Bitmap二次采样(处理图片过大的问题)

    private Bitmap createImageThumbnail(String filePath, int newHeight, int newWidth) { BitmapFactory.Op ...

  7. 【POJ 1523】SPF(割点)

    儿子数大于1的树根或者 Low[v] >= DFN[u]的非树根节点v 就是割点. #include <cstdio> #include <cstring> const ...

  8. Xcode找不到模拟器出现"My Mac"

    问题如图: 步骤一. 找到target->built settings->Architectures->Base SDK, 选择你需要的版本;如果还是不行,看步骤二. 步骤二. 1) ...

  9. CAEmitterLayer 粒子发射Layer的相关属性

    emitterCells:CAEmitterCell对象的数组,被用于把粒子投放到layer上 birthRate:可以通俗的理解为发射源的个数,默认1.0.当前每秒产生的真实粒子数为=CAEmitt ...

  10. bbs树形打印(一)

    前言:大家在bbs回帖时常常可以看到树形的回复形式. dfs设计 (1) 为使得Connection仅打开一次,因此以conn作为其中一个递归参数,在递归全程不关闭conn; (2)根据存入数据的树状 ...