首页 > 其他分享 >Autowired快捷键的使用

Autowired快捷键的使用

时间:2023-08-23 15:11:37浏览次数:43  
标签:String Autowired springframework 快捷键 使用 org public name

引用类型的注入可以使用@Autowired
@Autowired:spring框架提供的注解,实现引用类型的赋值。
spring中通过注解给引用类型赋值,使用的是自动注入原理,支持byName,byType
@Autowired:默认使用的是byType自动注入

位置:1)在属性定义的上面,无需set方法,推荐使用
2)在set方法的上面

@Autowired第一种使用方式---byType
1.在Student中先声明引用类型school,在声明school类的语句上面加入@Autowired
package com.example.ba03;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

@Component("myStudent")
public class Student {

    @Value(value = "张飞")
    private String name;
    @Value(value = "27")
    private Integer age;

    /*引用类型
    * @Autowired:spring框架提供的注解,实现引用类型的赋值。
    * spring中通过注解给引用类型赋值,使用的是自动注入原理,支持byName,byType
    * @Autowired:默认使用的是byType自动注入
    *
    * 位置:1)在属性定义的上面,无需set方法,推荐使用
    *       2)在set方法的上面
    * */
    @Autowired
    private School school;

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

    public void setAge(Integer age) {
        this.age = age;
    }


    @Override
    public String toString() {
        return "Student{" +
                "name='" + name + '\'' +
                ", age=" + age +
                ", school=" + school +
                '}';
    }
}

 

2.声明School对象,通过注解在School类上面加入@Component以及给属性的对象赋值;另外也可以通过配置文件来赋值

package com.example.ba03;

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

@Component("mySchool")
public class School {
    @Value(value = "北京大学")
    private String name;
    @Value(value = "北京海淀区")
    private String address;

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

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

    @Override
    public String toString() {
        return "School{" +
                "name='" + name + '\'' +
                ", address='" + address + '\'' +
                '}';
    }
}
<?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 http://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.example.ba03"/>
    <bean id="myXueXiao" class="com.example.ba03.School">
        <property name="name" value="清华大学"/>
        <property name="address" value="北京"/>
    </bean>
</beans>

 

 

@Autowired第二种使用方式---byName
 如果要使用byName方式,需要做的是:
1.在属性上面加入@Autowired
2.在属性上面加入@Qualifier(value="bean的id"):表示使用指定名称的bean完成赋值
package com.example.ba04;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

@Component("myStudent")
public class Student {

    @Value(value = "张飞")
    private String name;
    @Value(value = "27")
    private Integer age;

    /*引用类型
    * @Autowired:spring框架提供的注解,实现引用类型的赋值。
    * spring中通过注解给引用类型赋值,使用的是自动注入原理,支持byName,byType
    * @Autowired:默认使用的是byType自动注入
    *
    * 位置:1)在属性定义的上面,无需set方法,推荐使用
    *       2)在set方法的上面
    *
    * */
    @Autowired
    @Qualifier("mySchool")
    private School school;

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

    public void setAge(Integer age) {
        this.age = age;
    }


    @Override
    public String toString() {
        return "Student{" +
                "name='" + name + '\'' +
                ", age=" + age +
                ", school=" + school +
                '}';
    }
}

声明School对象,通过注解在School类上面加入@Component以及给属性的对象赋值

package com.example.ba04;

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

@Component("mySchool")
public class School {
    @Value(value = "中山大学")
    private String name;
    @Value(value = "广州")
    private String address;

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

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

    @Override
    public String toString() {
        return "School{" +
                "name='" + name + '\'' +
                ", address='" + address + '\'' +
                '}';
    }
}
补充:当@Qualifier内的名称与引用类型中声明的@Component内的名称不同时,执行代码会出现这样的错误提示:
复制代码
警告: Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.UnsatisfiedDependencyException: 
Error creating bean with name 'myStudent': Unsatisfied dependency expressed through field 'school';
nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException:
No qualifying bean of type 'com.example.ba05.School' available:
expected at least 1 bean which qualifies as autowire candidate. Dependency annotations:
{@org.springframework.beans.factory.annotation.Autowired(required=true), @org.springframework.beans.factory.annotation.Qualifier(value=mySchool-01)}
复制代码
这里Autowired(required=true)是Autowired的属性
属性:required,是boolean类型的,默认true
   required=true:表示引用类型赋值失败,程序报错,并终止执行

   required=fasle:表示引用类型赋值失败,程序正常执行,引用类型是null

 

 

 

