zoukankan      html  css  js  c++  java
  • ##Spring框架的简单入门,你入门了么?

    Spring框架


    ##1,首先我们来说下spring框架的概念,让你有个大概了解:

      Spring 是分层的 Java SE/EE 应用 full-stack 轻量级开源框架,以 IoC(Inverse Of Control:反转控制)和 AOP(Aspect Oriented Programming:面向切面编程)为内核,提供了展现层 SpringMVC 和持久层 Spring JDBC 以及业务层事务管理等众多的企业级应用技术,还能整合开源世界众多著名的第三方框架和类库,逐渐成为使用最多的 Java EE 企业应用开源框架。
     
    ##2,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
     
    ##3,那么我们在开发中用到spring框架,他有什么优势呢:
      ~1,方便解耦,简化开发
        通过 Spring 提供的 IoC 容器,可以将对象间的依赖关系交由 Spring 进行控制,避免硬编码所造成的过度程序耦合。用户也不必再为单例模式类、属性文件解析等这些很底层的需求编写代码,可以更专注于上层的应用。
      ~2,AOP 编程的支持
        通过 Spring 的 AOP 功能,方便进行面向切面的编程,许多不容易用传统 OOP 实现的功能可以通过 AOP 轻松应付。
      ~3,声明式事务的支持
        可以将我们从单调烦闷的事务管理代码中解脱出来,通过声明式方式灵活的进行事务的管理,提高开发效率和质量。
      ~4,方便程序的测试
        可以用非容器依赖的编程方式进行几乎所有的测试工作,测试不再是昂贵的操作,而是随手可做的事情。
      ~5,方便集成各种优秀框架
        Spring 可以降低各种框架的使用难度,提供了对各种优秀框架(Struts、Hibernate、Hessian、Quartz等)的直接支持。
      ~6,降低 JavaEE API 的使用难度
        Spring 对 JavaEE API(如 JDBC、JavaMail、远程调用等)进行了薄薄的封装层,使这些 API 的使用难度大为降低。
      ~7,Java 源码是经典学习范例
        Spring 的源代码设计精妙、结构清晰、匠心独用,处处体现着大师对 Java 设计模式灵活运用以及对 Java 技术的高深造诣。它的源代码无意是 Java 技术的最佳实践的范例。
     
    ##4,Spring框架的体系结构:
          
    ##5,Spring框架IOC的概念以及作用:
        耦合性(Coupling),也叫耦合度,是对模块间关联程度的度量。耦合的强弱取决于模块间接口的复杂性、调用模块的方式以及通过界面传送数据的多少。模块间的耦合度是指模块之间的依赖关系,包括控制关系、调用关系、数据传递关系。模块间联系越多,其耦合性越强,同时表明其独立性越差( 降低耦合性,可以提高其独立性)。
                 
      解决程序之间的耦合度办法:
      1,添加依赖
          <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-context</artifactId>
                <version>5.0.2.RELEASE</version>
            </dependency>

      2,添加配置文件

        模板:

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

      3,通过创建spring容器对象读取配置文件,然后通过该对象去获取

    //1.使用 ApplicationContext 接口,就是在获取 spring 容器
    ApplicationContext ac = new ClassPathXmlApplicationContext("配置文件名称(.xml)");
    //2.根据 bean 的 id 获取对象
    实体类名称 实体类对象 = ac.getBean("实体类",实体类.class);

    ##6,通过以下几种方式给spring添加依赖注入

      搭建环境:

        以上是各种数值类型的,创建实体类product

       private Integer id;//整数型
        private String name;//字符串型
        private Date pdate;//日期类
        private Character hot;//单字符型
        private List<String> list;//list集合
        private Set<String> set;//set集合
        private String[] strs;//数据类型
        private Map<String ,String>map;//map集合类型
        private Properties properties;//properties类

      ~1,通过构造函数添加依赖注入

      <!--1,用构造函数添加依赖注入-->
        <bean id="product" class="cn.liurui.domain.Product">
        //name也可以用index索引表示,同样可以表示0代表id,1代表name,不过建议大家用name <constructor-arg name="id" value="1"></constructor-arg> <constructor-arg name="name" value="ruirui"></constructor-arg>
        //value只适用于基本数据类型和字符串,其他类型的我们用ref,同时还要另外新建bean
          //<bean id="pdate_st" class="java.util.Date"></bean>
            <constructor-arg name="pdate" ref="pdate_st"></constructor-arg>
            <constructor-arg name="hot" value="是"></constructor-arg>
        //同时如果是集合,我们按照以下方式进行赋值 <constructor-arg name="list"> <set> <value>瑞瑞</value> <value>豪豪</value> <value>哈哈</value> </set> </constructor-arg> <constructor-arg name="set"> <set> <value>瑞瑞</value> <value>豪豪</value> <value>哈哈</value> </set> </constructor-arg> <constructor-arg name="strs"> <set> <value>瑞瑞</value> <value>豪豪</value> <value>哈哈</value> </set> </constructor-arg> <constructor-arg name="map"> <props> <prop key="1">popo</prop> <prop key="2">asdd</prop> <prop key="3">dfgsd</prop> </props> </constructor-arg> <constructor-arg name="properties"> <map> <entry key="1" value="吱吱"></entry> <entry key="2" value="吱asdf"></entry> <entry key="3" value="sdfasdf"></entry> </map> </constructor-arg> </bean>

      测试类:

        @Test
        public void test01(){
            SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            ClassPathXmlApplicationContext ac = new ClassPathXmlApplicationContext("applicationConfig.xml");
            Product p = ac.getBean("product", Product.class);
            Integer id = p.getId();
            System.out.println(id);
            System.out.println(p.getName());
            System.out.println(format.format(p.getPdate()));
            System.out.println(p.getHot());
            System.out.println(p.getList());
            System.out.println(p.getSet());
            System.out.println(p.getStrs()[0]);
            System.out.println(p.getMap());
            System.out.println(p.getProperties());
        }

      打印结果:

    1
    ruirui
    2019-08-10 10:36:17
    是
    [瑞瑞, 豪豪, 哈哈]
    [瑞瑞, 豪豪, 哈哈]
    瑞瑞
    {3=dfgsd, 2=asdd, 1=popo}
    {3=sdfasdf, 2=吱asdf, 1=吱吱}

      ~2,通过set方式添加依赖注入:

    <!--2,用set方式依赖注入-->
        <bean id="product02" class="cn.liurui.domain.Product">
            <property name="id" value="1"/>
            <property name="name" value="ruirui"/>
            <property name="pdate" ref="pdate_st"/>
            <property name="hot" value="是"/>
            <property name="list">
                <set>
                    <value>,,</value>
                    <value>/?</value>
                </set>
            </property>
            <property name="set">
                <set>
                    <value>,,</value>
                    <value>/?</value>
                </set>
            </property>
            <property name="strs">
                <set>
                    <value>,,</value>
                    <value>/?</value>
                </set>
            </property>
            <property name="map">
                <props>
                    <prop key="1">2345</prop>
                </props>
            </property>
            <property name="properties">
                <map>
                    <entry key="1" value="234"></entry>
                </map>
            </property>
        </bean>

    其他的同上

      ~3,通过P名称空间添加依赖注入:

        在配置文件中我们需要添加一条:(红色部分

    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans"
           xmlns:p="http://www.springframework.org/schema/p"
           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">
      <!--3,用p方式注入-->
        <bean id="product03" class="cn.liurui.domain.Product" p:id="1" p:name="ruirui" 
      //红色部分特别注意,上面说过,不是基本数据类型和字符串类型的我们用ref,这里同样
        p:pdate-ref="pdate_st" p:hot="是"></bean>

    输入的结果和上面的一样

  • 相关阅读:
    ACM ICPC 2008–2009 NEERC MSC A, B, C, G, L
    POJ 1088 滑雪 DP
    UVA 11584 最短回文串划分 DP
    POJ 2531 Network Saboteur DFS+剪枝
    UVa 10739 String to Palindrome 字符串dp
    UVa 11151 Longest Palindrome 字符串dp
    UVa 10154 Weights and Measures dp 降维
    UVa 10271 Chopsticks dp
    UVa 10617 Again Palindrome 字符串dp
    UVa 10651 Pebble Solitaire 状态压缩 dp
  • 原文地址:https://www.cnblogs.com/liurui-bk517/p/11330795.html
Copyright © 2011-2022 走看看