Spring学习的第一天

  • Spring是以Ioc和Aop为内核,提供了表现层spring MVC 和持久层Spring JDBC等众多应用技术,还能整合开源世界众多著名的第三方框架和类库,成为使用最多的JavaEE企业应用开源框架。
  • Spring的优势:
  1. 方便解耦,简化开发;
  2. Aop编程的支持;
  3. 声明式事务的支持;
  4. 方便程序的测试;
  5. 方便集成各种优秀框架;
  6. 降低JavaEE  API的使用难度;
  7. Spring源码是经典学习的范例;
  • Spring的体系结构:全部基于核心容器Core Container

  • 编译时期依赖:JDBC在没有导依赖包时即mysql驱动时,将无法编译。即注册驱动时找不到com.mysql.jdbc.Driver()依赖包;
  • 耦合:程序间的依赖关系,其中包括:①类之间的依赖;②方法之间的依赖;
  • 解耦:降低程序间的依赖关系,实际开发中应该做到,编译时期不依赖,运行时才依赖;
  • 解耦的思路:
  1. 使用反射来创建对象,而避免使用new关键字,即Class.forName(“全限定类名”)。
  2. 通过读取配置文件来获取要创建对象的全类名,而不是直接写死,然后再通过反射来创建对象。
  • 解耦的方式:

    方式一:使用工程模式解耦:(创建Bean对象的工厂,用来创建Service和dao对象的)

    1.定义一个配置文件bean.properties配置service和dao层的全类名,

      配置的内容:

      accountService=com.itheima.service.impl.IAccountServiceImpl
      accountDao=com.itheima.dao.impl.IAccountImpl

    2.通过读取配置文件的内容,反射创建对象

    BeanFactory工厂类:

 3 import java.io.FileNotFoundException;
 4 import java.io.IOException;
 5 import java.io.InputStream;
 6 import java.util.Enumeration;
 7 import java.util.HashMap;
 8 import java.util.Map;
 9 import java.util.Properties;
