Spring框架入门级技术

Spring框架

Spring概述

Spring是一个分层的 JavaSE/JavaEE 一站式(full-stack)轻量级开源框架。
Spring兴起于2003,前身来源于EJB架构,作者Rod Johnson

Spring的优势
  • 方便解耦,简化开发

  • AOP编程的支持

  • 声明式事务的支持 编码式事务 转账 transfer方法手动植入进入 销户 DeleteAccount 事务支持

    提前在Spring的主配置文件/注解中applicationContext.xml去配置transfer deleteAccount

  • 方便程序的测试 Spring集成Junit

  • 方便集成各种优秀的框架 Jfinal、Mybatis Struts2 Hibernate…

  • 框架中的源码是需要借助学习的,经典的设计思想。设计模式…优秀的案例…

Spring Framework的体系架构

在这里插入图片描述

Spring环境搭建以及HelloWorld案例
schema讲解
<beans  xmlns="http://www.springframework.org/schema/beans"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xmlns:context="http://www.springframework.org/schema/context"
        xsi:schemaLocation="http://www.springframework.org/schema/beans"   
                           "http://www.springframework.org/schema/beans/spring-beans.xsd"
       "http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context.xsd"
       ">

配置约束的目的是为了在xml文件中进行配置的时候,能够按照提前约定好的约束进行标签的检验。

  • xmlns=“http://www.springframework.org/schema/beans”

xml ns—>namespace 命名空间
一般配置一个xml文件最多只能有一个匿名的命名空间
对于Spring框架来说,它把匿名的命名空间给beans使用了 。如果需要配置的话,xmlns:aa
那么当使用beans中的标签的时候,需要在aa:bean
因为我们要使用bean的约束,xml整体的约束采用schema约束,所以我们配置schema约束地址,
schemaLocation 约束方式采用key-value键值对的方式,
schemaLocation 约束依赖于http://www.w3.org/2001/XMLSchema-instance
所以xml配置首先http://www.w3.org/2001/XMLSchema-instance给他起了个别名 别名的名字叫xsi

IOC控制反转

Inversion Of Control 控制反转,将对象的创建权交给了Spring,提前在Spring容器把该对象信息构建好,我们可以通过Spring容器调用getBean(“name属性或者id属性”)获取到对应的对象。

  • ApplicationContext,使用它需要借助于它的实现类ClassPathXmlApplicationContext,默认会在创建的时候,将内部配置的所有对象全部加载到Spring容器中
  • ClassPathXmlApplicationContext,将来我们可以从classpath的目录下加载到Spring主配置文件。
bean元素的配置和创建

凡是交给Spring容器管理的对象,都可以通过bean元素进行配置。

Spring的本质就是对象管理容器】 Map

name属性和class属性
  • name属性:作用就是给创建的对象起一个名字,将来在应用中通估这个名字获取到这个对象

  • class属性:创建该对象映射的那个类 反射机制 类名 = 包名+类名

  • id属性: id属性和name属性的作用是一样的,name属性的名称值可以重复,但是id值不允许重复,唯一
           id属性值不能含有特殊字符,但是在使用中,一般把name属性值在容器的设置也是唯一的。

bean元素的创建

bean元素的创建默认采用空参构造方法创建对象。如果没有空参构造,那么一般就会报错。

  • 空参构造方法创建

<bean id=“user” class=“com.zhiyou100.pojo.User”></bean>

  • 静态工厂方式创建

<bean id=“userStatic” class=“com.zhiyou100.pojo.BeanFactory” factory-method=“getStaticBean”></bean>

  • 实例工厂方式创建

<bean name = “UserBeanFactory” class=“com.zhiyou100.pojo.BeanFactory”></bean>
<bean name = “userFactory” factory-bean=“UserBeanFactory” factory-method=“getUser”></bean>

scope属性

scope 范围 指的设置bean元素创建对象的方式

  • singleton: 单例模式,当前的对象在容器中只会创建一次,容器只会保存该类的一个对象 默认采用的是单例模式

    Spring认为一个bean就应该有且仅有一个对象

  • prototype :多例模式,当前对象在容器中每次调用getBean返回的都是新对象。

    在Structs2容器中,采用的是多例模式。每一个action都要配置成prototype

  • request 请求域中

  • session 会话 在服务器中

  • globalSession 全局的,跨session存储的

bean元素的生命周期

生命周期 : 从生到死。从创建到消亡

  • init-method:初始化方法 初始化bean对象

    ​ init-method bean对应的空参构造方法

  • destroy-method:销毁方法 当对象销毁的时候,会执行该方法。

    • 第一种情况,就是这个对象长期不用,GC就认为它是一个垃圾,GC就会清除掉
    • 第二种情况,当这个容器销毁/关闭的时候,存储在该容器中的bean对象也会销毁。
  • bean元素默认是交给Spring管理的。但是如果你把该bean元素的scope属性设置为多例模式,那么Spring只管生,不管死。在单例模式下,Spring管理bean元素的整个生命周期。

依赖注入DI

DI  Dependency Injection 依赖注入 ,有IOC环境的支持,Spring创建这个类的过程中,Spring将类的依赖的属性设置进去。

注入方式

如果是基本类型(String),则使用value,如果是引用类型使用ref

①set方法注入

该对象对应的类中必须有set方法,否则无法正常注入。

例子如下:
pojo实体类:(字段【构造方法和GetSet方法可自行配置】)
user类

 private Integer id;
 private String username;
private String address;
private Date birthday;
private boolean gender;
private Role role;

role类

 private Integer id;
    private String roleNmae;
    private String roleDesc;
    private String roleUpdate;
