Spring学习总结(一)

一、常用依赖

<dependencies>
    <!--mybatis依赖-->
        <dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis</artifactId>
            <version>3.5.2</version>
        </dependency>
    <!--mysql依赖-->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>8.0.25</version>
        </dependency>
    <!--junit依赖-->
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.13.1</version>
            <scope>test</scope>
        </dependency>
    <!-- https://mvnrepository.com/artifact/org.springframework/spring-webmvc -->
    <!--spring-webmvc依赖-->
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-webmvc</artifactId>
        <version>5.3.8</version>
    </dependency>
    </dependencies>

二、DI依赖注入(Dependency Injection)

首先了解控制反转(IoC)设计思想:IoC思想

在xml文件中显示注入

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="sch.atbjut.pojo.Address">
        <property name="address" value="北京"/>
    </bean>

    <bean id="student" class="sch.atbjut.pojo.Student">
        <!--1.普通值注入。value-->
        <property name="name" value="北京工业大学"/>
        <!--2.Bean注入。ref-->
        <property name="address" ref="address"/>
        <!--3.数组注入。ref-->
        <property name="books">
            <array>
                <value>红楼梦</value>
                <value>西游记</value>
                <value>三国演义</value>
                <value>水浒传</value>
            </array>
        </property>
        <!--4.List注入。-->
        <property name="hobby">
            <list>
                <value>听歌</value>
                <value>写代码</value>
                <value>跑步</value>
            </list>
        </property>
        <!--5.Map注入。-->
        <property name="card">
            <map>
                <entry key="身份证" value="111111111"/>
                <entry key="银行卡" value="222222222"/>
            </map>
        </property>
        <!--6.Set注入-->
        <property name="games">
            <set>
                <value>lol</value>
                <value>csgo</value>
            </set>
        </property>
        <!--7.null-->
        <property name="wife">
            <null/>
        </property>
        <!--8.Properties-->
        <property name="info">
            <props>
                <prop key="学号">S2019xxx</prop>
                <prop key="姓名">黄xx</prop>
                <prop key="性别"></prop>
                <prop key="年龄">18</prop>
            </props>
        </property>
    </bean>

</beans>

实体类

package sch.atbjut.pojo;

import java.util.*;

public class Student {
    private String name;
    private Address address;
    private String[] books;
    private List<String> hobby;
    private Map<String ,String > card;
    private Set<String> games;
    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> getHobby() {
        return hobby;
    }

    public void setHobby(List<String> hobby) {
        this.hobby = hobby;
    }

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

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

    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 + '\n' +
                "address=" + address.toString() +'\n' +
                "books=" + Arrays.toString(books) +'\n' +
                "hobby=" + hobby +'\n' +
                "card=" + card +'\n' +
                "games=" + games +'\n' +
                "wife='" + wife + '\n' +
                "info=" + info +
                '}';
    }
}

三、Bean的自动装配

自动装配是Spring满足bean依赖一种方式!
Spring会在上下文中自动寻找,并自动给bean装配属性。

在Spring中有三种装配方式:

  1. 在xml文件中显示装配【二的内容】
  2. 在java中显示配置
  3. 隐式的自动装配bean【重要】

注解说明

@Autowired

自动装配,通过byName、byType。如果不能唯一自动装配上属性,则需要通过@Qualifier(value=“xxx”)

@Nullable

字段标记了这个注解,说明这个字段可以为null

@Resource

作用与autowired注解相同

@Component

组件,放在类上,说明这个类被Spring管理了,就是bean!

@Scope

作用域,通过@Scope(“xxx”) 【singleton、prototype、request、session、application、websocket】

@Value

赋值,等价于

<property name="name" value="xxx"/>
【@Component】的几个衍生注解:
dao:【@Repository】
service:【@Service】
controller【@Controller】
这四个注解功能都是一样的,都是代表将某个类注册到Spring的IoC容器中,装配Bean!

【注】使用注解需要导入context约束,增加注解的支持!

<?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:component-scan base-package="sch.atbjut.pojo"/>
    <context:annotation-config/>
    
</beans>

注解小结

xml与注解:

  1. xml更加万能,适用于任何场合!维护简单方便;
  2. 注解不是自己类使用不了,维护相对复杂。

xml与注解最佳实践

  1. xml用来管理bean;
  2. 注解只负责完成属性的注入;
  3. 我们在使用的过程中,只需要注意一个问题:必须开启注解支持,让注解生效。

四、使用Java配置类的方式配置Spring

我们完全不适用Spring的xml配置了,全权交给java来做!
JavaConfig是Spring

配置类

package sch.atbjut.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import sch.atbjut.pojo.User;

//@Configuration也会被Spring容器托管,注册到容器中,因为他本来就是一个@Component
//@Configuration代表这是一个配置类,相当于beans.xml
@Configuration
@ComponentScan("sch.atbjut.pojo")  //扫描包下的所有类
@Import(KuangCongfig2.class)   //Import注解导入其他配置类
public class KuangCongfig {
    //注册一个bean,就相当于我们之前写的一个bean标签
    //方法名=bean标签中的id
    //返回值类型=bean标签中的class属性
    @Bean
    public User getUser(){
        return new User();
    }
}

实体类

package sch.atbjut.pojo;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

@Component
public class User {
    @Value("唐僧")
    private String name;

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

    public User() {
    }

    public String getName() {
        return name;
    }

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

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

测试类

import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import sch.atbjut.config.KuangCongfig;
import sch.atbjut.pojo.User;


public class Mytest {
    @Test
    public void test(){
        ApplicationContext context = new AnnotationConfigApplicationContext(KuangCongfig.class);
        User user = context.getBean("getUser", User.class);
        System.out.println(user);
    }
}

这种纯java的配置方式,在SpringBoot中随处可见!

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值