zoukankan      html  css  js  c++  java
  • .Net转Java自学之路—Spring框架篇一(IOC入门)

    Spring概述:

      spring是一个一站式轻量级的开源框架。spring核心主要有两部分:AOP(面向切面)、IOC(控制反转)。

      aop:卖你想切面编程。扩展功能不修改源代码实现。

      ioc:控制反转。当类中有非静态方法,调用该方法时需要创建类的对象。而spring把对象的创建不是通过new的方式实现,而是交给spring配置创建类的对象。

    Spring一站式框架:

      spring在JavaEE三层结构中,每一层都提供不同的解决技术。

      web层:SpringMVC
      service层:spring的ioc
      dao层:spring的jdbcTemplate

    IOC:

      把对象的创建交给Spring进行管理。ioc操作分为俩部分:ioc的配置文件方式、ioc的注释方式。

      ioc底层原理使用技术:1、xml配置文件。2、工厂设计模式。3、dom4j解决xml。4、反射。

    IOC入门案例:

      1、导入jar包。Java的特点都具有三个jar包:
          spring-beans-RELEASE.jar
          spring-beans-RELEASE-javadoc.jar
          spring-beans-RELEASE-sources.jar。
        在做spring最基本的功能时,导入四个核心jar包即可:
          spring-beans-RELEASE.jar
          spring-context-RELEASE.jar
          spring-core-RELEASE.jar
          spring-expression-RELEASE.jar
        支持日志输出的jar包:
          commons-logging.jar
          log4j.jar

      2、创建类、方法。

    public class Test{
        public void method(){
            //code...
        }
    }

      3、创建spring配置文件,配置创建类。

        》Spring核心配置文件名称和位置不是固定的。建议放在src下applicationContext.xml
        》引入Schema约束。

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

        》配置对象创建。

    <?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="test" class="Test类全路径"></bean>
    
    </beans>

      4、加载Spring配置文件。

    ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");

      5、得到配置创建的对象。

    Test test=(Test)context.getBean("test");
    test.method();

    Spring的bean管理(xml配置文件):

      bean实例化方式:在spring中通过配置文件创建对象。

      实例化的三种方式:

        1、使用类的无参构造函数创建:若类中没有无参构造函数,会报异常。
        2、使用静态工厂创建:在工厂类中创建静态方法,返回类对象。

    <bean id="test" class="Test类全路径" factory-method="method"></bean>

        3、使用实例工厂创建:创建非静态方法,返回类对象。需要先配置创建工厂类对象。

    <bean id="testFactory" class="TestFactory类全路径"></bean>
    <bean id="test" factory-bean="testFactory" factory-method="method"></bean>

      bean标签常用属性:

        id:id属性值名称任意命名(非中文及特殊符号)。根据id值得到配置的对象。
        class:创建对象所在类的全路径。
        name:功能和id属性一致。但name属性值可包含特殊符号。
        scope:bean的作用范围。属性值都有:
          singleton:默认值,单例的。
          prototype:多例的。
          request:web项目中,Spring创建一个Bean对象,将对象存入request域中。
          session:web项目中,Spring创建一个Bean对象,将对象存入session域中。
          globalSession:web项目中,应用在Porlet环境,若没有Porlet环境,那么globalSession就相当于session 。

      属性注入:

        创建对象时,向类中的属性设置值。

        属性注入的三种方式:

          1、使用set方法注入:

    <bean id="test" class="Test类全路径">
        <property name="类中属性名称" value="属性值">
    </bean>
    <!-- 注入对象类型属性 -->
    <bean id="testDao" class="TestDao类全路径"></bean>
    <bean id="testService" class="TestService类全路径">
        <property name="属性名称(对象名称)" ref="testDao(要注入的对象)">
    </bean>

          2、有参构造函数注入:

    <bean id="test" class="Test类全路径">
        <constructor-arg name="属性名称" value="属性值"></constructor-arg>
    </bean>

          3、使用接口注入:在Spring框架中,不支持接口注入。

        名称空间 p 注入:

    <!-- 在名称空间p注入时需要在schema约束中需要添加 -->
    xmlns:p="http://www.springframework.org/schema/p"
    
    <bean id="test" class="Test类全路径" p:属性名称="注入属性值"></bean>

        Spring注入复杂数据:

          数组类型注入:

    <bean id="test" class="Test全路径">
        <property name="属性名">
            <list>
                <value>属性值1</value>
                <value>属性值2</value>
            </list>
        </property>
    </bean>

          List 类型注入:

    <bean id="test" class="Test全路径">
        <property name="属性名">
            <list>
                <value>属性值1</value>
                <value>属性值2</value>
            </list>
        </property>
    </bean>

          Map 类型注入:

    <bean id="test" class="Test全路径">
        <property name="属性名">
            <map>
                <entry key="键1" value="值1"></entry>
                <entry key="键2" value="值2"></entry>
            </map>
        </property>
    </bean>

          Properties 类型注入:

    <bean id="test" class="Test全路径">
        <property name="属性名">
            <props>
                <prop key="键1">value1</prop>
                <prop key="键2">value2</prop>
            </props>
        </property>
    </bean>

    ioc 和 di 区别:

      IOC:控制反转。把对象创建交给Spring进行配置。
      DI:依赖注入。向类中属性设置属性值。
      依赖注入不能单独存在,需要在IOC的基础之上完成操作。

    Spring 整合Web项目原理:

      加载Spring核心配置文件,需要new ClassPathXmlApplicationContext("");功能来实现,效率低下。

      为解决该问题:

        1、把加载配置文件和创建对象过程,放在服务器启动时来完成。

        2、底层主要使用ServletContext对象、监听器。

          在服务器启动时,为每个项目创建一个ServletContext对象,在ServletContext对象创建时,使用监听器可以具体到ServletContext什么时候创建。使用监听器监听ServletContext对象创建时,加载Spring配置文件,把配置文件配置的对象创建。把创建的对象放到ServletContext域对象中。

        3、在Spring框架中已经封装了一个监听器,使用时只需要配置监听器即可。

          在web.xml中配置监听器,在配置监听器之前需要导入一个Spring整合的jar包:spring-web.release.jar

    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>

          配置完成监听器之后,需要指定加载spring配置文件的位置。

    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath:配置文件路径</param-value>
    </context-param>
  • 相关阅读:
    tmux 鼠标滚动
    宝藏主题 cnblogsthemesilence
    数组乱序初始化:sorry, unimplemented: nontrivial designated initializers not supported
    SSH 连接 WSL
    移动硬盘变成 RAW 格式
    Linux 终端快捷键
    Adaptive AUTOSAR 学习笔记 1 概述、背景、AP CP 对比区别
    Qt扫盲篇
    Qt(C++)之实现风行播放器界面
    Qt之统一的UI界面格式基调,漂亮的UI界面
  • 原文地址:https://www.cnblogs.com/zltao/p/10511060.html
Copyright © 2011-2022 走看看