<!--方式一:set方法注入  -->
    <bean name="user" class="com.is.pojo.User">
        <property name="address" value="北京"/>
        <property name="birthday" ref="date"/>
        <property name="gender" value="true"/>
        <property name="id" value="1"/>
        <property name="username" value="小孙"/>
        <property name="role" ref="role"/>
      </bean>
    <!--配置role对象-->
    <bean id="role" class="com.is.pojo.Role">
        <property name="id" value="1"/>
        <property name="roleDesc" value="打扫卫生"/>
        <property name="roleNmae" value="保洁阿姨"/>
        <property name="roleUpdate" value="2020-05-15"/>
     </bean>
    <!--配置Date对象-->
    <bean id="date" class="java.util.Date"/>

测试:

//创建Spring容器
        ApplicationContext container = new ClassPathXmlApplicationContext("applicationContext.xml");
        //从容器当中获取想要的对象   user
        User user = container.getBean("user", User.class);
        System.out.println(user);
②构造方法注入
<!--方式二  构造方法注入-->
    <bean name="user2" class="com.is.pojo.User">
        <!--
            name 表示的是参数的名称
            value 表示的是参数的值
            ref   表示的是引用的bean对象
            index   表示的是参数的索引
            type    表示的是参数的类型
            Integer id, String username, String address, Date birthday, boolean gender
        -->
        <constructor-arg name="id" value="2" type="java.lang.Integer" index="0"/>
        <constructor-arg name="username" value="小赵" type="java.lang.String" index="1"/>
        <constructor-arg name="address" value="河南" type="java.lang.String" index="2"/>
        <constructor-arg name="birthday" ref="date" type="java.util.Date" index="3"/>
        <constructor-arg name="gender" value="true" type="boolean" index="4"/>
        <constructor-arg name="role" ref="role" type="com.is.pojo.Role" index="5"/>
    </bean>

测试:

		 //创建Spring容器
        ApplicationContext container = new ClassPathXmlApplicationContext("applicationContext.xml");
		/*构造器注入*/
        User user2 = container.getBean("user2", User.class);
        System.out.println(user2);
③P命名空间注入
<!--方式三   p名称空间注入-->
    <bean name="user3" class="com.is.pojo.User" P:id="3"
          P:address="信仰" P:birthday-ref="date" P:gender="false" P:username="小花" P:role-ref="role">
    </bean>

测试

 		//创建Spring容器
        ApplicationContext container = new ClassPathXmlApplicationContext("applicationContext.xml");
		/*p命名空间注入*/
        User user3 = container.getBean("user3", User.class);
        System.out.println(user3);
④Spel表达式注入
 <!--方式四  SPEL  表达式注入-->
    <bean name="user4" class="com.is.pojo.User">
        <property name="id" value="#{role.id==2?1:2}"/>
        <property name="username" value="#{user.username.equals('小孙')?'小名':'小孙'}"/>
        <property name="address" value="#{user.address.equals('北京')?'河南':'北京'}"/>
        <property name="birthday" ref="date"/>
        <property name="gender" value="#{user.gender==true?true:false}"/>
        <property name="role" ref="role"/>
      </bean>

测试:

		//创建Spring容器
        ApplicationContext container = new ClassPathXmlApplicationContext("applicationContext.xml");
		/*SPEL表达式注入*/
        User user4 = container.getBean("user4", User.class);
        System.out.println(user4);
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
Spring Integration是一个基于Spring框架的集成框架,它提供了一种轻量级的、可扩展的、易于使用的方式,用于在应用程序中集成异构系统和技术。 下面是Spring Integration入门的步骤: 1. 添加依赖:在Maven或Gradle项目中添加Spring Integration的依赖。 2. 配置Spring Integration:在Spring配置文件中配置Spring Integration的组件,如消息通道、消息转换器、消息适配器等。 3. 创建消息处理流程:将各种消息处理器(包括过滤器、路由器、转换器等)按照一定顺序组合成消息处理流程。 4. 启动Spring Integration:通过启动Spring容器来启动Spring Integration。 以下是一个简单的Spring Integration示例: 1. 添加依赖: ``` <dependency> <groupId>org.springframework.integration</groupId> <artifactId>spring-integration-core</artifactId> <version>5.1.3.RELEASE</version> </dependency> ``` 2. 配置Spring Integration: ``` <bean id="inputChannel" class="org.springframework.integration.channel.DirectChannel"/> <bean id="outputChannel" class="org.springframework.integration.channel.DirectChannel"/> <bean id="messageConverter" class="org.springframework.integration.json.ObjectToJsonTransformer"/> <bean id="messageHandler" class="org.springframework.integration.handler.LoggingHandler"/> <int:channel id="inputChannel"/> <int:channel id="outputChannel"/> <int:transformer input-channel="inputChannel" output-channel="outputChannel" ref="messageConverter"/> <int:logging-channel-adapter id="logger" log-full-message="true" level="INFO" channel="outputChannel"/> ``` 3. 创建消息处理流程: ``` <int:gateway id="gateway" service-interface="com.example.MyGateway" default-request-channel="inputChannel"/> <int:chain input-channel="inputChannel" output-channel="outputChannel"> <int:filter expression="payload != 'ignore'"/> <int:router expression="payload"/> <int:transformer expression="'Hello, ' + payload"/> <int:service-activator ref="messageHandler"/> </int:chain> ``` 4. 启动Spring Integration: ``` ApplicationContext context = new ClassPathXmlApplicationContext("spring-integration.xml"); MyGateway gateway = context.getBean(MyGateway.class); String result = gateway.process("World"); System.out.println(result); ``` 这是一个简单的Spring Integration示例,它演示了如何创建一个消息处理流程,并启动Spring Integration来处理消息。如果你想深入了解Spring Integration,可以参考官方文档或者相关书籍。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值