版权声明

  笔记出自《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. 图片全屏轮播插件poposlides

    jQuery轻量级全屏自适应焦点图插件poposlides 在线演示本地下载

  2. Spring学习笔记之依赖的注解(2)

    Spring学习笔记之依赖的注解(2) 1.0 注解,不能单独存在,是Java中的一种类型 1.1 写注解 1.2 注解反射 2.0 spring的注解 spring的 @Controller@Com ...

  3. LeetCode 75. Sort Colors (python一次遍历,模拟三路快排)

    LeetCode 75. Sort Colors (python一次遍历,模拟三路快排) 题目分析: 本题需要实现数字只包含0,1,2的排序,并且要求一次遍历. 由于只用把数字隔离开,很容易想到快排的 ...

  4. Android高效加载大图

    通过BitmapFactory的decode方法设置特定的options缩小图片到指定尺寸 1:通过加载设置了只编码图片边界options的图片,获取原图的尺寸和类型 2:计算图片需要缩小的倍数 3: ...

  5. ubuntu 搭建Mercurial 服务(nginx)

    ubuntu 搭建Mercurial 服务(nginx) 环境:ubuntu 12.05  Mercurial 步骤: (1)安装nginx 和 Mercurial: sudo apt-get ins ...

  6. 01--Qt扫盲篇

    Qt扫盲篇 1.What is Qt 一个跨平台应用程序和UI开发框架,主要偏向于UI框架方面,由诺基亚公司开发维护. 使用 Qt 只需一次性开发应用程序,无须重新编写源代码,便可跨不同桌面和嵌入式操 ...

  7. jQuery 父级,祖先,兄弟,等选择性操作

    jQuery.parent(expr) 找父亲节点,可以传入expr进行过滤,比如$("span").parent()或者$("span").parent(&q ...

  8. js和css引入文件消除缓存

    ?version=<?php echo filemtime('引用路径'); ?>

  9. 微信小程序打开PDF

    具体情况是:微信小程序打开springboot返回的pdf文件.微信端先downloadFile,然后openDocument.但是打开文档一直不成功.后来发现官网的例子没有加fileType,我在参 ...

  10. [2018.8.12]模拟赛B组

    T1 打表出奇迹,发现结论为\(E(a_n)=n+1\)即可. #include <iostream> #include <cstdio> #include <cctyp ...