@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

目录
相关文章
|
3月前
|
Java Spring
spring注解@Autowired、@Resource说明
spring注解@Autowired、@Resource说明
|
7月前
|
Java 编译器 Spring
Spring框架@Autowired和@Resource到底有什么区别
Spring框架@Autowired和@Resource到底有什么区别
356 0
|
8月前
|
Java Spring
解析Spring注解:@Resource与@Autowired的区别
在Spring框架中,依赖注入是实现松耦合、可维护性高的应用的重要方式之一。`@Resource`和`@Autowired`是两个常用的注解,用于实现依赖注入。然而,它们在用法和特点上有所不同。本文将详细介绍`@Resource`和`@Autowired`之间的区别,以及在Spring应用中的应用场景。
119 0
|
Java 测试技术 开发者
Spring探索丨既生@Resource,何生@Autowired?
读了本文你将会了解到:1、@Resource和@Autowired来源;2、Spring官方为什么会支持这两个功能如此相似的注解?3、为什么@Autowired属性注入的时候Idea会曝出黄色的警告?4、@Resource和@Autowired推荐用法
483 1
Spring探索丨既生@Resource,何生@Autowired?
|
Java 测试技术 开发者
Spring探索:既生@Resource,何生@Autowired?
提到Spring依赖注入,大家最先想到应该是@Resource和@Autowired,很多文章只是讲解了功能上的区别,对于Spring为什么要支持两个这么类似的注解却未提到,属于知其然而不知其所以然。不知大家在使用这两个注解的时候有没有想过,@Resource又支持名字又支持类型,还要@Autowired干嘛,难道是Spring官方没事做了?
13574 3
Spring探索:既生@Resource,何生@Autowired?
|
开发框架 Java Spring
@Autowired与@Resource有何区别?
@Autowired与@Resource有何区别?
75 0
|
开发框架 Java Spring
spring注解:@Autowired 和@Resource
spring注解:@Autowired 和@Resource
276 0
|
开发框架 Java Spring
Spring中@Autowired和@Resource的区别及详细使用
Spring中@Autowired和@Resource的区别及详细使用
|
Java Spring 容器
Spring中的Autowired、Qualifier、Resource注解详解
使用Spring系列的框架对这三个注解肯定都不会陌生,这三个注解有一个特性,就是用于属性注入,说白了点就是将Spring容器中的对象取出来,这样我们才可以使用,那么这三者到底是什么关系,又有什么区别呢?
469 0
|
Java 程序员 开发者
Spring 中 @Autowired 和 @Resource 有什么区别?
背景 做为一名 Java 程序员,日常开发中使用最多的便是 Spring,工作了很多年,很多人都停留在使用的层面上,甚至连最基本的概念都没搞懂。笔者在 Java 领域也辛勤耕耘了几年,为了避免浮于表面,在今年6月份开始看 Spring 的源码,其优秀的设计确实值得每一个 Java 开发者去学习。
181 0