@Resources @Inject @Autowied @Qualifier @named

本文详细介绍了Java中依赖注入的相关概念及使用方式,包括@Autowired、@Inject、@Resource等注解的功能区别及其应用场景,同时探讨了如何解决依赖注入过程中的常见问题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Annotations

Annotation    Package                                            Source
@Resource javax.annotation                                 Java
@Inject         javax.inject                                            Java
@Qualifier javax.inject                                                Java
@Autowired org.springframework.bean.factory    Spring

@Autowied

是通过ByType注入的,可以在field.contractor注入,如果是有两个以及以上相同的类型或者没有就会报错,但是@Qualifier配合使用可以制定bean的名称也就是把自动装配类型从By-type 变成By-name 当然 @Autowied有一个属性为Required 当值为false时
 

@Autowired(required=false)
 private HelloChina helloChina;

当不能确定 Spring 容器中一定拥有某个类的 Bean 时,可以在需要自动注入该类 Bean 的地方可以使用  @Autowired (required =  false ),这等于告诉 Spring:在找不到匹配 Bean 时也不报错
----------------------------------------------------------

@Inject Annotation based on JSR-330

该注解几乎可以完全替代Spring的@Autowired注解。所以除了使用Spring特定的@Autowired注解,我们可以选择@Inject。

和@Autowired一个,@Inject可以用来自动装配属性、方法和构造器;与@Autowired不同的是,@Inject没有required属性。因此@Inject注解注入的依赖关系必须存在,否则报异常。

与@Autowired一样,@Inject有自己限定的方法,即处理限定歧义性的依赖,配置如下:

@Inject
@Named("xxName")
private HelloChina helloChina;


如上所示,@Qualifier注解尝试注入ID为xxName的Bean。
原理,也即是将byType自动装配转换为byName自动装配。
-------------------------

@Resource

 默认按照名称装配,当找不到与名称匹配的bean才会按照类型装配,可以通过name属性指定,如果没有指定name属性,当注解标注在字段上,即默认取字段的名称作为bean名称寻找依赖对象,当注解标注在属性的setter方法上,即默认取属性名作为bean名称寻找依赖对象.
注意:如果没有指定name属性,并且按照默认的名称仍然找不到依赖的对象时候,会回退到按照类型装配,但一旦指定了name属性,就只能按照名称装配了

参考博客:

http://blog.sina.com.cn/s/blog_a795a96f01016if1.html