10 
11 /**
12  * 创建Bean对象的工厂
13  * Bean:在计算机英语中,有可重用组件的含义
14  *
15  * 它就是创建service和dao对象的
16  *
17  * 1.定义一个配置文件配置service和dao
18  *      配置的内容:唯一标志=全限定类名(key=value形式)
19  * 2.通过读取配置文件当中的内容,反射创建对象
20  */
21 public class BeanFactory {
22     //定义一个properties对象
23     private static Properties properties = new Properties();
24 
25     //定义一个map,用于存储我们创建的对象,称之为容器
26     private static Map<String,Object> beans;
27 
28     //使用静态代码块为Properties对象赋值
29     static {
30 
31         try {
32             //获取properties文件的流对象
33             InputStream is = BeanFactory.class.getClassLoader().getResourceAsStream("bean.properties");
34             properties.load(is);
35             beans = new HashMap<String, Object>();
36             //取出配置文件中所有的key
37             Enumeration keys = properties.keys();
38             //遍历枚举
39             while (keys.hasMoreElements()){
40                 //取出每个key
41                 String key = keys.nextElement().toString();
42                 //根据key获取value
43                 String beanPath = properties.getProperty(key);
44                 //反射创建对象
45                 Object value = Class.forName(beanPath).newInstance();
46                 //把key和value存入容器之中
47                 beans.put(key,value);
48             }
49         } catch (FileNotFoundException e) {
50             e.printStackTrace();
51         } catch (IOException e) {
52             e.printStackTrace();
53         } catch (IllegalAccessException e) {
54             e.printStackTrace();
55         } catch (InstantiationException e) {
56             e.printStackTrace();
57         } catch (ClassNotFoundException e) {
58             e.printStackTrace();
59         }
60     }
61 
62     /**
63      * 根据bean的名称获取bean对象,此时获取的对象为单例对象
64      * @param beanName
65      * @return
66      */
67     public static Object getBean(String beanName){
68         return beans.get(beanName);
69     }
70 }

   *使用工厂模式创建出来的对象,使用Map集合作为容器去存储之后,创建出来的为单例对象,是我们需要的。(单例对象:只被创建一次,类中的成员只会初始化一次,效率高,而且一般没有成员变量的定义,如果要定义变量也是在方                法内部。多例对象:每次都是创建一个新的对象,执行效率无单例对象高,从而类中的成员每次都会被初始化

   方式二:使用Spring的Ioc控制反转(把创建对象的权利交给框架,是框架的重要特征,它包括依赖注入(DI)和依赖查找(DL))

   Spring的入门

   获取核心容器对象以及如何根据id获取bean对象的方式

/**
 * 模拟一个表现层,用于调用业务层
 */
public class Client {

    /**
     * 获取springIoc的核心容器,并且根据id获取对象
     *
     * ApplicationContext常用的三个实现类
     * ClassPathXmlApplicationContext,它可以加载类路径下的配置文件,要求配置文件必须在类路径下,不在的话加载不了(更常用)
     * FileSystemXmlApplicationContext,它可以加载磁盘任意路径下的配置文件,但要有访问权限
     *
     *
     *
     * AnnotationConfigApplicationContext,它是用于读取注解创建容器的
     * @param args
     */
    public static void main(String[] args) {

        //1.获取核心容器对象
        ApplicationContext ac =  new ClassPathXmlApplicationContext("bean.xml");
        //2.根据id查询获取bean对象
        IAccountService accountService = ac.getBean("accountService", IAccountService.class);
        IAccountDao accountDao = (IAccountDao) ac.getBean("accountDao");
        System.out.println(accountService);
        System.out.println(accountDao);
}
}

    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来管理 id为所要创建对象的名称  class为创建new对象的全限定类名-->
    <bean id="accountDao" class="com.itheima.dao.impl.IAccountImpl"></bean>

    <bean id="accountService" class="com.itheima.service.impl.IAccountServiceImpl"></bean>
</beans>
  • 关于核心容器的两个接口引发的问题:

    ApplicationContetx:在创建核心容器时,创建对象采取的策略是采用立即加载的方式,也就是说,一读取完配置文件马上就创建配置文件中配置对象(单例对象)。

    BeanFactory:在创建核心容器时,创建对象采取的策略是采用延迟加载的方式,也就是说,什么时候根据id获取对象了,什么时候才真正创建对象(多例对象)。

  • 创建bean对象的三种方式:
<!--创建bean的三种方式-->

    <!--
    第一种方式:使用默认构造函数进行创建,若无默认构造函数,则无法创建
    -->
    <bean id="accountService" class="com.itheima.service.impl.IAccountServiceImpl" scope="prototype"
    init-method="init" destroy-method="destroy">
    </bean>

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

    <!--
    第三种方式:使用工厂中的静态方法创建对象(使用某个类中的静态方法创建对象并且存入spring容器)
    -->
    <!--<bean id="accountService" class="com.itheima.factory.StaticFactory" factory-method="getAccountService"></bean>-->
  • bean的作用范围:<bean>的scope属性取值:singleton-单例(默认值),prototype-多例,request:    web中的request请求范围,session:    web中的会话范围,global-session:   集群环境会话范围(全局会话)
  • bean的生命周期:
  1. 单例对象生命周期与核心容器相同;
  2. 多例对象在我们使用对象时被spring框架为我们创建,对象只要在使用过程中就一直活着,当对象长时间不用时由JVM虚拟机垃圾回收。
  • Spring中的依赖注入就是为了维护依赖关系,参考bean.xml

    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="accountService" class="com.itheima.service.impl.IAccountServiceImpl">
        <constructor-arg name="name" value="test"></constructor-arg>
        <constructor-arg name="age" value="18"></constructor-arg>
        <constructor-arg name="date" ref="now"></constructor-arg>
    </bean>

<!--配置一个日期对象 Date类型不为基本数据类型,所以要先反射创建Date对象存入Ioc核心容器,之后再用ref属性接收--> <bean id="now" class="java.util.Date"></bean> <!--使用set方法注入 比较常用的方式--> <bean id="accountService1" class="com.itheima.service.impl.IAccountServiceImpl1"> <property name="name" value="test1"></property> <property name="age" value="19"/> <property name="date" ref="now"/> </bean>
<!--复杂类型的注入/集合类型的注入 结构相同标签可以互换,List结构集合:list,array,set标签可以通用,Map结构集合:map,props标签可以通用--> <bean id="accountService2" class="com.itheima.service.impl.IAccountServiceImpl2"> <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="myMap"> <map> <entry key="testA" value="aaa"></entry> <entry key="testB"> <value>bbb</value> </entry> </map> </property> <property name="myProperties"> <props> <prop key="testC">ccc</prop> <prop key="testD">ddd </prop> </props> </property> </bean> </beans>
原文地址:https://www.cnblogs.com/zxgCoding/p/11380539.html