03spring详解与IOC基础

明确 Spring不属于MVC中

一、Spring是什么?

Spring是一个开源的轻量级、非侵入式的控制反转和面向切面的容器框架。
1、轻量级
资源包很小,所有jar包加在一起只有不到1MB,开发时往往有对环境尽量小的限制,因为手机电脑内存有限
2、非侵入式
侵入式:大量、频繁的使用一个框架的API
非侵入式:虽然Spring在开发时经常被使用,但代码里几乎看不到任何一行代码与Spring的API相关
开发中往往本着的原则是针推扩展开放,针对修改关闭
非侵入式相对于侵入式好处在于:侵入式框架的写法已经固定,几乎不能做任何扩展、记忆起来非常费劲、不容易研究底层逻辑
3、容器
那么,既然Spring的非侵入性要求几乎见不到API,我们怎么去实现Spring?用xml配置文件,xml就成为Spring框架的容器
4、面向切面,英文是aop,切入点(关注点):程序实现的每一个功能点,将切入点分成两类,横切关注点,不涉及具体业务需求、大部分功能点都会涉及的需求(记录日志,无论在什么功能),垂直关注点,具体的业务需求(注册、登录),面向切面编程就是针对横切关注点去做
作用:提高开发效率,提升代码可重用性

5、控制反转,英文是IOC,用以解决应用程序中对象管理,现有A类、B类,如果A类想调用B类中的方法,就需要new B类,此时就说A、B两类之间有依赖关系,由于追求低耦合高内聚,所谓耦合就指这种依赖关系,如果某一天要对A类做修改,非常麻烦,因此A类可扩展性就小,所谓控制反转就是将把类之间依赖的控制反转给容器,在Spring框架中容器就是Spring的核心配置文件
作用:降低模块间的耦合,提高代码的可维护性

二、Spring框架组成

七大部分
Spring Core主要指IOC功能
Spring AOP主要指AOP功能
Spring ORM主要指和持久层集成功能,例Mybatis和Spring整合,事务处理、连接池管理
Spring Web和Servlet集成功能,实际上不怎么用
Spring Context和企业级应用功能集成的功能,如付款、邮件
Spring Web MVC涉及mvc,代替Servlet完成控制功能

三、# IOC

IOC控制反转(从创建对象的角度来诠释spring)/DI注入(从给对象设置属性的角度来诠释spring)
spring容器内部协作(图略)

image

加载配置方式

1)BeanFactory(低层 不推荐)
2 ) ApplicatioinContext(推荐)
主要区别:BeanFactory采用延迟加载机制,他不会在系统启动时校验配置文件正确性,只有在第一次调用getBean时才会验证,会导致运行期才发现错误
ApplicatioinContext启动时就验证bean的正确性,能避免一些运行期错误

BEAN加载配置文件的方式

1)ClassPathXmlApplicationContext
2 ) FileSystemXmlApplicationContext
例子:
//通过加载文件系统下的xml来获取应用程序上下文
//ApplicationContext ctx=new FileSystemXmlApplicationContext("classpath:ApplicationContext.xml");
ApplicationContext ctx=new FileSystemXmlApplicationContext("E:/workspace/springstudy/src/main/resource/ApplicationContext.xml");

依赖注入的方式

三种:spring支持前两种

1)属性的set方法(已经讲过)

2)构造方法

改造上午写的userService 加一个构造方法

package com.neuedu.service;

import com.neuedu.dao.IUserDao;

public class UserServiceImpl implements IUserService {
private IUserDao userDao;

    public UserServiceImpl(IUserDao userDao) {
        super();
        this.userDao = userDao;
    }
    @Override
    public int login(String username, String pwd) {
        return userDao.login(username, pwd);
    }
    public IUserDao getUserDao() {
        return userDao;
    }
    public void setUserDao(IUserDao userDao) {
        this.userDao = userDao;
    }
}

配置文件改造:

<?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-4.3.xsd">
    <!-- IUserDao userDao=new UserDaoImpl(); -->
    <bean id="userDao2" class="com.neuedu.dao.UserDaoImpl2"></bean>
    <!-- IUserService service=new  UserServiceImpl();
      service.setUserDao(userDao);-->
    <bean id="userService" class="com.neuedu.service.UserServiceImpl">
<!--       <property name="userDao" ref="userDao"></property>-->
   <constructor-arg name="userDao" ref="userDao2"></constructor-arg>
    </bean>
</beans>

重新测试UserTest.java(略)

3)接口 (spring不支持)##

第2节

注入属性

  • 普通类型
  • 对象类型(已经用过,最常用)
  • null类型
  • 集合类型
  1. list
  2. map
  3. set

