一、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 技术的最佳实践的范例。

二、Ioc的概念+作用(核心部分!!!)

1.程序的耦合(当看小说看看吧,反正也不长)

耦合性(Coupling),也叫耦合度,是对模块间关联程度的度量。耦合的强弱取决于模块间接口的复杂性、调用模块的方式以及通过界面传送数据的多少。模块间的耦合度是指模块之间的依赖关系,包括控制关系、调用关系、数据传递关系。模块间联系越多,其耦合性越强,同时表明其独立性越差( 降低耦合性,可以提高其独立性)。耦合性存在于各个领域,而非软件设计中独有的,但是我们只讨论软件工程中的耦合。

   在软件工程中,耦合指的就是就是对象之间的依赖性。对象之间的耦合越高,维护成本越高。因此对象的设计应使类和构件之间的耦合最小。软件设计中通常用耦合度和内聚度作为衡量模块独立程度的标准。划分模块的一个准则就是高内聚低耦合。
它有如下分类:

(1)内容耦合。当一个模块直接修改或操作另一个模块的数据时,或一个模块不通过正常入口而转入另一个模块时,这样的耦合被称为内容耦合。内容耦合是最高程度的耦合,应该避免使用之。
(2)公共耦合。两个或两个以上的模块共同引用一个全局数据项,这种耦合被称为公共耦合。在具有大量公共耦合的结构中,确定究竟是哪个模块给全局变量赋了一个特定的值是十分困难的。
(3) 外部耦合 。一组模块都访问同一全局简单变量而不是同一全局数据结构,而且不是通过参数表传递该全局变量的信息,则称之为外部耦合。
(4) 控制耦合 。一个模块通过接口向另一个模块传递一个控制信号,接受信号的模块根据信号值而进行适当的动作,这种耦合被称为控制耦合。
(5)标记耦合 。若一个模块 A 通过接口向两个模块 B 和 C 传递一个公共参数,那么称模块 B 和 C 之间存在一个标记耦合。
(6) 数据耦合。模块之间通过参数来传递数据,那么被称为数据耦合。数据耦合是最低的一种耦合形式,系统中一般都存在这种类型的耦合,因为为了完成一些有意义的功能,往往需要将某些模块的输出数据作为另一些模块的输入数据。
(7) 非直接耦合 。两个模块之间没有直接关系,它们之间的联系完全是通过主模块的控制和调用来实现的。

总结:
    耦合是影响软件复杂程度和设计质量的一个重要因素,在设计上我们应采用以下原则:如果模块间必须存在耦合,就尽量使用数据耦合,少用控制耦合,限制公共耦合的范围,尽量避免使用内容耦合。

内聚与耦合
   内聚标志一个模块内各个元素彼此结合的紧密程度,它是信息隐蔽和局部化概念的自然扩展。内聚是从功能角度来度量模块内的联系,一个好的内聚模块应当恰好做一件事。它描述的是模块内的功能联系。耦合是软件结构中各模块之间相互连接的一种度量,耦合强弱取决于模块间接口的复杂程度、进入或访问一个模块的点以及通过接口的数据。 程序讲究的是低耦合,高内聚。就是同一个模块内的各个元素之间要高度紧密,但是各个模块之间的相互依存度却要不那么紧密。
    内聚和耦合是密切相关的,同其他模块存在高耦合的模块意味着低内聚,而高内聚的模块意味着该模块同其他模块之间是低耦合。在进行软件设计时,应力争做到高内聚,低耦合。

举例子:

(1)、早期的代码

public class AccountServiceImpl implements IAccountService {
     private IAccountDao accountDao = new AccountDaoImpl();
}

分析:

业务层调用持久层,并且此时业务层在依赖持久层的接口和实现类。如果此时没有持久层实现类,编译将不能通过。这种编译期依赖关系,应该在我们开发中杜绝。我们需要优化代码解决。

(2)、JDBC

public static void main(String[] args) throws Exception {
    //1.注册驱动
    //DriverManager.registerDriver(new com.mysql.jdbc.Driver());
    Class.forName("com.mysql.jdbc.Driver");
    //2.获取连接
    //3.获取预处理 sql 语句对象
    //4.获取结果集
    //5.遍历结果集
}

分析:

我们的类依赖了数据库的具体驱动类(MySQL),如果这时候更换了数据库品牌(比如 Oracle),需要修改源码来重新数据库驱动。这显然不是我们想要的。

解决思路:

当是我们讲解 jdbc 时,是通过反射来注册驱动的,代码如下:
Class.forName("com.mysql.jdbc.Driver");//此处只是一个字符串

