@autowired和@Resource

简介: 前言关于注解和xml配置的官方回答Are annotations better than XML for configuring Spring?The introduction of annotation-based configurations raised the question of whether this approach is 'better'

前言

关于注解和xml配置的官方回答

Are annotations better than XML for configuring Spring?

The introduction of annotation-based configurations raised the question of whether 
this approach is 'better' than XML. The short answer is it depends. The long 
answer is that each approach has its pros and cons, and usually it is up to the 
developer to decide which strategy suits them better. Due to the way they are 
defined, annotations provide a lot of context in their declaration, 
leading to shorter and more concise configuration. However, XML excels at 
wiring up components without touching their source code or recompiling them. 
Some developers prefer having the wiring close to the source while others 
argue that annotated classes are no longer POJOs and, furthermore, 
that the configuration becomes decentralized and harder to control.

No matter the choice, Spring can accommodate both styles and even mix them 
together. It’s worth pointing out that through its JavaConfig option, Spring 
allows annotations to be used in a non-invasive way, without touching the 
target components source code and that in terms of tooling, all configuration 
styles are supported by the Spring Tool Suite.

两种注解的说明

@autowired

简介

autowired是spring官方提供的一种装配方式。

使用方法

1.使用在方法上:

public class SimpleMovieLister {

    private MovieFinder movieFinder;

    @Autowired
    public void setMovieFinder(MovieFinder movieFinder) {
        this.movieFinder = movieFinder;
    }

    // ...

}

2.使用在字段上

  @Autowired
    private MovieCatalog movieCatalog;

说明

autowired主要是用bytype的方式注入。

@Resource

简介

@resource是java自带的一种装配方式。

使用方法

与@autowired相似
1.指定name属性

 @Resource(name="myMovieFinder")
    public void setMovieFinder(MovieFinder movieFinder) {
        this.movieFinder = movieFinder;
    }

2.默认[使用默认提供名称]

 @Resource
    public void setMovieFinder(MovieFinder movieFinder) {
        this.movieFinder = movieFinder;
    }

说明

@Resource默认按名称进行装配,名称可以通过name属性进行指定,如果没有指定name属性,当注解写在字段上时,默认取字段名进行名称查找。

总结

两种方式的功能大致相同,默认推荐使用@Resource方式,这是j2ee生的方式。能降低和spring的耦合度。

更多具体详情请参见官方:http://spring.io

目录
相关文章
|
存储 分布式计算 大数据
HBase分布式数据库关键技术与实战:面试经验与必备知识点解析
【4月更文挑战第9天】本文深入剖析了HBase的核心技术,包括数据模型、分布式架构、访问模式和一致性保证,并探讨了其实战应用,如大规模数据存储、实时数据分析及与Hadoop、Spark集成。同时,分享了面试经验,对比了HBase与其他数据库的差异,提出了应对挑战的解决方案,展望了HBase的未来趋势。通过Java API代码示例,帮助读者巩固理解。全面了解和掌握HBase,能为面试和实际工作中的大数据处理提供坚实基础。
720 3
|
3月前
|
运维 安全 测试技术
Hydra-SSH 漏洞安全防范
Hydra 是由 THC 组织开发的强力网络安全测试工具,主要用于对 SSH、FTP、HTTP 等协议进行认证爆破,适用于授权渗透测试与弱口令检测。其高效性依赖于优化的字典策略,强调质量优先,结合目标信息定制密码列表,提高破解成功率。
448 1
|
10月前
|
算法 测试技术 Swift
Kimi开源Moonlight-16B-A3B:基于Muon优化器的高效大模型,性能与训练效率双突破!
Kimi开源Moonlight-16B-A3B:基于Muon优化器的高效大模型,性能与训练效率双突破!
362 5
|
存储 算法 网络安全
二进制加密PHP Webshell原理及简单实现
二进制加密PHP Webshell原理及简单实现
389 8
|
安全 程序员 数据安全/隐私保护
创业之路的故事|如何设计一个用户系统
本文作者将用户系统的设计类比为一次创业项目。深入浅出地介绍了用户系统的设计方式。
|
前端开发 JavaScript 架构师
了解微前端,深入前端架构的前世今生
该文章深入探讨了微前端架构的起源、发展及其解决的问题,并详细讲解了微前端在现代Web应用中的实现方式与优势,帮助读者理解微前端的设计理念和技术细节。
|
弹性计算 Kubernetes Serverless
Serverless Kubernetes - 理想,现实与未来
Serverless(无服务器)容器是让用户无需购买和管理服务器直接部署容器应用的产品、技术形态。Serverless容器可以极大提高容器应用部署的敏捷度和弹性能力,降低用户计算成本;让用户聚焦业务应用而非底层基础设施管理,极大地提高应用开发效率,降低运维成本。 本文将分享我们对Serverless Kubernetes领域的深度思考,从架构设计和底层基础设施多个维度深度剖析Serverless Kubernetes。
14688 0
Serverless Kubernetes - 理想,现实与未来
crash工具学习 —— percpu相关的一些用法
crash工具学习 —— percpu相关的一些用法
|
机器学习/深度学习 自然语言处理 算法
从滑动窗口到YOLO、Transformer:目标检测的技术革新
从滑动窗口到YOLO、Transformer:目标检测的技术革新
399 0
|
XML SQL 缓存
MyBatis Mapper 接口方法执行原理分析
前言 通过前面入门 MyBatis 的文章《MyBatis 初探,使用 MyBatis 简化数据库操作(超详细)》,我们已经对 MyBatis 有了一定了解。
613 0