一、Spring的简介

1、spring是一个full-stack轻量级开源框架
    2、spring的两大核心
        IoC: inverse of control  控制反转:反转是对象的创建权,由原来自己new方式变成让spring容器(IoC容器)创建对象  作用:完成解耦合
        AOP: 面向切面编程(底层实现:动态代理)  作用:不修改源码的情况下,对目标方法进行增强
    3、spring优点
        方便解耦 简化开发:IoC
        aop编程支持:aop
        声明式事务的支持:声明式事务控制
        方便程序测试:spring集成junit
        方便集成各种优秀框架:ssh整合
        降低javaEEAPI的使用难度:
            spring提供了很多工具模版  XXXTemplate
            例如:    
                JdbcTemplate(相当于DBUtils)
                HibernateTemplate
                RedisTemplate
                JmsTemplate
                ... ...

二、Spring的快速入

开发步骤:
        1、导入spring的相关jar
            4个基本包:beans、core、context、expression
            日志:apache-commons-logging.jar、apache-log4j.jar(log4j.properties)
        2、定义Bean
            public class CustomerDaoImpl implements CustomerDao {
                @Override
                public void save() {
                    System.out.println("save running......");
                }
            }
        3、配置xml文件
            习惯将该xml文件命名为applicationContext.xml  存放到src下
            约束头位置:spring-framework-4.2.4.RELEASE/docs/spring-framework-reference/html/xsd-configuration.html
            
            <?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="customerDao" class="com.itheima.dao.impl.CustomerDaoImpl"></bean>

</beans>
            
        4、通过spring客户端代码获得Bean对象
            ApplicationContext app = new ClassPathXmlApplicationContext("applicationContext.xml");
            CustomerDao customerDao = (CustomerDao) app.getBean("customerDao");
            customerDao.save();

  

三、Spring的配置

<!--
        1、bean的基本配置(重点)
            id: 产生对象在spring容器中的唯一标识
            class: 要创建的bean的全限定名
            scope: bean的作用域,取值很多
                singleton(默认值): 单例
                prototype: 原型,多例
                
            Spring创建Bean的时机?
                singleton:加载spring配置文件时 Bean创建
                prototype:当getBean时创建Bean对象
                
            <bean id="customerDao" class="com.itheima.dao.impl.CustomerDaoImpl" scope="prototype"></bean>
            
     -->
    
     <!-- 2、bean的初始化和销毁方法配置(了解)
             init-method:指定该Bean的初始化方法
             destroy-method:指定该Bean的销毁方法
             
             <bean id="customerDao" class="com.itheima.dao.impl.CustomerDaoImpl" init-method="init" destroy-method="destroy"></bean>
     -->
    
     <!-- 3、bean的实例化三种方式
             3.1  通过无参构造创建bean(有参构造)  重点
             3.2  通过静态工厂方式(工厂方法是静态的) (了解了解了解)
                 <bean id="customerDao" class="com.itheima.factory.StaticBeanFactory" factory-method="getCustomerDao"></bean>
             3.3  通过实例工厂方式  (了解了解了解)
                 <bean id="beanFactory" class="com.itheima.factory.InstanceBeanFactory"></bean>
                 <bean id="customerDao" factory-bean="beanFactory" factory-method="getCustomerDao"></bean>
      -->
      
    
     <!-- 配置dao -->
     <bean id="customerDao" class="com.itheima.dao.impl.CustomerDaoImpl"></bean>
      
     <!-- 4、配置service (依赖注入DI)
             property:属性
                 name:Service对象的属性名称  setXxx中xxx
                 ref:根据id引用spring容器中的Bean对象
                 
             依赖注入:DI
                 解释:service要想正常的工作 内部必须依赖于dao
                 
             问题:IoC与DI是否是同一件事情?
                 IoC:控制反转,在spring容器中创建Bean
                 DI:依赖注入,在创建Bean时 将数据注入到Bean中
                 
             <bean id="customerService" class="com.itheima.service.impl.CustomerServiceImpl">
                 <property name="customerDao" ref="customerDao"></property>
             </bean>
     -->
    
    
    <!-- 5、依赖注入的数据类型
            5.1 对象引用(ref)
                <property name="customerDao" ref="customerDao"></property>
            5.2 普通属性
                <property name="username" value="zhangsan"></property>
            5.3 集合属性
                见下面
                
     -->
    <bean id="customerService" class="com.itheima.service.impl.CustomerServiceImpl">
         <property name="customerDao" ref="customerDao"></property>
         <property name="username" value="zhangsan"></property>
         <property name="strList">
             <list>
                 <value>aaa</value>
                 <value>bbb</value>
             </list>
         </property>
         <property name="customerList">
             <list>
                 <ref bean="customer1"/>
                 <ref bean="customer2"/>
             </list>
         </property>
         <property name="customerMap">
             <map>
                 <entry key="c1" value-ref="customer1"></entry>
                 <entry key="c2" value-ref="customer2"></entry>
             </map>
         </property>
         <property name="props">
             <props>
                 <prop key="p1">ppp1</prop>
                 <prop key="p2">ppp2</prop>
             </props>
         </property>
     </bean>
    
     <!-- Customer -->
     <bean id="customer1" class="com.itheima.domain.Customer">
         <property name="name" value="tom"></property>
         <property name="age" value="18"></property>
     </bean>
     <bean id="customer2" class="com.itheima.domain.Customer">
         <property name="name" value="jeck"></property>
         <property name="age" value="19"></property>
     </bean>
    
    <!--
        6、注入的三种方式
            6.1 set方法注入(重点)
            6.2  构造方法注入
            6.3 p命名空间注入 (也是set方法注入)
                对set方法注入的简化操作
                在xml中引入p命名空间
                    xmlns:p="http://www.springframework.org/schema/p"
                
     -->
    
    <bean id="customerService2" class="com.itheima.service.impl.CustomerServiceImpl2" p:username="lucy" p:customer-ref="customer2">
        <constructor-arg name="name" value="zhangsan"></constructor-arg>
        <constructor-arg name="age" value="20"></constructor-arg>
        <constructor-arg name="customer" ref="customer1"></constructor-arg>
    </bean>
  