此时的好处是,我们的类中不再依赖具体的驱动类,此时就算删除 mysql 的驱动 jar 包,依然可以编译(运行就不要想了,没有驱动不可能运行成功的)。
    解耦思路:
         1.使用反射创建对象,避免new 编译时期出错,
         2.通过读取配置文件 获取要创建的对象全限定类名
  
同时,也产生了一个新的问题,mysql 驱动的全限定类名字符串是在 java 类中写死的,一旦要改还是要修改源码。

解决这个问题也很简单,使用配置文件配置。

2、工厂模式解耦

在实际开发中我们可以把三层的对象都使用配置文件配置起来,当启动服务器应用加载的时候,让一个类中的方法通过读取配置文件,把这些对象创建出来并存起来。在接下来的使用的时候,直接拿过来用就好了。

那么,这个读取配置文件,创建和获取三层对象的类就是工厂。

案例:
package com.itheima.factory;

import java.io.IOException;
import java.io.InputStream;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;

/**
 * 创建bean对象的工厂
 *
 * Bean:在 计算机英语 ,可重用组件的含义
 *
 * JavaBean: 用java 语言编写的可重用组件
 *          javabean > 实体类
 *
 *          它就是创建我们的service和dao对象
 *
 *     第一:需要配置文件配置 我们的 service 和 dao
 *           配置的内容,唯一标识==全限定类名(key=value)
 *     第二:通过读取配置文件中 配置的内容 ,反射创建对象
 *
 *     我的配置文件 可以是xml,可以是 properties
 */
public class BeanFactory {

    //定义一个Properites对象
    private static Properties props;

    //定义一个Map,用于存放我们要创建的对象,我们把它称之为容器
    private static Map<String,Object> beans;

    //使用静态代码快为Properties对象赋值
    static {
        try {
            //实例化对象
            props = new Properties();
            //获取文件流对象
            InputStream in = BeanFactory.class.getClassLoader().getResourceAsStream("bean.properties");
            props.load(in);

            //实例化容器
            beans = new HashMap<String, Object>();

            //取出配置文件中所有的key
            Enumeration keys = props.keys();

            //遍历枚举
            while (keys.hasMoreElements()){
                //取出每个key
                String key = keys.nextElement().toString();
                //根据key获取value
                String beanPath = props.getProperty(key);
                //反射创建对象
                Object value = Class.forName(beanPath).newInstance();
                //把key和value存入容器中
                beans.put(key,value);
            }
        } catch (Exception e) {
            throw new ExceptionInInitializerError("初始化 properties失败");
        }
    }

    /**
     * 根据bean的名称获取对象 (单例)
     * @param beanName
     * @return
     */
    public static Object getBean(String beanName){
        return beans.get(beanName);
    }
    /**
     * 根据有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;
    }*/
}

package com.itheima.service.impl;

import com.itheima.dao.IAccountDao;
import com.itheima.factory.BeanFactory;
import com.itheima.service.IAccountService;

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

    //业务层 调用持久层
//    private IAccountDao accountDao = new AccountDaoImpl();
    private IAccountDao accountDao = (IAccountDao) BeanFactory.getBean("accountDao");
    public void saveAccount() {
        int i =1;
        accountDao.saveAccount();
        System.out.println(i);
        i++;
    }
}

package com.itheima.dao.impl;

import com.itheima.dao.IAccountDao;

/**
 * 持久层实现类
 */
public class AccountDaoImpl implements IAccountDao {

    public void saveAccount() {
        System.out.println("保存了,账户");
    }
}

package com.itheima.ui;

import com.itheima.factory.BeanFactory;
import com.itheima.service.IAccountService;
import com.itheima.service.impl.AccountServiceImpl;

/**
 * 模拟一个表现曾,用于调用业务层
 */
public class Client {
    public static void main(String[] args) {
//        IAccountService as = new AccountServiceImpl();

        for (int i = 0; i < 5; i++) {
            IAccountService as  = (IAccountService) BeanFactory.getBean("accountService");
            System.out.println(as);
            as.saveAccount();
        }


    }
}

z这个案例呢,我是使用的properties解析配置文件,

//bean.properties
accountService=com.itheima.service.impl.AccountServiceImpl
accountDao=com.itheima.dao.impl.AccountDaoImpl

最开始出现了一个问题,

每次都会创建一个全新的对象。这个是多例情况

如果需要控制对象,单例,则需要使用容器

我代码中的这个部分很清晰了,注解也都很清晰

3、控制反转!(重点)

1、存哪去?

分析:由于我们是很多对象,肯定要找个集合来存。这时候有 Map 和 List 供选择。
 到底选 Map 还是 List 就看我们有没有查找需求。有查找需求,选 Map。
