版权声明

  笔记出自《Spring 开发指南》一书。

Spring 初探

  前面我们简单介绍了 Spring 的基本组件和功能,现在我们来看一个简单示例:

  1. Person接口
    Person接口定义了一个 say 的方法,在其不同的实现中实现了各自的 say 逻辑。

     /**
      * @author X
      */
     public interface Person {
    
         public void say();
     }
  2. Person接口的两个实现
     /**
      * @author X
      */
     public class Man implements Person {
    
         private String word;
    
         public String getWord() {
             return word;
         }
    
         public void setWord(String word) {
             System.out.println("执行了Set");
             this.word = word;
         }
    
         @Override
         public void say() {
             System.out.println("一个男人委屈的说:" + word);
         }
     }
     /**
      * @author X
      */
     public class Woman implements Person {
    
         private String word;
    
         public String getWord() {
             return word;
         }
    
         public void setWord(String word) {
             this.word = word;
         }
    
         @Override
         public void say() {
             System.out.println("一个女人委屈的说:" + word);
         }
     }
  3. Spring配置文件(bean.xml)
     <?xml version="1.0" encoding="UTF-8"?>
     <beans xmlns="http://www.springframework.org/schema/beans"
            xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
            xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
    
         <bean id="TheAction" class="Man">
             <property name="word">
                 <value>一百块钱都不给我还打我</value>
             </property>
         </bean>
     </beans>
  4. 调用代码
     import org.springframework.context.ApplicationContext;
     import org.springframework.context.support.FileSystemXmlApplicationContext;
    
     /**
      * @author X
      */
     public class Run {
         public static void main(String[] args) {
    
             ApplicationContext ac = new FileSystemXmlApplicationContext("D:\\Study\\study-spring\\src\\main\\resources\\bean.xml");
    
             Person action = (Person) ac.getBean("TheAction");
             action.say();
         }
     }

    运行结果如下图所示:

     

  怎么样,是不是很简单?通过这个简单的例子,我们可以看到,我们在实例化Person的时候,根本无需知道它是如何实现的。在编码中,我们只需要去关心接口就可以了,而无需关心它的创建过程,因为这件事 Spring 已经帮我们做掉了。如果你想了解更多,推荐你去了解控制反转(IOC)和依赖注入(DI)的概念。

Spring学习笔记(二) 初探Spring的更多相关文章

  1. Spring学习(二)——Spring中的AOP的初步理解[转]

      [前面的话] Spring对我太重要了,做个关于web相关的项目都要使用Spring,每次去看Spring相关的知识,总是感觉一知半解,没有很好的系统去学习一下,现在抽点时间学习一下Spring. ...

  2. Spring学习(二)——Spring中的AOP的初步理解

    [前面的话] Spring对我太重要了,做个关于web相关的项目都要使用Spring,每次去看Spring相关的知识,总是感觉一知半解,没有很好的系统去学习一下,现在抽点时间学习一下Spring.不知 ...

  3. spring学习笔记二 注解及AOP

    本节需要导入spring-aop包 注解 使用注解的目的是为了代替配置,在使用注解时,省略键时,则是为value赋值. 扫描某个包下的所有类中的注解 <?xml version="1. ...

  4. Spring学习笔记五:Spring进行事务管理

    转载请注明原文地址:http://www.cnblogs.com/ygj0930/p/6776256.html  事务管理主要负责对持久化方法进行统一的提交或回滚,Spring进行事务管理即我们无需在 ...

  5. Spring学习(二)--Spring的IOC

    1.依赖反转模式 依赖反转:高层次的模块不应该依赖于低层次的模块,两者都应该依赖于抽象接口.抽象接口不应该依赖于具体实现.而具体实现则应该依赖于抽象接口. 在面向对象编程领域中,依赖反转原则(Depe ...

  6. Spring学习笔记(二)之装配Bean

    一,介绍Bean的装配机制 在Spring中,容器负责对象的创建并通过DI来协调对象之间的关系.但是我们要告诉Spring创建哪些Bean并且如何将其装配在一起.,装配wiring就是DI依赖注入的本 ...

  7. Spring学习笔记二:注入方式

    转载请注明原文地址:http://www.cnblogs.com/ygj0930/p/6774608.html  我们说,IOC的实现方式是依赖注入,也就是把被依赖对象赋值到依赖对象的成员属性.怎么做 ...

  8. Spring学习笔记(二)

    1.Spring MVC 返回json数据 <bean class="org.springframework.web.servlet.mvc.annotation.Annotation ...

  9. Spring 学习笔记(二)

    spring 核心 (xml方式.注解方式) 两种方式实现 ioc :控制反转 aop : 面向切面

  10. spring学习笔记二:spring使用构造方法注入(set方式注入)

    项目目录树: 1.spring的依赖包配置 * SPRING_HOME/dist/spring.jar * SPRING_HOME/lib/log4j/log4j-1.2.14.jar * SPRIN ...

随机推荐

  1. [Offer收割]编程练习赛37

    热门号码 #include<stdio.h> #include<string.h> #include<stdlib.h> #include<vector> ...

  2. hiho一下 第172周

    题目1 : Matrix Sum 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 You are given an N × N matrix. At the beginn ...

  3. 对比JavaScript的入口函数和jQuery的入口函数

    JavaScript的入口函数要等到页面中所有的资源(包括图片.文件)加载完成才开始执行. jQuery的入口函数只会等待文档数加载完成就开始执行,并不会等待图片.文件的加载.

  4. Android ExpandableListView group的item有间距child间隔不变

    <ExpandableListView android:id="@+id/lv" android:layout_width="fill_parent" a ...

  5. vue 子组件向父组件传值通信

    父组件 子组件 子组件用this.$emit

  6. 学了5天Arm,今天谈谈初学感受 (转)

    一.初探      4月1日入手友善mini2440.先看了下板子,感觉没什么稀奇的,s3c2440总线上外挂SDRAM(对这个不是很感冒,之前搞过一个FPGA的SDRAM控制器),NOR    .  ...

  7. CentOS6.5 静默安装Oracle 11g过程中提示:Exception in thread “main” java.lang.NoClassDefFoundError

    原来是系统中设置了DISPLAY环境变量,执行: [oracle@qa26 database]$ ./runInstaller  -silent -responseFile /usr/local/or ...

  8. BZOJ1143: [CTSC2008]祭祀river 网络流_Floyd_最大独立集

    Description 在遥远的东方,有一个神秘的民族,自称Y族.他们世代居住在水面上,奉龙王为神.每逢重大庆典, Y族都 会在水面上举办盛大的祭祀活动.我们可以把Y族居住地水系看成一个由岔口和河道组 ...

  9. 洛谷P2296 寻找道路_简单BFS

    Code: #include<cstdio> #include<queue> #include<algorithm> using namespace std; co ...

  10. vj线段树专题

    vj线段树专题题解 单点更新模板 void build(int x,int l,int r){//sum[x]控制l-r区域 if(l==r){Sum[x]=num[l];return ;} int ...