四、Spring相关API

@Test
    //测试Spring的相关API
    public void test8(){
        /*
         * 加载spring配置文件 创建spring容器
         *     ApplicationContext接口的实现主要3个
         *         ClassPathXmlApplicationContext:
         *             ClassPath:该配置存在位置,类路径(src)
         *             Xml:加载配置的文件类型
         *         FileSystemXmlApplicationContext:
         *             FileSystem:该配置存在位置,文件系统(磁盘路径)
         *             Xml:加载配置的文件类型
         *
         *         AnnotationConfigApplicationContext:
         *             AnnotationConfig: 注解配置 当消除xml配置文件后 配置使用注解替代后
         *
         *         
         */
        ApplicationContext app = new ClassPathXmlApplicationContext("applicationContext.xml");
        //ApplicationContext app = new FileSystemXmlApplicationContext("D:\\java\\workspace\\workspack-heima311\\SpringDay01\\src\\applicationContext.xml");
        //ApplicationContext app = new AnnotationConfigApplicationContext();
        
        /*
         * getBean有两种方式:
         *     根据id从容器中获得Bean
         *     根据类型从容器中获得Bean
         */
        CustomerService customerService = (CustomerService) app.getBean("customerService2");
        //CustomerService customerService = app.getBean(CustomerService.class);
        customerService.save();
    }