1)普通类型(以String为例)

    public class SimplePropertyDemo {
    private String userName;
    private String password;

    public String getUserName() {
        return userName;
    }

    public void setUserName(String userName) {
        this.userName = userName;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

}

bean定义

    <bean id="simplePropertyDemo" class="com.neuedu.spring.chap02.SimplePropertyDemo">
    <property name="userName" value="root"></property>
    <property name="password" value="123456"></property>
    </bean>

测试类

   public static void main(String[] args) {
 ApplicationContext ctx=new ClassPathXmlApplicationContext("ApplicationContext.xml");
 SimplePropertyDemo spd=(SimplePropertyDemo) ctx.getBean("simplePropertyDemo");
     System.out.println(spd.getPassword());
     System.out.println(spd.getUserName());
    }

学生练习:
调用构造方法注入基本类型

public SimplePropertyDemo(String userName, String password) {
        super();
        this.userName = userName;
        this.password = password;
    }

配置文件更改

    <bean id="simplePropertyDemo1" class="com.neuedu.spring.chap02.SimplePropertyDemo">
       <constructor-arg name="userName" value="root"></constructor-arg>
       <constructor-arg name="password" value="123456"></constructor-arg>
    </bean>

测试类:

SimplePropertyDemo spd=(SimplePropertyDemo) ctx.getBean("simplePropertyDemo1");
     System.out.println(spd.getPassword());
     System.out.println(spd.getUserName());

list类型

list里存放普通类型

java类:

public class ListDemo {
  private List<String> strList;

public List<String> getStrList() {
    return strList;
}

public void setStrList(List<String> strList) {
    this.strList = strList;
}

}

配置文件

 <bean id="listDemo" class="com.neuedu.spring.chap02.ListDemo">
  <property name="strList">
  <list>
  <value>李海峰</value>
  <value>刘春雨</value>
  <value>陈昊</value>
  </list>
</property>
</bean>

测试类

public static void main(String[] args) {
  ApplicationContext ctx=new ClassPathXmlApplicationContext("ApplicationContext.xml");
  ListDemo ld=(ListDemo) ctx.getBean("listDemo");
  System.out.println(ld.getStrList());
  }

注入map set

新建javabean

public class MapSetDemo {
    private Map<String, String> cityMap;
    private Set<String> sets;
......
}

写配置文件

<bean id="mapSetDemo" class="com.neuedu.spring.chap02.MapSetDemo">
  <property name="cityMap">
  <map>
  <entry key="024" value="沈阳"></entry>
  <entry key="0413" value="抚顺"></entry>
  <entry key="0411" value="大连"></entry>
  </map>
  </property>
  <property name="sets">
  <set>
  <value>和平区</value>
  <value>沈河区</value>
  <value>大东区</value>
  <value>铁西区</value>
  <value>皇姑区</value>
  </set>
  </property>
</bean>

测试类

public static void main(String[] args) {
        ApplicationContext ctx=new ClassPathXmlApplicationContext("ApplicationContext.xml");
        MapSetDemo msd=ctx.getBean("mapSetDemo",MapSetDemo.class);
        System.out.println(msd.getCityMap());
        System.out.println(msd.getSets());
        }

第3节

注入list,list里放引用类型

User.java

public class User {
    private String name;
    private String password;
......
}

public class ListDemo {
  ......
  private List<User>   userList;
  ......
}

相关配置文件

    <bean id="user1" class="com.neuedu.pojo.User">
       <property name="name" value="zzf"></property>
       <property name="password" value="123456"></property>
    </bean>
     <bean id="user2" class="com.neuedu.pojo.User">
       <property name="name" value="lwk"></property>
       <property name="password" value="123456"></property>
    </bean>

<bean id="listDemo" class="com.neuedu.spring.chap02.ListDemo">
  <property name="strList">
      <list>
          <value>李海峰</value>
          <value>刘春雨</value>
          <value>陈昊</value>
      </list>
    </property>
   ** <property name="userList">
      <list>
       <ref bean="user1"/>
       <ref bean="user2"/>
      </list>

    </property>**
</bean>

相关测试类

public static void main(String[] args) {
  ApplicationContext ctx=new ClassPathXmlApplicationContext("ApplicationContext.xml");
  ListDemo ld=(ListDemo) ctx.getBean("listDemo");
  System.out.println(ld.getStrList());
  List<User> users=ld.getUserList();
  for(User u:users){
      System.out.println(u.getName());
  }
   }
}
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 217,185评论 6 503
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 92,652评论 3 393
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 163,524评论 0 353
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 58,339评论 1 293
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 67,387评论 6 391
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 51,287评论 1 301
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 40,130评论 3 418
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 38,985评论 0 275
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 45,420评论 1 313
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 37,617评论 3 334
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 39,779评论 1 348
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 35,477评论 5 345
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 41,088评论 3 328
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 31,716评论 0 22
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 32,857评论 1 269
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 47,876评论 2 370
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 44,700评论 2 354

推荐阅读更多精彩内容

  • 本来是准备看一看Spring源码的。然后在知乎上看到来一个帖子,说有一群**自己连Spring官方文档都没有完全读...
    此鱼不得水阅读 6,934评论 4 21
  • 2.1 我们的理念是:让别人为你服务 IoC是随着近年来轻量级容器(Lightweight Container)的...
    好好学习Sun阅读 2,713评论 0 11
  • 1.Spring简介 Spring是J2EE开发中一个很重要的框架。它主要用来解决下面两个问题。 解决大型软件开发...
    sixleaves阅读 1,369评论 0 6
  • 参考W3C Spring教程 Spring致力于J2EE应用的各种解决方案,而不仅仅专注于某一层解决方案。可以说S...
    王侦阅读 1,171评论 0 6
  • 前言 只有光头才能变强 回顾前面: 给女朋友讲解什么是代理模式 包装模式就是这么简单啦 单例模式你会几种写法? 工...
    Java3y阅读 2,284评论 1 60