当前位置: 首页 > news >正文

Spring基础05

Spring基础05

Bean的作用域

  1. bean的作用域官网图示:

    在这里插入图片描述

  2. Bean的作用域分类:

    • 单例模式:Spring的默认机制,每次从容器中取出的都是同一个对象。

      <!-- 默认为单例模式 -->
      <bean id="address" class="Address"  scope="singleton">
      </bean>
      

      在这里插入图片描述

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

      <!-- 原型模式 -->
      <bean id="address" class="Address"  scope="prototype">
      </bean>
      

      在这里插入图片描述

    • 其余request,session,application这些只能在web开发中使用到!

  3. Bean的自动装配:

    • 自动装配是Spring满足bean依赖的一种方式!

    • Spring会在上下文中自动寻找,并自动给bean装配属性!

    • 在Spring中自动装配的方式:

      • 在xml中显示的配置。
      • 在java中显示配置。
      • 隐式的自动装配bean。
    • 使用注解实现自动装配:

      • Spring2.5支持注解。
  4. xml配置代码示例:

    • 编写实体类:
    public class Cat {
        public void shout(){
            System.out.println("miao~");
        }
    }
    
    public class Dog {
        public void shout(){
            System.out.println("wang~");
        }
    }
    
    public class People {
        
        private Cat cat;
        private Dog dog;
        private String name;
    
        public Cat getCat() {
            return cat;
        }
    
        public void setCat(Cat cat) {
            this.cat = cat;
        }
    
        public Dog getDog() {
            return dog;
        }
    
        public void setDog(Dog dog) {
            this.dog = dog;
        }
    
        public String getName() {
            return name;
        }
    
        public void setName(String name) {
            this.name = name;
        }
    
        @Override
        public String toString() {
            return "People{" +
                    "cat=" + cat +
                    ", dog=" + dog +
                    ", name='" + name + '\'' +
                    '}';
        }
    }
    
    • 编写spring配置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="cat" class="Cat"></bean>
    
        <bean id="dog" class="Dog"></bean>
    
         <bean id="people" class="People">
            <property name="name" value="张三"></property>
            <property name="cat" ref="cat"></property>
            <property name="dog" ref="dog"></property>
        </bean> 
        
    </beans>
    
     <!-- byName: 会自动在容器上下文中查找,和自己对象set方法后面值对应的beanid-->
        <bean id="people" class="People" autowire="byName">
            <property name="name" value="张三"></property>
        </bean> >
    
        <!-- byType: 会自动在容器上下文中查找,和自己对象属性类型相同的beanid-->
        <bean id="people" class="People" autowire="byType">
            <property name="name" value="张三"></property>
        </bean>
    
    • 编写测试类:
    import org.springframework.context.support.ClassPathXmlApplicationContext;
    
    public class Test {
    
        public static void main(String[] args) {
            ClassPathXmlApplicationContext classPathXmlApplicationContext = new ClassPathXmlApplicationContext("beans.xml");
            People people = (People) classPathXmlApplicationContext.getBean("people");
            people.getDog().shout();
            people.getCat().shout();
        }
    }
    
    • 注意点:

      • byName的时候,需要保证所有的bean的id唯一,并且这个bean需要和自动注入的属性的set方法值一致。
      • byType的时候,需要保证所有的bean的class唯一,并且这个bean需要和自动注入的属性的类型一致。
  5. 自动装配xml代码示例:

    • 使用注解须知:

      1. 导入约束: context约束。

        <beans        xmlns:context="http://www.springframework.org/schema/context"
               xsi:schemaLocation="pring-beans.xsd
                http://www.springframework.org/schema/context
        		https://www.springframework.org/schema/context/spring-context.xsd">
        </beans>
        
      2. 配置注解的支持: context:annotation-config/ 。

        <?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="cat" class="Cat"></bean>
        
            <bean id="dog" class="Dog"></bean>
        
            <bean id="people" class="People"></bean>
        </beans>
        
        public class People {
            
            @Autowired
            private Cat cat;
            @Autowired // 可以在set方法上使用
            private Dog dog;
            private String name;
        
            public Cat getCat() {
                return cat;
            }
        
            public Dog getDog() {
                return dog;
            }
        
            public String getName() {
                return name;
            }
        
            public void setName(String name) {
                this.name = name;
            }
        }
        
      3. 如果自动装配场景比较复杂,无法只使用@Autowired注解完成时,我们可以使用@Qualifier(value=“xxx”)去配合@Autowired使用,指定一个唯一的bean对象注入!

        import org.springframework.beans.factory.annotation.Autowired;
        import org.springframework.beans.factory.annotation.Qualifier;
        
        public class People {
        
            @Autowired
            private Cat cat;
            @Autowired
            @Qualifier(value = "dog1")
            private Dog dog;
            private String name;
        
        }
        
      4. @Resource注解

        import javax.annotation.Resource;
        
        public class People {
        
            private Cat cat;
            @Resource(name = "dog1")
            private Dog dog;
            private String name;
        }
        
      5. @Resource和@Autowired的区别:

        • 都是可以用来自动装配,都可以放在属性字段上。

        • @Autowired通过byType的方式实现,必须存在这个类型的对象,如果一种类型对应多个对象,则搭配@Qualifier(value=“xxx”)使用。

        • @Resource默认通过byName的方式实现,如果找不到名字,则通过byType实现,如果两种都找不到就会报错,使用@Resource(name = “xxx”)解决报错。

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.dtcms.com/a/45525.html

相关文章:

  • git 鼓励频繁提交commit early, commit often,用好分支,多用分支
  • 【SpringBoot+Vue】博客项目开发二:用户登录注册模块
  • 乡村研学旅行小程序(论文源码调试讲解)
  • 行为型模式 - 观察者模式 (Publish/Subscribe)
  • 【华三】从零开始掌握SR技术:原理、架构与应用全解析
  • golang介绍,特点,项目结构,基本变量类型与声明介绍(数组,切片,映射),控制流语句介绍(条件,循环,switch case)
  • 开发一个o2o(线上到线下)商城需要具备以下条件
  • 快速入手-搭建Flask框架封装mysql并结合业务实际情况使用
  • 【MySQL】MySQL用户管理
  • Flask笔记
  • 在 Element Plus 的 <el-select> 组件中,如果需要将 <el-option> 的默认值设置为 null。 用于枚举传值
  • JavaScript 注释
  • LeetCode 解题思路 6(Hot 100)
  • Nginx 配置与常用命令速查手册
  • 神经网络|(十一)|神经元和神经网络
  • Hive-01之数仓、架构、数据类型、DDL、内外部表
  • 纯c#字体处理库(FontParser) -- 轻量、极速、跨平台、具有字体子集化功能
  • 开源程序wordpress在海外品牌推广中的重要作用
  • Qt空项目代码解释
  • Redis SCAN 命令详解:安全遍历海量键的利器
  • SOA(面向服务架构)全面解析
  • 【wiki知识库】07.用户管理后端SpringBoot部分
  • 左值引用与右值引用
  • NO.22十六届蓝桥杯备战|一维数组|七道练习|冒泡排序(C++)
  • 对seacmsv9进行sql注入,orderby,过滤information_schema
  • 构建神经网络之常用pandas(补充中 )
  • leetcode459 重复的子字符串 周期性字符串问题 KMP算法
  • 解析AI工具库中三款 AI 图片转页面工具
  • Vidma Ver.2.14.0 高级版
  • OpenSSL 基础使用流程