Spring学习笔记_01

4 篇文章 0 订阅

Spring

1. IOC

IOC本质:(Inversion of Control)控制反转

对象交给Spring创建、管理、装配!

  1. IOC创建对象的方式

    1. 无参构造时

    2. 有参构造时

      <!--第一种,下标赋值 Constructor argument index-->
      <bean id="user" class="com.eicoma.javabean.User">
         <constructor-arg index="0" value="晴明"/>
      </bean>
      
      <!--    第二种,通过类型创建-->
          <bean id="user" class="com.eicoma.javabean.User">
              <constructor-arg type="java.lang.String" value="晴明"/>
          </bean>
      
      <!--    第三种,直接通过参数名创建-->
      <bean id="user" class="com.eicoma.javabean.User">
          <constructor-arg name="name" value="晴明"/>
      </bean>
      
  2. 当配置文件被加载时,所配置的所有对象就已经被创建了!

2.DI

DI:依赖注入

  1. 通过set方法注入

    使用set方法依赖注入时,Spring首先实例化对象,然后才实例化所有依赖的对象(先有我才有他们)

  2. 构造器注入(可以有set方法)

    使用构造函数依赖注入时,Spring保证该对象所有依赖的对象先实例化后,才实例化这个对象。(没有他们就没有我原则)

  3. 拓展注入

    1. P命名空间注入
    2. C命名空间注入

3.bean的作用域

ScopeDescription
singleton单例模式下,Bean对象只会创建一次
prototype原型模式下,Bean对象会多次创建(每次从容器中get时都会产生新对象)
requestScopes a single bean definition to the lifecycle of a single HTTP request. That is, each HTTP request has its own instance of a bean created off the back of a single bean definition. Only valid in the context of a web-aware Spring ApplicationContext.
sessionScopes a single bean definition to the lifecycle of an HTTP Session. Only valid in the context of a web-aware Spring ApplicationContext.
applicationScopes a single bean definition to the lifecycle of a ServletContext. Only valid in the context of a web-aware Spring ApplicationContext.
websocketScopes a single bean definition to the lifecycle of a WebSocket. Only valid in the context of a web-aware Spring ApplicationContext.

其余四个在web中使用到

4.自动装配bean

  1. ByName自动装配

    会自动在容器上下文中查找,和自己对象的类的Set方法后面的值对应相同的bean id

    <!--
    Spring自动装配
    
    byName: 会自动在容器上下文中查找,和自己对象set方法后面的参数相对应的bean id
    tip: 在byName下 id必须符合相对应对象的属性类型名
    -->
    <bean id="dog" class="com.eicoma.javabean.Dog"/>
    <bean id="cat" class="com.eicoma.javabean.Cat"/>
    
    <bean id="people" class="com.eicoma.javabean.People" autowire="byName">
        <property name="name" value="晴明"/>
    </bean>
    
  2. ByType自动装配

    会自动在容器上下文中查找,和自己对象的类的属性类型相同的类对应的bean

    <!--
    Spring自动装配
    
    byType: 会自动在容器上下文中查找,和自己对象属性类型相同的bean
    
    tip: 在byType下 同一个类下创建多个bean对象会报错!
    -->
    <bean id="dog22222222" class="com.eicoma.javabean.Dog"/>
    <bean id="cat" class="com.eicoma.javabean.Cat"/>
    
    <bean id="people" class="com.eicoma.javabean.People" autowire="byType">
        <property name="name" value="晴明"/>
    </bean>
    
  3. 注解实现自动装配属性

    配置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:annotation-config/>
    
    </beans>
    
    <!--
    Spring注解装配
    -->
    <bean id="dog" class="com.eicoma.javabean.Dog"/>
    <bean id="cat" class="com.eicoma.javabean.Cat"/>
    <bean id="people" class="com.eicoma.javabean.People">
        <property name="name" value="晴明"/>
    </bean>
    
    public class People {
        private String name;
        @Autowired
        private Cat cat;
        @Autowired
        private Dog dog;
    
        public String getName() {
            return name;
        }
    
        public void setName(String name) {
            this.name = name;
        }
    
    	........................
    }
    

    @Autowired 注解注入属性

    1. 直接在属性上使用

    2. 也可写在set方法上

    @Qualifier

    如果@Autowired不能唯一自动装配属性,则可以用@Qualifier(value=“xxx”)配合@Autowired使用,指定一个唯一的bean对象注入
    在这里插入图片描述