所以我们的答案就是
     在应用加载时,创建一个 Map,用于存放三层对象。
     我们把这个 map 称之为容器。
2、还是没解释什么是工厂?
工厂就是负责给我们从容器中获取指定对象的类。这时候我们获取对象的方式发生了改变。
原来:
 我们在获取对象时,都是采用 new 的方式。是主动的。
现在: 我们获取对象时,同时跟工厂要,有工厂为我们查找或者创建对象。是被动的。
这种被动接收的方式获取对象的思想就是控制反转,它是 spring 框架的核心之一。
明确 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来管理-->

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

    <bean id="accountDao" class="com.itheima.dao.impl.AccountDaoImpl"></bean>
</beans>
package com.itheima.dao.impl;

import com.itheima.dao.IAccountDao;

/**
 * 持久层实现类
 */
public class AccountDaoImpl implements IAccountDao {

    public void saveAccount() {
        System.out.println("保存了,账户");
    }
}

package com.itheima.service.impl;

import com.itheima.dao.IAccountDao;
import com.itheima.dao.impl.AccountDaoImpl;

import com.itheima.service.IAccountService;

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

    //业务层 调用持久层
    private IAccountDao accountDao = new AccountDaoImpl();

    public AccountServiceImpl(){
        System.out.println("对象创建了");
    }

    public void saveAccount() {
        int i =1;
        accountDao.saveAccount();
        System.out.println(i);
        i++;
    }
}

package com.itheima.ui;


import com.itheima.dao.IAccountDao;
import com.itheima.service.IAccountService;
import com.itheima.service.impl.AccountServiceImpl;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.context.support.FileSystemXmlApplicationContext;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;

import javax.annotation.Resources;

/**
 * 模拟一个表现曾,用于调用业务层
 */
public class Client {
    /**
     * 获取srping容器 IOC核心容器,并根据id获取对象
     *
     * ApplicationContext的三个常用实现类
     *          ClassPathXmlApplicationContext
     *              它可以加载类路径下的配置文件
     *                  要求配置文件必须在类路径下
     *          FileSystemXmlApplicationContext
     *              它可以加载磁盘任意路径下的配置文件(必须有访问权限)
     *
     *          AnnotationConfigApplicationContext
     *              它用于读取注解创建容器
     *
     * 核心容器的两个接口引发出的问题
     *  ApplicationContext:  使用场景:单例对象适用  (更多的采用此接口)
     *      它在构建核心容器时,创建对象采取的策略时 立即加载,
     *              也就是说 只要一读取完,就立马创建配置文件中的配置对象
     *
     *  BeanFactory:   使用场景:多例对象适用
     *      它在构建创建核心容器时,创建对象 采用延迟加载方式,
     *              也就是说,什么时候根据id获取对象,什么时候才真正的创建对象
     *
     * @param args
     */
    public static void main(String[] args) {
        //1.获取核心容器对象
//        ApplicationContext ac = new ClassPathXmlApplicationContext("bean.xml");

//        ApplicationContext ac = new FileSystemXmlApplicationContext("C:\\Users\\78703\\Desktop\\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();

//-----------------------------------------------------
        Resource resource = new ClassPathResource("bean.xml");
        BeanFactory factory = new XmlBeanFactory(resource);
        IAccountService as = (IAccountService) factory.getBean("accountService");
        System.out.println(as);
    }
}

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

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

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

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

    <!-- bean的作用范围调整 -->
    <!--
        spring 默认情况下是单例的 对象
        bean标签的scope属性
            作用:用于至于bean的作用范围
            取值:常用 1.2.
                singleton:单例的(默认值)
                prototype:多例的
                request:作用于web应用的请求范围
                session:作用与web应用的会话范围
                global-session:作用于集群环境的会话范围(全局会话范围)
                                当不是集群环境是,就是session

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

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

</beans>
package com.itheima.ui;



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


/**
 * 模拟一个表现曾,用于调用业务层
 */
public class Client {
    /**
     * @param args
     */
    public static void main(String[] args) {
        //1.获取核心容器对象
//        ApplicationContext ac = new ClassPathXmlApplicationContext("bean.xml");
        ClassPathXmlApplicationContext ac = new ClassPathXmlApplicationContext("bean.xml");

        //2.根据id获取bean对象
        //下面是两种方式
        IAccountService as1 = (IAccountService) ac.getBean("accountService");

        as1.saveAccount();

        //收到关闭容器
        ac.close();


    }
}

package com.itheima.service.impl;



