Spring框架概述及Spring中基于xml的IOC配置

1、spring的概述

spring是什么

 Spring 是分层的 Java SE/EE 应用 full-stack 轻量级开源框架,以 IoC(Inverse Of Control:
 反转控制)和 AOP(Aspect Oriented Programming:面向切面编程)为内核,提供了展现层 Spring
 MVC 和持久层 Spring JDBC 以及业务层事务管理等众多的企业级应用技术,还能整合开源世界众多
 著名的第三方框架和类库,逐渐成为使用最多的 Java EE 企业应用开源框架。

spring的两大核心

 IoC(控制反转)和AOP(面向切面编程)

spring的发展历程和优势

1997 年 IBM 提出了 EJB 的思想
1998 年,SUN 制定开发标准规范 EJB1.0
1999 年,EJB1.1 发布
2001 年,EJB2.0 发布
2003 年,EJB2.1 发布
2006 年,EJB3.0 发布
Rod Johnson(spring 之父)
Expert One-to-One J2EE Design and Development(2002)
阐述了 J2EE 使用 EJB 开发设计的优点及解决方案
Expert One-to-One J2EE Development without EJB(2004)
阐述了 J2EE 开发不使用 EJB 的解决方式(Spring 雏形)
2017 年 9 月份发布了 spring 的最新版本 spring 5.0 通用版(GA)

spring体系结构

方便解耦,简化开发
通过 Spring 提供的 IoC 容器,可以将对象间的依赖关系交由 Spring 进行控制,避免硬编码所造
成的过度程序耦合。用户也不必再为单例模式类、属性文件解析等这些很底层的需求编写代码,可
以更专注于上层的应用。

AOP 编程的支持
通过 Spring 的 AOP 功能,方便进行面向切面的编程,许多不容易用传统 OOP 实现的功能可以通过 AOP 轻松应付。

声明式事务的支持
可以将我们从单调烦闷的事务管理代码中解脱出来,通过声明式方式灵活的进行事务的管理,
提高开发效率和质量。

方便程序的测试
可以用非容器依赖的编程方式进行几乎所有的测试工作,测试不再是昂贵的操作,而是随手可
做的事情。

方便集成各种优秀框架
Spring 可以降低各种框架的使用难度,提供了对各种优秀框架(Struts、Hibernate、Hessian、Quartz
等)的直接支持。

降低 JavaEE API 的使用难度
Spring 对 JavaEE API(如 JDBC、JavaMail、远程调用等)进行了薄薄的封装层,使这些 API 的
使用难度大为降低。

Java 源码是经典学习范例
Spring 的源代码设计精妙、结构清晰、匠心独用,处处体现着大师对 Java 设计模式灵活运用以
及对 Java 技术的高深造诣。它的源代码无意是 Java 技术的最佳实践的范例。

2、程序的耦合及解耦

耦合可以简单理解为程序间的依赖关系,比如类之间的依赖,方法间的依赖

解耦:降低程序间的依赖关系(不可能完全没有依赖)

实际开发中应做到:编译期不依赖,运行时才依赖

解耦的思路

  1. 使用反射来创建对象,而避免使用new关键字
  2. 通过读取配置文件来获取要创建对象的全限定类名

工厂模式解耦

BeanFactory.java

package com.itheima.factory;

import java.io.FileInputStream;
import java.io.InputStream;
import java.util.Properties;

/**
 * 一个创建bean对象的工厂
 *
 * bean:在计算机英语中,有可重用组件的含义
 * javabean,用java语言编写的可重用组件
 * JavaBean不等于实体类,实体类是javabean的一部分
 *
 * 它就是创建我们的service和dao对象的
 *
 * 1、需要一个配置文件来配置我们的service和dao
 *      配置的内容:唯一标示=全限定类名(key=value)
 * 2、通过读取配置文件的内容,反射创建对象
 *
 * 我们的配置文件可以是xml,也可以是properties
 */
