Spring注解-(七)@Resource的用法

本文最后更新于:June 14, 2022 am

Spring 是目前主流的 Java Web 开发框架,是 Java 世界最为成功的框架。Spring框架是由于软件开发的复杂性而创建的。Spring使用的是基本的JavaBean来完成以前只可能由EJB完成的事情。然而,Spring的用途不仅仅限于服务器端的开发。从简单性、可测试性和松耦合性角度而言,绝大部分Java应用都可以从Spring中受益。该框架是一个轻量级的开源框架,具有很高的凝聚力和吸引力。Spring 框架不局限于服务器端的开发。从简单性、可测试性和松耦合的角度而言,任何 Java 应用都可以从 Spring 中受益。Spring 框架还是一个超级粘合平台,除了自己提供功能外,还提供粘合其他技术和框架的能力。

目录

@Resource默认根据名称进行注入,如果没有找到名称则根据类型进行注入。如下:

目标Bean类:

1
2
3
4
5
6
7
8
9
10
11
12
13
package com.SpringTestAnnotation.TestValue;

import org.springframework.stereotype.Component;

/**
* @Author DragonOne
* @Date 2022/6/10 21:56
* @墨水记忆 www.tothefor.com
*/
@Component
public class OrderS {
}

使用:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
package com.SpringTestAnnotation.TestValue;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

/**
* @Author DragonOne
* @Date 2022/6/10 11:16
* @墨水记忆 www.tothefor.com
*/
@Component
public class Per {

@Resource
private OrderS orderS1;

public void show(){
System.out.println("Per show "+orderS1);
}

}

根据名称orderS1进行注入,但容器并没有名称为orderS1的Bean,所以会再根据类型OrderS进行注入。

测试:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
package com.SpringTestAnnotation;

import com.SpringTestAnnotation.TestValue.Per;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;

/**
* @Author DragonOne
* @Date 2022/6/10 11:16
* @墨水记忆 www.tothefor.com
*/
public class TestAnnotation {
public static void main(String[] args) {
AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext(TestConfig.class);
Per per = applicationContext.getBean("per", Per.class);
per.show();
}
}

输出:

1
Per show com.SpringTestAnnotation.TestValue.OrderS@319b92f3

这是@Resource默认的注入方式。但是,如果就要求只根据名称进行注入,如果没有对应的名称就注入失败怎么办?可以通过指定name来实现,见下。

名称注入

可以通过指定name来实现,如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
package com.SpringTestAnnotation.TestValue;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

/**
* @Author DragonOne
* @Date 2022/6/10 11:16
* @墨水记忆 www.tothefor.com
*/
@Component
public class Per {

@Resource(name = "orderS123") //根据名称orderS123进行注入,没有则注入失败
private OrderS orderS1;

public void show(){
System.out.println("Per show "+orderS1);
}

}

将会通过名称orderS123进行注入,如果没有找到名称为orderS123的Bean,则注入失败;而不会再根据类型进行注入,报错如下:

1
警告: Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'per': Injection of resource dependencies failed; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'orderS123' available

报错内容表示没有名称为orderS123的Bean。

总结

  • 默认情况下,@Resource会先根据名称进行注入,如果没有名称则会再根据类型进行注入。
  • 如果指定了@Resource的name属性(@Resource(name = “xxxx”)),则只会根据指定的名称进行注入,如果没有则报错。
  • JDK自带注解。