标签:String,Autowired,springframework,快捷键,使用,org,public,name
From: https://www.cnblogs.com/ttyypjt/p/17651684.html

相关文章

  • 【腾讯云 TDSQL-C Serverless 产品体验】 使用 Python 向 TDSQL-C 添加读取数据 实现
    前言TDSQL-CMySQL版(TDSQL-CforMySQL)是腾讯云自研的新一代云原生关系型数据库。融合了传统数据库、云计算与新硬件技术的优势,为用户提供具备高弹性、高性能、海量存储、安全可靠的数据库服务。TDSQL-CMySQL版100%兼容MySQL5.7、8.0。实现超百万级QPS的高吞吐,最高PB级智......
  • 任务调度工具_Spring Task在SpringBoot中使用教程
    ##SpringTask1.1介绍SpringTask是Spring框架提供的任务调度工具,可以按照约定的时间自动执行某个代码逻辑。定位:定时任务框架作用:定时自动执行某段Java代码为什么要在Java程序中使用SpringTask?应用场景:1).信用卡每月还款提醒2).银行贷款每月还款提醒3).火车......
  • linux下查看使用了哪些端口号,以及该端口运行的内容
    linux查看哪些端口被使用,以及对应的进程id使用netstat命令如netstat-nltp 同时可以使用lsof-i:端口号查看端口运行的详细信息 ......
  • vue3 使用 router 进行跳转备忘
    1.在画面中添加子画面,使用el-menu菜单进行跳转,只更新子画面a.首先在配置router路径的js文件中配置画面的路径,子画面的路径要在父画面的children下面 在父节点下设置redirect属性,打开父画面时会默认打开相应子画面,否则子画面默认显示为空白 b.在画面显示区域添加......
  • 快捷键
    在下方插入一行ctrlaltenter在上方插入一行alt+↑ 向上移动一行shift+alt+↓ 向下复制一行选中一个词:ctrl +dHome光标跳转到行头End光标跳转到行尾Ctrl+Home跳转到页头Ctrl+End跳转到页尾......
  • [AHK2-UI] 使用#Include
    #Include是什么一句话介绍:可以将一个脚本的代码插入到Include语句的位置。作用使用#Include可以实现分模块开发,对于代码组织有十分重要的作用。通常使用小型脚本(只有些热键和热字串)不需要使用;但当脚本不仅仅是这些,还要写ui界面或更繁杂的功能时,我们最好将ui和数据处理的逻辑分......
  • 如何优雅的使用telnet测试端口连通性
    telnet命令是TELNET协议的用户接口,它支持两种模式:命令模式和会话模式,虽然telnet支持许多命令,但大部分情况下,我们只是使用它查看目标主机是否打开了某端口(默认是23)。其执行结果有两种:端口未打开$telnet101.199.97.6562715Trying101.199.97.65...telnet:connecttoaddres......
  • java多线程使用详解与案例,超详细
    一、创建线程的方式1、继承Thread类让子类继承Thread线程类子类必须重写Thread类的run方法创建一个自己定义的线程对象调用start()方法启动线程//测试类/***1、让子类继承Thread线程类*/publicclassThreadTest1extendsThread{//2、子类必须重写Thread类......
  • Windows上使用主机名访问统信UOS上共享文件夹
    原文链接:Windows上使用主机名访问统信UOS上共享文件夹hello,大家好啊,今天给大家介绍一个在Windows上使用主机名访问统信UOS上共享文件夹的方法,我们在windows上除了可以使用pingIP地址的方式确定与其他主机是否通信正常,也可以直接ping其他主机的主机名从而来确定是否与该主机通信正......
  • 在工程中如何使用一个公用的页面
    在我们工作的过程中,会遇到这样的问题,比如一个界面被频繁的使用,比如登录界面等;那么这样个问题可以这样解决:先在入口类里面perproty所需要使用的界面,然后调用进入首页的方法,然后创建导航,把导航的跟视图设为所用使用的界面,再在原来window的跟视图设为这个导航,然后就可以用这几句代码随......