Spring知识点小结(一)的更多相关文章

  1. Spring知识点小结汇总

    Spring部分 1.谈谈你对spring IOC和DI的理解,它们有什么区别? IoC Inverse of Control 反转控制的概念,就是将原本在程序中手动创建UserService对象的控 ...

  2. Spring知识点小结(四)

    一.JdbcTemplate(jdbc模版--抽取的工具) web阶段DBUtils:        QueryRunner runner = new QueryRunner(dataSource); ...

  3. Spring知识点小结(三)

    一.aop的简介 aop:面向切面编程    aop是一种思想,面向切面编程思想,Spring内部提供了组件对aop进行实现    aop是在运行期间使用动态代理技术实现的思想    aop是oop延 ...

  4. Spring知识点小结(二)

    一.配置非自定义的Bean(数据源DataSource模型) DBCP数据源:        导入dbcp的jar包:dbcp+pool+connector                代码实现:  ...

  5. 【SpringBoot MQ 系列】RabbitMq 核心知识点小结

    [MQ 系列]RabbitMq 核心知识点小结 以下内容,部分取材于官方教程,部分来源网络博主的分享,如有兴趣了解更多详细的知识点,可以在本文最后的文章列表中获取原地址 RabbitMQ 是一个基于 ...

  6. SpringBoot 系列教程之事务隔离级别知识点小结

    SpringBoot 系列教程之事务隔离级别知识点小结 上一篇博文介绍了声明式事务@Transactional的简单使用姿势,最文章的最后给出了这个注解的多个属性,本文将着重放在事务隔离级别的知识点上 ...

  7. disruptor笔记之四:事件消费知识点小结

    欢迎访问我的GitHub https://github.com/zq2599/blog_demos 内容:所有原创文章分类汇总及配套源码,涉及Java.Docker.Kubernetes.DevOPS ...

  8. C++重要知识点小结---3

    C++重要知识点小结---1:http://www.cnblogs.com/heyonggang/p/3246631.html C++重要知识点小结---2:http://www.cnblogs.co ...

  9. C++重要知识点小结---2

    C++重要知识点小结--1 :http://www.cnblogs.com/heyonggang/p/3246631.html 1.C++允许程序员声明一个不能有实例对象的类,这样的类惟一的用途是被继 ...

随机推荐

  1. APNS 证书生成注意事项

    APNS证书导出pem: openssl x509 -in aps_development.cer -inform der -out yourCertName.pem APNS证书密钥导出: 先在&q ...

  2. spring+springmvc+hibernate整合遇到的问题

    spring+springmvc+hibernate整合遇到的问题2016年10月20日 23:24:03 守望dfdfdf 阅读数:702 标签: ssh学习经历的异常exception异常框架更多 ...

  3. WinSock WSAEventSelect 模型

    在前面我们说了WSAAsyncSelect 模型,它相比于select模型来说提供了这样一种机制:当发生对应的IO通知时会立即通知操作系统,并调用对应的处理函数,它解决了调用send和 recv的时机 ...

  4. Django——stark组件

    stark组件是仿照django的admin模块开发的一套组件,它的作用是在网页上对注册的数据表进行增删改查操作. 一.配置 1.创建stark应用,在settings.py中注册stark应用 st ...

  5. 【HTML&CSS】文本的基本处理

    其实在写这篇博客的时候已经学了很久,也写了不少代码,特别是很枯燥的看完整个html部分,因为不带有CSS写出来的东西干巴巴的一点也不好看. 直到展开CSS学习才开来补上博客,嗯,这是个好习惯. 这是运 ...

  6. Git回退到指定节点的版本

    1.获取某个历史版本的id(即change-id,每个版本唯一) 方法1:使用git log命令查看所有的历史版本,输入q便可退出. git log 方法2:使用gitk图形化界面查看节点信息.(在安 ...

  7. Centos6配置samba服务器并批量添加用户和文件夹

    一.需求 局域网内有若干用户,所有用户访问一个共享目录 每个用户在共享目录里有自己的文件夹 每个用户都可以读取其他人的文件夹 每个用户只能对自己的文件夹有写入权限 所有用户都属于filesgroup组 ...

  8. Arcgis flex 切片地图麻点

    在arcgis server中发布地图切片完成后,有时候在访问地图的时候会出现很多麻点, 其实是你切片的时候没有注意到一些选项.... 默认的切片是PNG8,说到这可能就明白了吧,png8的色彩范围: ...

  9. 《ArcGIS Runtime SDK for Android开发笔记》——(11)、ArcGIS Runtime SDK常见空间数据加载

    ArcGIS Runtime SDK for Android 支持多种类型空间数据源.每一种都提供了相应的图层来直接加载,图层Layer是空间数据的载体,其主要继承关系及类型说明如下图所示: 转载请注 ...

  10. 上传文件到Maven仓库

    1.上传jar到本地仓库 mvn install:install-file -DgroupId=org.csource -DartifactId=fastdfs-client-java -Dversi ...