import com.itheima.service.IAccountService;

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



    public AccountServiceImpl(){
        System.out.println("对象创建了");
    }

    public void saveAccount() {
        System.out.println("service中的saveAccount方法执行了");
    }

    public void init() {
        System.out.println("对象初始化了。。。。");
    }

    public void destroy() {
        System.out.println("对象销毁了。。。。。");
    }
}

package com.itheima.factory;

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

/**
 * 模拟工厂类(该类可能存在与jar包中)
 */
public class InstanceFactory {

    public IAccountService getAccountService(){
        return new AccountServiceImpl();
    }
}

package com.itheima.factory;

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

public class StaticFactory {

    public static IAccountService getAccountService(){
        return new AccountServiceImpl();
    }
}

三、依赖注入!

<?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方法提供
                第三种:使用注解提供
 -->

    <!-- 构造函数注入
         使用的标签:contructor-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="test"></constructor-arg>
        <constructor-arg name="age" value="18"></constructor-arg>
        <constructor-arg name="birthday" ref="now"></constructor-arg>
    </bean>

    <!--配置一个日期对象-->
    <!-- 读取这个全限定类名,反射创建一个对象,并且存入spring核心容器中 -->
    <bean id="now" class="java.util.Date"></bean>



   <!-- set方法注入      更常用的方式
        涉及的标签:property
        出现的位置:bean标签的内部
        标签的属性:
            name:用于指定注入时 所调用的set方法名称      常用
            value:用于提供基本类型 和 String类型的数据
            ref:用于指定其他的bean类型数据,指的就是在spring的Ioc核心容器中
                        出现的bean对象

        优势:
            创建对象时,没有明确的限制,可以直接使用默认构造函数
        弊端:
            如果有某个成员必须有值,则获取对象时有可能set方法没有执行
   -->
    <bean id="accountService2" class="com.itheima.service.impl.AccountServiceImpl2">
        <property name="name" value="TEST"></property>
        <property name="age" value="21" ></property>
        <property name="birthday" ref="now"></property>

    </bean>


    <!-- 复杂类型的注入/集合类型的注入
         用于给List结构集合注入的标签:
            list array set
          用于给Map结构集合注入的标签:
            map   props
          结构相同,标签可以互换

          通常开发时,只需记list  map 两组就够了
    -->
    <bean id="accountService3" class="com.itheima.service.impl.AccountServiceImpl3">
        <property name="myStrs">
            <array>
                <value>AAA</value>
                <value>BBB</value>
                <value>CCC</value>
            </array>
        </property>

        <property name="myList">
            <list>
                <value>AAA</value>
                <value>BBB</value>
                <value>CCC</value>
            </list>
        </property>

        <property name="mySet">
            <set>
                <value>AAA</value>
                <value>BBB</value>
                <value>CCC</value>
            </set>
        </property>
        <property name="MymMap">
            <map>
                <entry key="testA" value="aaa"></entry>
                <entry key="testB">
                    <value>BBB</value>
                </entry>
            </map>
        </property>

        <property name="myProps">
            <props>
                <prop key="testC">ccc</prop>
                <prop key="testD">ddd</prop>
            </props>
        </property>
    </bean>
</beans>
package com.itheima.ui;



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


/**
 * 模拟一个表现曾,用于调用业务层
 */
public class Client {
    /**
     * @param args
     */
    public static void main(String[] args) {
        //1.获取核心容器对象
//        ApplicationContext ac = new ClassPathXmlApplicationContext("bean.xml");
        ClassPathXmlApplicationContext ac = new ClassPathXmlApplicationContext("bean.xml");

        //2.根据id获取bean对象
        //下面是两种方式
//        IAccountService as1 = (IAccountService) ac.getBean("accountService");

//        as1.saveAccount();
//        IAccountService as2 = (IAccountService) ac.getBean("accountService2");
//        as2.saveAccount();
        IAccountService as3 = (IAccountService) ac.getBean("accountService3");

        as3.saveAccount();

    }
}

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.age = age;
        this.birthday = birthday;
        this.name = name;
    }

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

  }

package com.itheima.service.impl;



import com.itheima.service.IAccountService;

import java.util.Date;

/**
 * 账户业务层实现类
使用set方法注入!!
 */
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);
    }

  }

package com.itheima.service.impl;



import com.itheima.service.IAccountService;

import java.util.*;

/**
 * 账户业务层实现类

使用set方法注入!!
 */
public class AccountServiceImpl3 implements IAccountService {
    private String[] myStrs;
    private List<String> myList;
    private Set<String> mySet;
    private Map<String ,String> mymMap;
    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 setMymMap(Map<String, String> mymMap) {
        this.mymMap = mymMap;
    }

    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(mymMap);
        System.out.println(myProps);
    }

  }