@Resource

在这里插入图片描述

@Autowired和**@Resource**的区别

  • 都用来自动装配,放在属性字段上
  • @Autowired通过byType实现,在通过byName
  • @Resource通过byname实现,若找不到名字,则通过bytype实现
  • 两者执行顺序不同

自动装配

@Autowired

  • 将类自动装配,通过类型,名字

@Resource

  • 将类自动装配,通过名字,类型

@Qualifier

  • 如果@Autowired不能唯一自动装配属性,则可以用@Qualifier(value=“xxx”)配合@Autowired使用,指定一个唯一的bean对象注入

@Nullable

  • 字段使用该注解后,表示该字段可以为Null

常用依赖

<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-webmvc</artifactId>
    <version>5.3.8</version>
</dependency>
<dependency>
    <groupId>junit</groupId>
    <artifactId>junit</artifactId>
    <version>4.12</version>
</dependency>

5.使用注解开发

  1. @Component 注解创建对象

    • 放在类上,表示将该类注册到Spring容器中,装配Bean
  2. 属性如何注入

    @Value

    • 放在属性或set方法上,可以显示地给该属性赋值@Value(“XXX”)
    @Component
    @Scope("singleton")
    public class User {
    //    public String name = "晴明";
    
       //相当于<property name="name" value="晴明"/>
        @Value("晴明")
        public String name;
    }
    
  3. 衍生的注解

    注解创建对象

    配置

    <?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="com.eicoma"/>
    <!--    &lt;!&ndash;开启注解支持&ndash;&gt;-->
    <!--    &lt;!&ndash;在这里可以不要,因为已经被上边的扫描配置包含进去了&ndash;&gt;-->
    <!--    <context:annotation-config/>-->
        
    </beans>
    

    @Component的衍生注解,在web开发中按mvc三层架构分层

    • dao 【@Repository】

    • service 【@Service】

    • controller 【@Controller】

      这四个注解都表示将该类注册到Spring容器中,装配Bean

  4. 作用域

    • @Scope(“XXX”)

      • XXX可为singleton,prototype,request,session
      @Component
      //作用域
      @Scope("singleton")
      public class User {
      //    public String name = "晴明";
      
         //相当于<property name="name" value="晴明"/>
          @Value("晴明")
          public String name;
      
  5. 小结

    • xml适用于所有场合
    • 注解只有本类可用,维护相对复杂

6.通过javaConfig配置Spring

  • 完全通过JavaConfig来配置Spring
  1. pojo包
  • 创建一个实体类,并使用@Component注解,将该类注册到Spring容器中
//使用该注解,将User类注册到Spring容器,装配Bean
@Component
public class User {
    private String name;

    public String getName() {
        return name;
    }

    @Value("晴明")
    public void setName(String name) {
        this.name = name;
    }

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

  1. config包
  • 使用@Configuration将该配置类注册到Spring容器中
//使用这个注解后,该类也会被注册到Spring容器中,@Configuration也属于一个@Component
//使用@Configuration代表这是一个配置类,类似applicationContext.xml
@Configuration
public class EicomaConfig {

    //@Bean  该注解可注册一个Bean
    //该方法的名字,相当于xml中bean的id
    //该方法的返回值,相当于xml中bean的class
    @Bean
    public User getUser(){
        //返回要注入到Spring容器中的对象
        return new User();
    }
}

  • 使用@Bean将要注册的类注册到Spring容器中
//@Bean  该注解可注册一个Bean
    //该方法的名字,相当于xml中bean的id
    //该方法的返回值,相当于xml中bean的class
    @Bean
    public User getUser(){
        //返回要注入到Spring容器中的对象
        return new User();
    }



  • 使用@ComponentScan(“com.eicoma.pojo”)注册配置类,和@Component相似
//@ComponentScan("com.eicoma.pojo")的功能和Configuration一致
@ComponentScan("com.eicoma.pojo")
public class EicomaConfig {

    //@Bean  该注解可注册一个Bean
    //该方法的名字,相当于xml中bean的id
    //该方法的返回值,相当于xml中bean的class
    @Bean
    public User getUser(){
        //返回要注入到Spring容器中的对象
        return new User();
    }
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值