首页 > 其他分享 >spring::注解开发

spring::注解开发

时间:2022-11-27 13:35:28浏览次数:66  
标签:SpellChecker Autowired spring spellChecker 注释 bean 开发 注解 public

@Required

文档
@Required 注释应用于 bean 属性的 setter 方法,它表明受影响的 bean 属性在配置时必须放在 XML 配置文件中,否则容器就会抛出一个 BeanInitializationException 异常。下面显示的是一个使用 @Required 注释的示例。

个人感觉这个没什么用,就是要抛各个异常,其他不设置也行

Autowired

@Autowired 注释对在哪里和如何完成自动连接提供了更多的细微的控制。

@Autowired 注释可以在 setter 方法中被用于自动连接 bean,就像 @Autowired 注释,容器,一个属性或者任意命名的可能带有多个参数的方法。

你可以在 XML 文件中的 setter 方法中使用 @Autowired 注释来除去 元素。当 Spring遇到一个在 setter 方法中使用的 @Autowired 注释,它会在方法中视图执行 byType 自动连接。

例子

public class TextEditor {
   private SpellChecker spellChecker;
   @Autowired//这里准备自动装配
   public void setSpellChecker( SpellChecker spellChecker ){
      this.spellChecker = spellChecker;
   }
   public SpellChecker getSpellChecker( ) {
      return spellChecker;
   }
   public void spellCheck() {
      spellChecker.checkSpelling();
   }
}
public class SpellChecker {
   public SpellChecker(){
      System.out.println("Inside SpellChecker constructor." );
   }
   public void checkSpelling(){
      System.out.println("Inside checkSpelling." );
   }  
}
<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-3.0.xsd
    http://www.springframework.org/schema/context
    http://www.springframework.org/schema/context/spring-context-3.0.xsd">

   <context:annotation-config/>
    <!--这里并没有用propers装载依赖,这里是以为有@Autowired-->
   <!-- Definition for textEditor bean without constructor-arg  -->
   <bean id="textEditor" class="com.tutorialspoint.TextEditor">
   </bean>

   <!-- Definition for spellChecker bean -->
   <bean id="spellChecker" class="com.tutorialspoint.SpellChecker">
   </bean>

</beans>

qualifier

作用,对于有多个bean,选择某一个bean

public class demo{
  @Autowired
   @Qualifier("student1")
   private Student student;
}
<bean id="demo" class="....demo">
</bean>

<bean id="student1" class="....student">
</bean>

<bean id="student2" class="....student">
</bean>

标签:SpellChecker,Autowired,spring,spellChecker,注释,bean,开发,注解,public
From: https://www.cnblogs.com/tsqo/p/16929517.html

相关文章

  • 【Spring Cloud实战】Hystrix断路器
    gitee地址:https://gitee.com/javaxiaobear/spring-cloud_study.git在线阅读地址:https://javaxiaobear.gitee.io/1、概述分布式面临的问题复杂分布式体系结构中的应用程序有......
  • 基于Servlet+jsp+mysql开发javaWeb学生管理系统(学生信息、学生选课、学生成绩、学生
    你知道的越多,你不知道的越多点赞再看,养成习惯文章目录​​一、开发背景​​​​二、需求分析​​​​三、开发环境​​​​四、运行效果​​​​五、开发流程​​​​工......
  • 微服务SpringBoot 整合Redis 实现点赞、点赞排行榜
    文章目录​​⛅引言​​​​一、发布探店笔记​​​​二、查看探店笔记​​​​三、SpringBoot整合Redis实现点赞功能​​​​四、SpringBoot整合Redis实现点赞排行......
  • springboot集合efk搭建日志平台
    springboot继承efk实现日志收集1.安装es和kibana我使用的云服务器centos7,2核+4G内存,跑起来内存使用率50%左右建议使用最低配置和我一样,1+2的配置kibana应该跑不起来,......
  • SpringBoot源码-00-环境
    一源码附上带注释的源码分支my-study-3.0.1二编译环境版本Spring-Boot3.0.1OSmacOS11.5.2Java17.0.1Gradle7.4.2IDEA2022.2.1三文件......
  • Spring源码-00-环境
    一源码附上带注释的源码分支my-study-6.0.3二编译环境版本Spring6.0.3-SNAPSHOTOSmacOS11.5.2Java17.0.1Gradle7.4.2IDEA2022.2.1三......
  • spring boot 访问静态资源文件
    项目结构:springBoot通过classpath/static目录访问静态资源。注意存放静态资源的目录名称必须是static。将静态资源放在此目录下,通过浏览器直接可以访问访问路径:ht......
  • Web开发人员应当知道的15个开源项目
    如今,构建网站和开发Web应用程序已经不仅要求开发人员是一名优秀的程序员,更需要聪明的程序员。这也就是说,在可能的情况下,重复使用已有的代码和应用程序,而不是自己重头开始。......
  • WebService传输DataSet的一点想法和实践-.NET教程,Web Service开发
    其实这个标题很大,实现起来也可以有许多的办法。甚至,应否这样做也许都能惹出许多的争论(比如,为什么用ws而不是remoting?为什么传dataset而不是entity[]?)。      由于ds......
  • SpringBoot(三):全局配置文件以及yaml语法
    一、SpringBoot配置文件1.什么是SpringBoot配置文件  在SpringBoot项目中,资源文件夹下会有一个叫做application.properties的文件,这就是SpringBoot的配置文件。2.Sp......