public class BeanFactory {
    //定义一个Properties对象
    private static Properties props;
    //使用静态代码块为properties对象赋值
    static {
        try{
            //实例化对象
            props=new Properties();
            //获取properties对象的流对象
            InputStream in=BeanFactory.class.getClassLoader().getResourceAsStream("bean.properties");
            props.load(in);
        }catch (Exception e){
            throw new ExceptionInInitializerError("初始化失败");
        }
    }

    /**
     * 根据bean的名称获取bean对象
     * @param beanName
     * @return
     */
    public static Object getBean(String beanName){
        Object bean=null;
        try {
            String beanPath = props.getProperty(beanName);
            bean = Class.forName(beanPath).newInstance();
        }catch (Exception e){
            e.printStackTrace();
        }
        return bean;
    }

}

bean.properties

accountService=com.itheima.service.impl.AccountServiceImpl
accountDao=com.itheima.dao.impl.AccountDaoImpl

3、IOC概念和spring中的IOC

在这里插入图片描述
上面的方法是我们平常的创建对象的方法,会造成应用和资源糅合

下面即是IOC(控制反转,其实也就是降低依赖,通过工厂来控制资源,应用从工厂获取资源)

就是把对类的控制权交给了工厂,所以也叫控制反转,作用就是降低糅合
在这里插入图片描述

spring中基于XML的IOC环境搭建

  1. 配置文件pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.itheima</groupId>
    <artifactId>day01_eesy_03spring</artifactId>
    <version>1.0-SNAPSHOT</version>
    <packaging>jar</packaging>

    <dependencies>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>5.0.2.RELEASE</version>
        </dependency>
    </dependencies>

</project>
  1. 配置文件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">

    <!--把对象的创建交给spring来管理-->
    <bean id="accountService" class="com.itheima.service.impl.AccountServiceImpl"></bean>
     
    <bean id="accountDao" class="com.itheima.dao.impl.AccountDaoImpl"></bean>
</beans>
  1. 获取spring的ioc核心容器,并根据id获取对象
package com.itheima.ui;

import com.itheima.dao.IAccountDao;
import com.itheima.service.IAccountService;
import com.itheima.service.impl.AccountServiceImpl;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

/**
 * 模拟一个表现层,用于调用业务层
 */
public class Client {
    /**
     * 获取spring的ioc核心容器,并根据id获取对象
     * @param args
     */
    public static void main(String[] args) {
        //1、获取核心容器对象
        ApplicationContext ac=new ClassPathXmlApplicationContext("bean.xml");
        //2、根据id获取bean对象(下面两句用法都可以,一种是强转类型,一种是直接让他直接根据字节码转成对象)
        IAccountService as=(IAccountService) ac.getBean("accountService");
        IAccountDao adao=ac.getBean("accountDao",IAccountDao.class);

        System.out.println(as);
        System.out.println(adao);
        as.saveAccount();
    }
}

简单来说,IOC通过及其少的配置文件语句,实现了之前的工厂模式

bean的细节