sbt:spark-app> assembly [warn] multiple main classes detected: run 'show discoveredMainClasses' to see the list [error] 37 error(s) were encountered during the merge: [error] stack trace is suppressed; run last assembly for the full output [error] (assembly) [error] Deduplicate found different file contents in the following: [error] Jar name = protobuf-java-3.19.6.jar, jar org = com.google.protobuf, entry target = google/protobuf/api.proto [error] Jar name = spark-core_2.12-3.5.0.jar, jar org = org.apache.spark, entry target = google/protobuf/api.proto [error] Deduplicate found different file contents in the following: [error] Jar name = protobuf-java-3.19.6.jar, jar org = com.google.protobuf, entry target = google/protobuf/field_mask.proto [error] Jar name = spark-core_2.12-3.5.0.jar, jar org = org.apache.spark, entry target = google/protobuf/field_mask.proto [error] Deduplicate found different file contents in the following: [error] Jar name = commons-logging-1.1.3.jar, jar org = commons-logging, entry target = org/apache/commons/logging/Log.class [error] Jar name = jcl-over-slf4j-2.0.7.jar, jar org = org.slf4j, entry target = org/apache/commons/logging/Log.class [error] Deduplicate found different file contents in the following: [error] Jar name = protobuf-java-3.19.6.jar, jar org = com.google.protobuf, entry target = google/protobuf/struct.proto [error] Jar name = spark-core_2.12-3.5.0.jar, jar org = org.apache.spark, entry target = google/protobuf/struct.proto [error] Deduplicate found different file contents in the following: [error] Jar name = commons-logging-1.1.3.jar, jar org = commons-logging, entry target = org/apache/commons/logging/impl/NoOpLog.class [error] Jar name = jcl-over-slf4j-2.0.7.jar, jar org = org.slf4j, entry target = org/apache/commons/logging/impl/NoOpLog.class [error] Deduplicate found different file contents in the following: [error] Jar name = arrow-format-11.0.0.jar, jar org = org.apache.arrow, entry target = arrow-git.properties [error] Jar name = arrow-memory-core-11.0.0.jar, jar org = org.apache.arrow, entry target = arrow-git.properties [error] Jar name = arrow-memory-netty-11.0.0.jar, jar org = org.apache.arrow, entry target = arrow-git.properties [error] Jar name = arrow-vector-11.0.0.jar, jar org = org.apache.arrow, entry target = arrow-git.properties [error] Deduplicate found different file contents in the following: [error] Jar name = javax.inject-1.jar, jar org = javax.inject, entry target = javax/inject/Singleton.class [error] Jar name = jakarta.inject-2.6.1.jar, jar org = org.glassfish.hk2.external, entry target = javax/inject/Singleton.class [error] Deduplicate found different file contents in the following: [error] Jar name = protobuf-java-3.19.6.jar, jar org = com.google.protobuf, entry target = google/protobuf/duration.proto [error] Jar name = spark-core_2.12-3.5.0.jar, jar org = org.apache.spark, entry target = google/protobuf/duration.proto [error] Deduplicate found different file contents in the following: [error] Jar name = javax.inject-1.jar, jar org = javax.inject, entry target = javax/inject/Named.class [error] Jar name = jakarta.inject-2.6.1.jar, jar org = org.glassfish.hk2.external, entry target = javax/inject/Named.class [error] Deduplicate found different file contents in the following: [error] Jar name = javax.inject-1.jar, jar org = javax.inject, entry target = javax/inject/Inject.class [error] Jar name = jakarta.inject-2.6.1.jar, jar org = org.glassfish.hk2.external, entry target = javax/inject/Inject.class [error] Deduplicate found different file contents in the following: [error] Jar name = commons-logging-1.1.3.jar, jar org = commons-logging, entry target = org/apache/commons/logging/LogFactory.class [error] Jar name = jcl-over-slf4j-2.0.7.jar, jar org = org.slf4j, entry target = org/apache/commons/logging/LogFactory.class [error] Deduplicate found different file contents in the following: [error] Jar name = commons-logging-1.1.3.jar, jar org = commons-logging, entry target = org/apache/commons/logging/LogConfigurationException.class [error] Jar name = jcl-over-slf4j-2.0.7.jar, jar org = org.slf4j, entry target = org/apache/commons/logging/LogConfigurationException.class [error] Deduplicate found different file contents in the following: [error] Jar name = jakarta.annotation-api-1.3.5.jar, jar org = jakarta.annotation, entry target = javax/annotation/security/DenyAll.class [error] Jar name = jsr250-api-1.0.jar, jar org = javax.annotation, entry target = javax/annotation/security/DenyAll.class [error] Deduplicate found different file contents in the following: [error] Jar name = protobuf-java-3.19.6.jar, jar org = com.google.protobuf, entry target = google/protobuf/timestamp.proto [error] Jar name = spark-core_2.12-3.5.0.jar, jar org = org.apache.spark, entry target = google/protobuf/timestamp.proto [error] Deduplicate found different file contents in the following: [error] Jar name = jakarta.annotation-api-1.3.5.jar, jar org = jakarta.annotation, entry target = javax/annotation/Generated.class [error] Jar name = jsr250-api-1.0.jar, jar org = javax.annotation, entry target = javax/annotation/Generated.class [error] Deduplicate found different file contents in the following: [error] Jar name = protobuf-java-3.19.6.jar, jar org = com.google.protobuf, entry target = google/protobuf/source_context.proto [error] Jar name = spark-core_2.12-3.5.0.jar, jar org = org.apache.spark, entry target = google/protobuf/source_context.proto [error] Deduplicate found different file contents in the following: [error] Jar name = protobuf-java-3.19.6.jar, jar org = com.google.protobuf, entry target = google/protobuf/empty.proto [error] Jar name = spark-core_2.12-3.5.0.jar, jar org = org.apache.spark, entry target = google/protobuf/empty.proto [error] Deduplicate found different file contents in the following: [error] Jar name = jakarta.annotation-api-1.3.5.jar, jar org = jakarta.annotation, entry target = javax/annotation/Resource.class [error] Jar name = jsr250-api-1.0.jar, jar org = javax.annotation, entry target = javax/annotation/Resource.class [error] Deduplicate found different file contents in the following: [error] Jar name = jakarta.annotation-api-1.3.5.jar, jar org = jakarta.annotation, entry target = javax/annotation/security/RolesAllowed.class [error] Jar name = jsr250-api-1.0.jar, jar org = javax.annotation, entry target = javax/annotation/security/RolesAllowed.class [error] Deduplicate found different file contents in the following: [error] Jar name = jakarta.annotation-api-1.3.5.jar, jar org = jakarta.annotation, entry target = javax/annotation/PreDestroy.class [error] Jar name = jsr250-api-1.0.jar, jar org = javax.annotation, entry target = javax/annotation/PreDestroy.class [error] Deduplicate found different file contents in the following: [error] Jar name = protobuf-java-3.19.6.jar, jar org = com.google.protobuf, entry target = google/protobuf/descriptor.proto [error] Jar name = spark-core_2.12-3.5.0.jar, jar org = org.apache.spark, entry target = google/protobuf/descriptor.proto [error] Deduplicate found different file contents in the following: [error] Jar name = commons-logging-1.1.3.jar, jar org = commons-logging, entry target = org/apache/commons/logging/impl/SimpleLog.class [error] Jar name = jcl-over-slf4j-2.0.7.jar, jar org = org.slf4j, entry target = org/apache/commons/logging/impl/SimpleLog.class [error] Deduplicate found different file contents in the following: [error] Jar name = jakarta.annotation-api-1.3.5.jar, jar org = jakarta.annotation, entry target = javax/annotation/security/DeclareRoles.class [error] Jar name = jsr250-api-1.0.jar, jar org = javax.annotation, entry target = javax/annotation/security/DeclareRoles.class [error] Deduplicate found different file contents in the following: [error] Jar name = protobuf-java-3.19.6.jar, jar org = com.google.protobuf, entry target = google/protobuf/wrappers.proto [error] Jar name = spark-core_2.12-3.5.0.jar, jar org = org.apache.spark, entry target = google/protobuf/wrappers.proto [error] Deduplicate found different file contents in the following: [error] Jar name = javax.inject-1.jar, jar org = javax.inject, entry target = javax/inject/Qualifier.class [error] Jar name = jakarta.inject-2.6.1.jar, jar org = org.glassfish.hk2.external, entry target = javax/inject/Qualifier.class [error] Deduplicate found different file contents in the following: [error] Jar name = jakarta.annotation-api-1.3.5.jar, jar org = jakarta.annotation, entry target = javax/annotation/security/PermitAll.class [error] Jar name = jsr250-api-1.0.jar, jar org = javax.annotation, entry target = javax/annotation/security/PermitAll.class [error] Deduplicate found different file contents in the following: [error] Jar name = protobuf-java-3.19.6.jar, jar org = com.google.protobuf, entry target = google/protobuf/any.proto [error] Jar name = spark-core_2.12-3.5.0.jar, jar org = org.apache.spark, entry target = google/protobuf/any.proto [error] Deduplicate found different file contents in the following: [error] Jar name = jakarta.annotation-api-1.3.5.jar, jar org = jakarta.annotation, entry target = javax/annotation/Resources.class [error] Jar name = jsr250-api-1.0.jar, jar org = javax.annotation, entry target = javax/annotation/Resources.class [error] Deduplicate found different file contents in the following: [error] Jar name = jakarta.annotation-api-1.3.5.jar, jar org = jakarta.annotation, entry target = javax/annotation/Resource$AuthenticationType.class [error] Jar name = jsr250-api-1.0.jar, jar org = javax.annotation, entry target = javax/annotation/Resource$AuthenticationType.class [error] Deduplicate found different file contents in the following: [error] Jar name = jackson-core-2.15.2.jar, jar org = com.fasterxml.jackson.core, entry target = META-INF/versions/9/module-info.class [error] Jar name = jackson-databind-2.15.2.jar, jar org = com.fasterxml.jackson.core, entry target = META-INF/versions/9/module-info.class [error] Jar name = jackson-datatype-jsr310-2.13.4.jar, jar org = com.fasterxml.jackson.datatype, entry target = META-INF/versions/9/module-info.class [error] Jar name = gson-2.10.1.jar, jar org = com.google.code.gson, entry target = META-INF/versions/9/module-info.class [error] Jar name = log4j-api-2.20.0.jar, jar org = org.apache.logging.log4j, entry target = META-INF/versions/9/module-info.class [error] Jar name = parquet-jackson-1.12.3.jar, jar org = org.apache.parquet, entry target = META-INF/versions/9/module-info.class [error] Jar name = jcl-over-slf4j-2.0.7.jar, jar org = org.slf4j, entry target = META-INF/versions/9/module-info.class [error] Jar name = slf4j-api-2.0.7.jar, jar org = org.slf4j, entry target = META-INF/versions/9/module-info.class [error] Jar name = xz-1.9.jar, jar org = org.tukaani, entry target = META-INF/versions/9/module-info.class [error] Deduplicate found different file contents in the following: [error] Jar name = protobuf-java-3.19.6.jar, jar org = com.google.protobuf, entry target = google/protobuf/type.proto [error] Jar name = spark-core_2.12-3.5.0.jar, jar org = org.apache.spark, entry target = google/protobuf/type.proto [error] Deduplicate found different file contents in the following: [error] Jar name = jakarta.annotation-api-1.3.5.jar, jar org = jakarta.annotation, entry target = javax/annotation/PostConstruct.class [error] Jar name = jsr250-api-1.0.jar, jar org = javax.annotation, entry target = javax/annotation/PostConstruct.class [error] Deduplicate found different file contents in the following: [error] Jar name = netty-all-4.1.96.Final.jar, jar org = io.netty, entry target = META-INF/io.netty.versions.properties [error] Jar name = netty-buffer-4.1.96.Final.jar, jar org = io.netty, entry target = META-INF/io.netty.versions.properties [error] Jar name = netty-codec-http2-4.1.96.Final.jar, jar org = io.netty, entry target = META-INF/io.netty.versions.properties [error] Jar name = netty-codec-http-4.1.96.Final.jar, jar org = io.netty, entry target = META-INF/io.netty.versions.properties [error] Jar name = netty-codec-socks-4.1.96.Final.jar, jar org = io.netty, entry target = META-INF/io.netty.versions.properties [error] Jar name = netty-codec-4.1.96.Final.jar, jar org = io.netty, entry target = META-INF/io.netty.versions.properties [error] Jar name = netty-common-4.1.96.Final.jar, jar org = io.netty, entry target = META-INF/io.netty.versions.properties [error] Jar name = netty-handler-proxy-4.1.96.Final.jar, jar org = io.netty, entry target = META-INF/io.netty.versions.properties [error] Jar name = netty-handler-4.1.96.Final.jar, jar org = io.netty, entry target = META-INF/io.netty.versions.properties [error] Jar name = netty-resolver-4.1.96.Final.jar, jar org = io.netty, entry target = META-INF/io.netty.versions.properties [error] Jar name = netty-transport-classes-epoll-4.1.96.Final.jar, jar org = io.netty, entry target = META-INF/io.netty.versions.properties [error] Jar name = netty-transport-classes-kqueue-4.1.96.Final.jar, jar org = io.netty, entry target = META-INF/io.netty.versions.properties [error] Jar name = netty-transport-native-epoll-4.1.96.Final.jar, jar org = io.netty, entry target = META-INF/io.netty.versions.properties [error] Jar name = netty-transport-native-epoll-4.1.96.Final.jar, jar org = io.netty, entry target = META-INF/io.netty.versions.properties [error] Jar name = netty-transport-native-epoll-4.1.96.Final.jar, jar org = io.netty, entry target = META-INF/io.netty.versions.properties [error] Jar name = netty-transport-native-kqueue-4.1.96.Final.jar, jar org = io.netty, entry target = META-INF/io.netty.versions.properties [error] Jar name = netty-transport-native-kqueue-4.1.96.Final.jar, jar org = io.netty, entry target = META-INF/io.netty.versions.properties [error] Jar name = netty-transport-native-unix-common-4.1.96.Final.jar, jar org = io.netty, entry target = META-INF/io.netty.versions.properties [error] Jar name = netty-transport-4.1.96.Final.jar, jar org = io.netty, entry target = META-INF/io.netty.versions.properties [error] Deduplicate found different file contents in the following: [error] Jar name = javax.inject-1.jar, jar org = javax.inject, entry target = javax/inject/Scope.class [error] Jar name = jakarta.inject-2.6.1.jar, jar org = org.glassfish.hk2.external, entry target = javax/inject/Scope.class [error] Deduplicate found different file contents in the following: [error] Jar name = log4j-1.2-api-2.20.0.jar, jar org = org.apache.logging.log4j, entry target = META-INF/org/apache/logging/log4j/core/config/plugins/Log4j2Plugins.dat [error] Jar name = log4j-core-2.20.0.jar, jar org = org.apache.logging.log4j, entry target = META-INF/org/apache/logging/log4j/core/config/plugins/Log4j2Plugins.dat [error] Deduplicate found different file contents in the following: [error] Jar name = javax.inject-1.jar, jar org = javax.inject, entry target = javax/inject/Provider.class [error] Jar name = jakarta.inject-2.6.1.jar, jar org = org.glassfish.hk2.external, entry target = javax/inject/Provider.class [error] Deduplicate found different file contents in the following: [error] Jar name = jakarta.annotation-api-1.3.5.jar, jar org = jakarta.annotation, entry target = javax/annotation/security/RunAs.class [error] Jar name = jsr250-api-1.0.jar, jar org = javax.annotation, entry target = javax/annotation/security/RunAs.class [error] Total time: 5 s, completed 2025年7月12日 下午12:56:20 [error] server failed to start on local:sbt-server-8814d8f5c3747ce114fe. java.io.IOException: Could not create lock for \\.\pipe\sbt-server-8814d8f5c3747ce114fe_lock, error 5 sbt:spark-app>
最新发布
07-13
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值