首页 > 其他分享 >【Spring】- 自动注入注解

【Spring】- 自动注入注解

时间:2024-02-06 19:33:03浏览次数:22  
标签:movieCatalogs Autowired Spring private bean 注解 ElementType public 注入

【@Autowired】

冷知识:

@Autowired
private MovieCatalog[] movieCatalogs; // 根据类型注入全部的bean对象数组
@Autowired
private Set<MovieCatalog> movieCatalogs; //根据类型注入全部的bean对象集合
@Autowired
private Map<String, MovieCatalog> movieCatalogs; //根据类型注入全部的bean对象,key=bean对象的名称
@Autowired
private Optional<MovieFinder> movieCatalogs; // 注入非必须的

除以上:还支持泛型注入

@Configuration
public class MyConfiguration {

	@Bean
	public StringStore stringStore() {
		return new StringStore();
	}

	@Bean
	public IntegerStore integerStore() {
		return new IntegerStore();
	}
}


@Autowired
private Store<String> s1; // <String> qualifier, injects the stringStore bean

@Autowired
private Store<Integer> s2; // <Integer> qualifier, injects the integerStore bean

【@Primary】

作用:

  • 当容器按照类型自动装配时,可能存在多个满足条件的bean,当使用@Primary修饰bean时该bean在自动装配时的优先级最高


【@Qualifiers】

作用:

  • 当容器按照类型自动装配时,可能存在多个满足条件的bean,可以使用@Qualifiers指定需要注入的bean的名称
@Autowired
@Qualifier("main")
private MovieCatalog movieCatalog;

自定义

@Target({ElementType.FIELD, ElementType.PARAMETER})
@Retention(RetentionPolicy.RUNTIME)
@Qualifier
public @interface Genre {

	String value();
}
@Autowired
@Genre("Action")
private MovieCatalog actionCatalog;

【自定义并注册Qualifier】

@Target({ElementType.FIELD, ElementType.METHOD, ElementType.TYPE, ElementType.PARAMETER})
@Retention(RetentionPolicy.RUNTIME)
public @interface CustomerQualifier {
} 


 @Bean
 public CustomAutowireConfigurer customAutowireConfigurer () {
   CustomAutowireConfigurer configurer = new CustomAutowireConfigurer();
   configurer.setCustomQualifierTypes(Collections.singleton(CustomerQualifier.class));
   return configurer;
 }


【@Value】


标签:movieCatalogs,Autowired,Spring,private,bean,注解,ElementType,public,注入
From: https://blog.51cto.com/u_11209802/9630631

相关文章

  • spring boot controller设置返回json
    在SpringBoot中,Controller通常会返回JSON格式的数据,这得益于SpringBoot的自动配置能力以及内嵌的Jackson库。以下是如何设置Controller返回JSON数据的基本步骤:添加依赖:首先,确保你的项目中包含了SpringBoot的WebStarter依赖,它已经包括了Jackson库,用于处理JSON序列化。<dependen......
  • ThinkPHP 6.0 SQL注入漏洞修复
    公司买的官网被政府网安检测出SQL注入漏洞:隐患描述SQL漏洞证明语句: python3sqlmap.py-u"http://xxxx?keywords=1"-pkeywords--level=5--risk=3--tamper=space2comment.py,between.py--current-db--random-agent--time-sec=10--batch--dbms="MySQL"漏洞回显:权......
  • spring boot 引入 log.info("[消息服务]初始化成功"); log 爆红
    首先在idea中下载lombok插件下载完就好了一个小辣椒logo的lombok其次导入日志库的问题:确保项目的依赖中包含正确的日志库。在SpringBoot项目中,常用的是SLF4J,您可以在pom.xml(如果是Maven项目)或build.gradle(如果是Gradle项目)中添加以下依赖:xml<!--Maven--><dependencie......
  • Java微服务SpringCloud+Uniapp+Vue3+Element Plus开源BizSpring商城
    产品介绍BizSpring电商平台概述BizSpring电商平台,是基于最新SpringCloud微服务架构开发的多语言电商平台,使用领先的Vue3.0+ElementPlus+uniapp技术开发的移动全端业务、实现了多平台同步构建及建设的解决方案。应用发布基于Uni-app,实现跨多个平台(H5、公众号、头条、抖音......
  • SpringBoot集成Flink-CDC 采集PostgreSQL变更数据发布到Kafka
    (之前写了一个flink-cdc同步数据的博客,发布在某N,最近代码开源了,直接复制过来了,懒得重新写了,将就着看下吧)最近做的一个项目,使用的是pg数据库,公司没有成熟的DCD组件,为了实现数据变更消息发布的功能,我使用SpringBoot集成Flink-CDC采集PostgreSQL变更数据发布到Kafka。 一、业务......
  • SpringBoot使用Validation框架手动校验对象是否符合规则
      在springboot项目中引入<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-validation</artifactId></dependency> 伪代码importlombok.Data;import......
  • 前后端跨域竟然不需要加注解!
    众所周知前后端跨域一直是让人头疼的问题。那什么是跨域呢?出于浏览器的同源策略限制。同源策略(Sameoriginpolicy)是一种约定,它是浏览器最核心也最基本的安全功能,如果缺少了同源策略,则浏览器的正常功能可能都会受到影响。可以说Web是构建在同源策略基础之上的,浏览器只是针对同源策......
  • springboot集成easypoi导出多sheet页
    pom文件<dependency> <groupId>cn.afterturn</groupId> <artifactId>easypoi-base</artifactId> <version>4.1.0</version></dependency>导出模板:编辑后端代码示例:/***导出加油卡进便利店大额审批列表*@throwsIOException......
  • org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named '
    开发遇到一个问题:org.springframework.beans.factory.NoSuchBeanDefinitionException:Nobeannamed'ckhSynCardNumToMbhkJob'available这个报错可能是因为:1.spring的xml配置文件Bean中的id和getBean的id不一致2.是否是忘记加注解了,3.启动类包扫描路径是否正确经过测试发......
  • SpringBoot的maven插件生成可以直接启动的jar
    简单使用<build><plugins><plugin><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId><configuration&g......