<?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">

    <!--把对象的创建交给spring来管理-->
    <!--spring对bean的管理细节
        1、创建bean的三种方式
        2、bean对象的作用范围
        3、bean对象的生命周期
    -->

    <!--创建bean的三种方式-->
    <!--1、使用默认构造函数创建,
        在spring的配置文件中使用bean标签,配以id和class属性,且没有其他属性和标签,
        采用的就是默认构造函数创建bean对象,此时如果类中没有默认构造函数,则无法创建

    <bean id="accountService" class="com.itheima.service.impl.AccountServiceImpl"></bean>
    -->

    <!--2、使用普通工厂中的方法创建对象(使用某个类中的方法创建对象并存入spring容器)
    <bean id="InstanceFactory" class="com.itheima.factory.InstanceFactory"></bean>
    <bean id="accountService" factory-bean="InstanceFactory" factory-method="getAccountService"></bean>
    -->

    <!--3、使用工厂中的静态方法创建对象(使用某个类中的静态方法创建对象,并存入spring容器
    <bean id="accountService" class="com.itheima.factory.StaticFactory" factory-method="getAccountService"></bean>-->

    <!--bean的作用范围调整
        bean标签的scope属性:
            作用:用于指定bean的作用范围
            属性: 常用的就是singleton和prototype
                singleton   单例的(默认值)
                prototype   多例的
                request     作用于web应用的请求范围
                session     作用于web应用的会话范围
                global-session  作用于集群环境的会话范围(全局会话范围)当不是集群环境时,它就是session

    <bean id="accountService" class="com.itheima.service.impl.AccountServiceImpl" scope="singleton"></bean>
    -->

    <!--bean对象的生命周期
            单例对象
                出生  :当容器创建时对象出生
                活着  :只要容器还在对象一直活着
                死亡  :容器销毁,对象消亡
                总结  :单例对象的生命周期和容器相同
            多例对象
                出生  :当我们使用对象时spring框架为我们创建
                活着  :使用过程中一直活着
                死亡  :当对象长时间不用且没有别的对象引用时,由java的垃圾回收器回收
    -->
    <bean id="accountService" class="com.itheima.service.impl.AccountServiceImpl"
          scope="prototype" init-method="init" destroy-method="destory"></bean>
</beans>

4、依赖注入(Dependency Injection)

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">

   <!--spring中的依赖注入
        依赖注入:
            Dependency Injection
        IOC的作用:
            降低程序间的糅合(依赖关系)
        依赖关系的管理:
            以后都交给了spring来维护
        在当前类中需要用到其他类的对象,由spring为我们提供,我们只需要在配置文件中说明
        依赖关系的维护:
            就称之为依赖注入
        依赖注入:
            能注入的数据:有三类
                基本类型和string
                其他bean类型(在配置文件中或者注解配置过的bean)
                复杂类型/集合类型
            注入的方式:有三种
                第一种:使用构造函数提供
                第二种:使用set方法提供
                第三种:使用注解提供
   -->

    <!--构造函数注入
        使用的标签:constructor-arg
        标签出现的位置:bean标签的内部
        标签中的属性
            type    用于指定要注入数据的数据类型,该数据类型也是构造函数中某个或某些参数的类型
            index   用于指定要注入的数据给构造函数中指定索引位置的参数赋值,索引的位置从0开始
            name    用于指定给构造函数中指定名称的参数赋值
            ===========以上三个用于指定给构造函数中哪个参数赋值========================================
            value   用于提供基本类型和String的数据
            ref     用于提供其它的bean类型数据,它指的就是在Spring的核心ioc容器中出现过的bean对象

        优势:
            在获取bean对象时,注入数据是必须的操作,否则无法成功

        弊端:
            改变了bean对象的实例化方式,使我们在创建对象时,如果用不到这些数据,也必须提供
    -->
    <bean id="accountService" class="com.itheima.service.impl.AccountServiceImpl" >
        <constructor-arg name="name" value="text"></constructor-arg>
        <constructor-arg name="age" value="18"></constructor-arg>
        <constructor-arg name="birthday" ref="now"></constructor-arg>
    </bean>

    <!--配置一个日期对象-->
    <bean id="now" class="java.util.Date"></bean>

    <!--set方法注入  更常用的方式
        涉及的标签:propertu
        出现的位置:bean标签的内部
        标签中的属性
            name    用于指定给构造函数中指定名称的参数赋值
            value   用于提供基本类型和String的数据
            ref     用于提供其它的bean类型数据,它指的就是在Spring的核心ioc容器中出现过的bean对象

        优势
            创建对象时没有明确的限制

        劣势
            如果有某个成员必须有值,无法保证一定注入,
    -->
    <bean id="accountService2" class="com.itheima.service.impl.AccountServiceImpl2" >
        <property name="name" value="哈哈哈"></property>
        <property name="age" value="18"></property>
        <property name="birthday" ref="now"></property>
    </bean>

    <!--复杂类型的注入/集合类型的注入
        用于给list结构集合注入的标签有
            list array set
        用于给map结构集合注入的标签有
            map props
        结构相同,标签就可以互换
    -->
    <bean id="accountService3" class="com.itheima.service.impl.AccountServiceImpl3" >
        <property name="myStrs">
            <array>
                <value>AAAA</value>
                <value>BBBB</value>
                <value>CCCC</value>
            </array>
        </property>

        <property name="myList">
            <list>
                <value>AAAA</value>
                <value>BBBB</value>
                <value>CCCC</value>
            </list>
        </property>

        <property name="mySet">
            <set>
                <value>AAAA</value>
                <value>BBBB</value>
                <value>CCCC</value>
            </set>
        </property>

        <property name="myMap">
            <map>
                <entry key="testA" value="aaaa"></entry>
                <entry key="testB" value="bbbb"></entry>
                <entry key="testC" value="cccc"></entry>
            </map>
        </property>

        <property name="myProps">
            <props>
                <prop key="A">aaa</prop>
                <prop key="B">bbb</prop>
            </props>
        </property>
    </bean>
</beans>

AccountServiceImpl(构造函数注入对应类)

package com.itheima.service.impl;
import com.itheima.service.IAccountService;

import java.util.Date;

/**
 * 账户的业务层实现类
 */
public class AccountServiceImpl implements IAccountService {

    //如果是经常变化的数据,并不适用于注入的方式
    private String name;
    private Integer age;
    private Date birthday;

    public AccountServiceImpl(String name,Integer age,Date birthday){
        this.name=name;
        this.age=age;
        this.birthday=birthday;
    }


    public void  saveAccount(){
        System.out.println("service中的saveAccount方法执行了。。。"+name+","+age+","+birthday);
    }
}

AccountServiceImpl2(set方法注入对应类)

package com.itheima.service.impl;
import com.itheima.service.IAccountService;

import java.util.Date;

/**
 * 账户的业务层实现类
 */
public class AccountServiceImpl2 implements IAccountService {

    //如果是经常变化的数据,并不适用于注入的方式
    private String name;
    private Integer age;
    private Date birthday;

    public void setName(String name) {
        this.name = name;
    }

    public void setAge(Integer age) {
        this.age = age;
    }

    public void setBirthday(Date birthday) {
        this.birthday = birthday;
    }

    public void  saveAccount(){
        System.out.println("service中的saveAccount方法执行了。。。"+name+","+age+","+birthday);
    }
}

AccountServiceImpl3(复杂类型注入对应类)

package com.itheima.service.impl;
import com.itheima.service.IAccountService;

import java.util.*;

/**
 * 账户的业务层实现类
 */
public class AccountServiceImpl3 implements IAccountService {

    private String[] myStrs;
    private List<String> myList;
    private Set<String> mySet;
    private Map<String ,String > myMap;
    private Properties myProps;

    public void setMyStrs(String[] myStrs) {
        this.myStrs = myStrs;
    }

    public void setMyList(List<String> myList) {
        this.myList = myList;
    }

    public void setMySet(Set<String> mySet) {
        this.mySet = mySet;
    }

    public void setMyMap(Map<String, String> myMap) {
        this.myMap = myMap;
    }

    public void setMyProps(Properties myProps) {
        this.myProps = myProps;
    }

    public void  saveAccount(){
        System.out.println(Arrays.toString(myStrs));

        System.out.println(myList);
        System.out.println(mySet);
        System.out.println(myMap);
        System.out.println(myProps);
    }
}

Client(Test)

package com.itheima.ui;

import com.itheima.service.IAccountService;
import com.itheima.service.impl.AccountServiceImpl;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

/**
 * 模拟一个表现层,用于调用业务层
 */
public class Client {
    /**
     *
     * @param args
     */
    public static void main(String[] args) {
        //1、获取核心容器对象
        ClassPathXmlApplicationContext ac= new ClassPathXmlApplicationContext("bean.xml");
        //2、根据id获取bean对象(下面两句用法都可以,一种是强转类型,一种是直接让他直接根据字节码转成对象)
//        IAccountService as=(IAccountService) ac.getBean("accountService");
//        as.saveAccount();
//        IAccountService as=(IAccountService) ac.getBean("accountService2");
//       as.saveAccount();
        IAccountService as=(IAccountService) ac.getBean("accountService3");
        as.saveAccount();
    }
}


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值