Spring学习笔记(第一篇)

Spring学习笔记(第一篇)

1.Spring的优点

  1. 轻量级、非入侵式的框架
  2. 控制反转(IOC) 、面向切面编程(AOP)
  3. 支持事物的处理,对框架整合的

2.Spring的七大模块

在这里插入图片描述

3.Ioc

主要思想:对象由Spring来创建,管理,装配。
第一个程序:
实体类:

package com.navy.pojo;

public class User {
    private String name;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    @Override
    public String toString() {
        return "User{" +
                "name='" + name + '\'' +
                '}';
    }
}

bean.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"
        xmlns:context="http://www.springframework.org/schema/context"
        xsi:schemaLocation="http://www.springframework.org/schema/beans
        https://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context
        https://www.springframework.org/schema/context/spring-context.xsd">
    <context:annotation-config/>


    <bean id="user" class="com.navy.pojo.User">
        <property name="name" value="navy"/>
    </bean>
</beans>

测试方法:

public class MyTest {
    public static void main(String[] args) {
        ApplicationContext context = new ClassPathXmlApplicationContext("bean.xml");
        User user = context.getBean("user",User.class);
        System.out.println(user.toString());
    }
}

第二种赋值:

public class User {
    private String name;

    public User(String name) {
        this.name = name;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    @Override
    public String toString() {
        return "User{" +
                "name='" + name + '\'' +
                '}';
    }
}

bean.xml文件:

    <bean id="user" class="com.navy.pojo.User">
        <constructor-arg name="name" value="heihei"/>
    </bean>

4.Spring配置

4.1别名
<alias name="user" alias="user2"/>
4.2bean的配置
    <bean id="user" class="com.navy.pojo.User" name="user3">
        <constructor-arg value=""/>
        <property name="name" value="jjjj"/>
    </bean>

id :bean的唯一标识符
class:bean对象对应的全限定名
name:别名,可以取多个

4.3import

用于将多个配置文件导入为一个。

5.依赖注入

5.1构造器注入
    <bean id="user" class="com.navy.pojo.User">
        <constructor-arg name="name" value="heihei"/>
    </bean>
5.2Set注入

依赖注入:Set注入
依赖:bean对象的创建依赖于容器
注入:bean对象中的所有属性,由容器来注入

实体类:

package com.navy.pojo;
import java.util.*;

public class Student {
    private String name;
    private Address address;
    private String[] books;
    private List<String> hobbys;
    private Set<String> games;
    private Map<String,String> card;

    public Map<String, String> getCard() {
        return card;
    }

    public void setCard(Map<String, String> card) {
        this.card = card;
    }

    private String wife;
    private Properties info;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Address getAddress() {
        return address;
    }

    public void setAddress(Address address) {
        this.address = address;
    }

    public String[] getBooks() {
        return books;
    }

    public void setBooks(String[] books) {
        this.books = books;
    }

    public List<String> getHobbys() {
        return hobbys;
    }

    public void setHobbys(List<String> hobbys) {
        this.hobbys = hobbys;
    }

    public Set<String> getGames() {
        return games;
    }

    public void setGames(Set<String> games) {
        this.games = games;
    }

    public String getWife() {
        return wife;
    }

    public void setWife(String wife) {
        this.wife = wife;
    }

    public Properties getInfo() {
        return info;
    }

    public void setInfo(Properties info) {
        this.info = info;
    }

    @Override
    public String toString() {
        return "Student{" +
                "name='" + name + '\'' +
                ", address=" + address.toString() +
                ", books=" + Arrays.toString(books) +
                ", hobbys=" + hobbys +
                ", wife=" + wife +
                ", info=" + info +
                '}';
    }
}

beans.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">
    <bean id="address" class="com.navy.pojo.Address"/>

    <bean id="student" class="com.navy.pojo.Student">
        <!--1.普通注入-->
        <property name="name" value="navy"/>

        <!--2.bean注入-->
        <property name="address" ref="address"/>

        <!--3.数组注入-->
        <property name="books">
            <array>
                <value>三国演义</value>
                <value>红楼梦</value>
                <value>水浒传</value>
                <value>西游记</value>
            </array>
        </property>

        <!--4.list注入-->
        <property name="hobbys">
            <list>
                <value>听歌</value>
                <value>敲代码</value>
                <value>看电影</value>
            </list>
        </property>

        <!--5.map注入-->
        <property name="card">
            <map>
                <entry key="身份证" value="13131313"/>
                <entry key="银行卡" value="131313131313"/>
            </map>
        </property>

        <!--6.set注入-->
        <property name="games">
            <set>
                <value>lol</value>
                <value>dnf</value>
                <value>chiji</value>
            </set>
        </property>

        <!--7.null注入-->
        <property name="wife">
            <null/>
        </property>

        <!--8.properties注入-->
        <property name="info">
            <props>
                <prop key="学号">2015302728</prop>
                <prop key="性别"></prop>
                <prop key="姓名">navy</prop>
            </props>
        </property>
    </bean>
</beans>

6.bean的作用域

在这里插入图片描述
1.bean的默认作用域为:singleton
2.原型模式:prototype ,每次从容器中get的时候,都会产生一个新的对象。

7.bean的自动装配

Sping的三种装配方式:
1.在xml中显示配置
2.在java中显示配置:用@Autowired注解配置,先按byType,再按byName.,自动装配无法通过一个注解完成时,使用@Qualifier(value="___")去配置,指定一个对象。
3.隐式的自动装配bean

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值