Spring框架学习心得(1)

Spring

<dependencies>
    
    <dependency>
        <groupId>org.springframeworkgroupId>
        <artifactId>spring-webmvcartifactId>
        <version>5.3.12version>
    dependency>
    <dependency>
        <groupId>junitgroupId>
        <artifactId>junitartifactId>
        <version>4.12version>
    dependency>
dependencies>

优点:

1.Spring是一个开源的免费的框架(容器)!

2.Spring是一个轻量级、非入侵式的框架!

3.控制反转(IOC)、面向切面编程(AOP)!

4.支持事物的处理,对框架整合的支持!

总结:Spring就是一个轻量级的控制反转(IOC)和面向切面编程(AOP)的框架!

Spring框架学习心得(1)_第1张图片

Spring拓展

Spring Boot:

  • 一个快速开发的脚手架
  • 基于Spring Boot可以快速的开发单个微服务
  • 约定大于配置

Spring Cloud:

  • Spring Cloud 是基于 Spring Boot实现的

现在大多数公司都在用Spring Boot 进行快速开发,学习Spring Boot的前提需要,掌握Spring及SpringMVC

弊端:发展太久,违背了设计理念,配置设计繁琐,人称:配置地狱

1.IOC推导理论

  • UserDao接口
  • UserDaoimpl实现类
  • UseService业务接口
  • UserServiceImpl 业务实现类

2.IOC创建对象的方式

  • 无参构造方法创建对象(默认)!
  • 假设有参构造方法创建对象 构造器创建
1. 
 <bean id="user" class="com.xing.pojo.User">
       <constructor-arg index="0" value="kexing">constructor-arg>
 bean>
      

3.Spring配置

3.1别名


<alias name="user" alias="asdavasvasvas">alias>

3.2 Bean的配置

5.  
<bean id="userT" class="com.xing.pojo.User" name="user2">

3.3 import

这个import,一般用于团队开发使用,他可以将多个配置文件,导入合并伟一个假设,现在项目有多个人开发,这三个人复制不同的类开发,不同的类需要注册在不桐的bean中 用import将所有人的beans.xml合并成一个总的

使用的时候,直接使用总的配置就可以了

<import resource="beans.xml">import>
<import resource="beans2.xml">import>
<import resource="beans3.xml">import>

4.依赖注入

4.1构造器注入

前面已经说过了

↑IOC创建对象方法

4.2set方式注入【重点】

依赖注入:Set注入!

依赖:bean对象的创建依赖于容器!

注入:bean对象中的所有属性,由容器来注入!

​ 【环境依赖】

  1. 复杂类型
public class Address {
   private  String address;

    public String getAddress() {
        return address;
    }

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

2.真实测试环境

public class Student {
    private String name;
    private Address address;
    private String[] books;
    private List<String> hobbys;
    private Map<String,String> card;
    private Set<String> games;
    private Properties info;
    private String wife;
  1. beans.xml
 
<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="student" class="com.xing.pojo.Student">
        
     <property name="name" value="abc">property>
        

    bean>
beans>

4.测试类

 public class MyTests {
    public static void main(String[] args) {
        ApplicationContext context = new ClassPathXmlApplicationContext ("beans.xml");
        Student student =(Student) context.getBean("student");
        System.out.println(student.getName());


    }
}

5.完善信息注入


<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="student" class="com.xing.pojo.Student">
        
     <property name="name" value="abc">property>
        
     <property name="address" ref="address">
     property>
        <property name="books">
            <array>
                <value>红楼梦value>
                <value>西游记value>
                <value>水浒传value>
                <value>三国演绎value>
            array>
        property>
        <property name="hobbys">
            <list>
                <value>听歌value>
                <value>敲代码value>
                <value>看电影value>
            list>
        property>
        
        <property name="card">
            <map>
                <entry key="身份证" value="12312312321">entry>
                <entry key="银行卡" value="45645645645">entry>
            map>
            
        property>
        <property name="games">
            <set>
                <value>lolvalue>
                <value>cocvalue>
                <value>bobvalue>
            set>
        property>
        
        <property name="wife">
            <null>null>
        property>
        
        <property name="info">
            <props>
                <prop key="root">rootprop>
                <prop key="url">urlprop>
                <prop key="password">passwordprop>
            props>
        property>
    bean>
    
    <bean id="address" class="com.xing.pojo.Address">
        <property name="address" value="北京">property>
    bean>

beans>

4.3拓展方式注入


<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:p="http://www.springframework.org/schema/p"
       xmlns:c="http://www.springframework.org/schema/c"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
    
    <bean id="user" class="com.xing.pojo.User" p:name="kobe" p:age="26">bean>
    
