spring框架教程


嘿嘿 testmybatisspring项目,都看的狂神说系列的视频,spring–Javaweb+MVC – springMVC --springBoot–springCloud,加油🚗🚓🚕


Spring 网站https://docs.spring.io/spring-framework/docs/current/reference/html/core.html#beans 🌍🌎🌏

Spring🌻🌼🌷

1 创建环境

1 导入依赖

spring-webmvc spring-jdbc

<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-webmvc</artifactId>
    <version>5.3.11</version>
</dependency>
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-jdbc</artifactId>
    <version>1.2.4</version>
</dependency>

2 优点

轻量级的控制反转和面向切面的框架
在这里插入图片描述

ApplicationContext
ConfigurableApplicationContext
AbstractApplicationContext
AbstractRefreshableApplicationContext
AbstractRefreshableConfigApplicationContext
AbstractXmlApplicationContext
ClassPathXmlApplicationContext

3 application.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
        https://www.springframework.org/schema/beans/spring-beans.xsd">
......
</beans>

2 IOC创建对象的四种方式

1 IOC 无参创建对象

    <bean id="user" class="com.test.pojo.User">
        <property name="name" value="张三"/>
    </bean>

2 IOC 有参创建对象

1 参数下标
    <bean id="user" class="com.test.pojo.User">
<!--        <property name="name" value="张三"/>-->
        <constructor-arg index="0" value="李四"/>
    </bean>
2 参数类型
    <bean id="user" class="com.test.pojo.User">
<!--        <property name="name" value="张三"/>-->
<!--        <constructor-arg index="0" value="李四"/>-->
        <constructor-arg type="java.lang.String" value="王五"/>
    </bean>
3 参数名
    <bean id="user" class="com.test.pojo.User">
<!--        <property name="name" value="张三"/>-->
<!--        <constructor-arg index="0" value="李四"/>-->
<!--        <constructor-arg type="java.lang.String" value="王五"/>-->
        <constructor-arg name="name" value="柳六"/>
    </bean>

3 依赖注入

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-JjLntEng-1637223993595)(C:\Users\Administrator\AppData\Roaming\Typora\typora-user-images\image-20211104105021148.png)]

1 构造器注入

IOC有参构造三种方式

2 setter注入

@Getter
@Setter
@ToString
public class Student {
   
    private String name;
    private Address address;
    private String[] books;
    private List<String> hobbies;
    private Set<String> games;
    private Map<String, String> card;
    private Properties info;
    private String married;
}

@ToString
@Getter
@Setter
public class Address {
   
    private String address;
}
<bean id="student" class="com.test.pojo.Student">
    <!--        基本数据类型-->
    <property name="name" value="ketty"/>
    <!--        引用数据类型-->
    <property name="address" ref="address"/>

    <!--        数组注入-->
    <property name="books">
        <array>
            <value>head first java</value>
            <value>hello spring5!</value>
        </array>
    </property>

    <!--        map注入-->
    <property name="card">
        <map>
            <entry key="歌手" value="许嵩"></entry>
            <entry key="演员" value="赵丽颖"/>
        </map>
    </property>

    <!--        set注入-->
    <property name="games">
        <set>
            <value>王者</value>
            <value>抖音</value>
        </set>
    </property>

    <!--        List注入-->
    <property name="hobbies">
        <list>
            <value>sing</value>
            <value>dance</value>
        </list>
    </property>

    <!--        null空值注入-->
    <property name="married">
        <null></null>
    </property>

    <!--    Properties注入-->
    <property name="info">
        
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值