    <bean id="user2" class="com.xing.pojo.User"  c:age="18" c:name="curry">bean>
beans>

4.4bean的作用域

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

1.单例模式(Spring默认机制)

<bean id="user2" class="com.xing.pojo.User"  c:age="18" c:name="curry" scope="singleton">bean>

2.原型模式:每次从容器中get的时候,都会产生一个新对象!

<bean id="user2" class="com.xing.pojo.User"  c:age="18" c:name="curry" scope="prototype">bean>

3.其余的request、session、applicaton、这些个只能在web开发中使用到!

5.Bean的自动装配

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

在Spring中有三种装配的方式

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

5.1测试

  1. 环境搭建:一个人有两个宠物(和)

5.2ByName自动装配


<bean id="people" class="com.xing.po.People" autowire="byName">
    <property name="name" value="">property>
bean>

5.3ByType自动装配


<bean id="people" class="com.xing.po.People" autowire="byType">
    <property name="name" value="">property>
bean>

小结:

  • ByName的时候,需要保证所有bean的id唯一,并且这个bean需要和自动注入的属性的set方法唯一!
  • ByType的时候,需要保证所有bean的class唯一,并且这个bean需要和自动注入的属性的类型一致!

5.4使用注解实现自动装配

jdk1.5支持的注解,Spring2.5就支持注解了!!![外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-56C5DozJ-1636552159844)(C:\Users\Star\AppData\Roaming\Typora\typora-user-images\image-20211110193111959.png)]

要使用注解须知:

  1. 导入约束

  2. 配置注解的支持 : context:annotation-config【很重要】
    
    <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">
          <context:annotation-config>context:annotation-config>
    beans>
    

    @Autowired

    直接在类上的属性注释即可,也可以在set方式上使用!

    使用Autowirde我们可以不用编写Set方法了,前提是你这个自动装配的属性在IOC(Spring)中存在,且符合名字ByName!

    科普:

    @Nullable 字段标记了这个注释,说明这个字段可以为Null
    
    @Documented
    public @interface Autowired {
        boolean required() default true;
    }
    

​ 测试代码

public class People {
    //如果显示定义Autowired的required属性为false,说明这个对象可以为Null,否则不可以为空
    @Autowired(required = false)
    private Cat cat;
    @Autowired
    private Dog dog;
    private String name;

如果@Autowired自动装配环境比较复杂,自动装配注解在无法通过一个注解【@Autowired】来实现时,我们可以通过@Qualifier(value=“xxx”) 来配合@Autowired的使用,指定一个唯一的bean对象注入

public class People {
        //如果显示定义Autowired的required属性为false,说明这个对象可以为Null,否则不可以为空
        @Autowired
        private Cat cat;
        @Autowired
        @Qualifier(value = "doggg")
        private Dog dog;
        private String name;

@Resource注解

import javax.annotation.Resource;

public class People {
        @Resource
        private Cat cat;
        @Resource
        private Dog dog;
        private String name;

@Autowired和@Resource的区别:

  • 都是用来自动装配的,都可以放置属性字段上
  • @Autowired通过ByType的方式实现【常用】
  • @Resource默认通过ByName的方式实现,如果找不到就通过ByType实现,如果两个都找不到就报错!【常用】
  • 执行顺序不同:@Autowirde 通过ByType的方式实现 . @Resource默认通过ByName的方式实现,

6.使用注解开发

在Spring4之后,要使用注解开发,必须要保证aop的包导入了

Spring框架学习心得(1)_第2张图片

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

  1. bean

  2. 属性如何注解

    @Component
    public class User {
        //相当于
        @Value("星哥" )
        public String name ;
    
        public String getName() {
            return name;
        }
    
        public void setName(String name) {
            this.name = name;
        }
    }
    
  3. 衍生的注解

    @Component 有几个衍生注解,我们在web开发中,会按照mvc三层架构分层!

    • dao【@Repository】

    • service【@Service】

    • controller【@Controller】

      这四个注解的功能都是一样的,都是代表将某个类注册到Spring中,装配Bean

  4. 自动装配置

    - @Autowirde:自动装配通过ByType,ByName
    - @Resource:自动装配通过ByName,ByType
    - @Nullable:允许属性为null
    - @Component:组件,放在类上,说明这个类被Spring管理了,就是bean!
    
  5. 作用域

    @Component
    @Scope("prototype")
    public class User {
        //相当于
        @Value("星哥" )
        public String name ;
    
        public String getName() {
            return name;
        }
    
        public void setName(String name) {
            this.name = name;
        }
    }
    
  6. 小结:

    xml与注解

    • @Component:组件写在类上面相当于Spring管理了整个类~=bean,取值为类名的小写

    • xml更加万能,适用任何场景的开发,维护简单方便

    • 注解 不是自己类使用不了,维护相对复杂

xml与注解最佳实践:

  • xml用来管理bean;

  • 注解只负责完成属性的注入;

  • 我们在使用的过程中,只需要注意一个问题:必须让注解生效,就需要开启注解的支持

    
        <context:component-scan base-package="com.xing.ko">context:component-scan>
        <context:annotation-config>context:annotation-config>
    

你可能感兴趣的:(Spring,Java,java,spring)