当前Spring版本为5.1.5
1.声明
当前内容主要用于本人复习@Resource注解,以及理解其作用
Spring官方介绍:一个首先按照修饰的字段的名称进行匹配,最后按照字段的类型进行匹配
,效果和@Autowired差不多
2.测试@Resource注解
applicationContext.xml文件
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
<!-- 开启注解配置 -->
<context:annotation-config/>
<context:component-scan base-package="com.hy.java.spring.annotation.resource"/>
</beans>
BeanConfig .java
@Configuration
public class BeanConfig {
@Bean("movieFinder")
public MovieFinder getMovieFinder() {
MovieFinder movieFinder = new MovieFinder();
movieFinder.setId(11111);
return movieFinder;
}
@Bean("movieFinder2")
public MovieFinder getMovieFinder2() {
MovieFinder movieFinder = new MovieFinder();
movieFinder.setId(22222);
return movieFinder;
}
}
MovieFinder .java
public class MovieFinder {
private int id;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public void find() {
System.out.println(this.id + ":current movie finder find a moviee ....");
}
}
SimpleMovieLister .java
@Component
public class SimpleMovieLister {
private MovieFinder movie;
public MovieFinder getMovie() {
return movie;
}
//@Resource// 首先按照名称查找,最后按照类型查找
@Resource(name = "movieFinder2") // 按照名称方式查找
public void setMovie(MovieFinder movie) {
this.movie = movie;
}
}
ResourceAnnotationTest .java
/**
* @description 当前内容主要用于测试当前的@Resource注解的使用
* @author admin
*
*/
public class ResourceAnnotationTest {
public static void main(String[] args) {
String xml = "/com/hy/java/spring/annotation/resource/applicationContext.xml";
ApplicationContext context = new ClassPathXmlApplicationContext(xml);
SimpleMovieLister simpleMovieLister = context.getBean("simpleMovieLister", SimpleMovieLister.class);
simpleMovieLister.getMovie().find();
}
}
1.只匹配名称相同、类型相同:(通过name="beanName"方式实现!)
直接运行ResourceAnnotationTest 结果显示:
22222:current movie finder find a moviee …
2.如果不指定@Resource中的name那么直接报错,直接修改SimpleMovieLister .java
文件
SimpleMovieLister .java
@Component
public class SimpleMovieLister {
private MovieFinder movie;
public MovieFinder getMovie() {
return movie;
}
@Resource// 首先按照名称查找,最后按照类型查找
public void setMovie(MovieFinder movie) {
this.movie = movie;
}
}
运行结果:(报错)
No qualifying bean of type ‘com.hy.java.spring.annotation.resource.MovieFinder’ available: expected single matching bean but found 2: movieFinder,movieFinder2
名称为找到,结果发现两个类型,所以无法注入…
3.按照类型注入:2中保持不变,直接修改BeanConfig.java中的内容
BeanConfig .java
@Configuration
public class BeanConfig {
@Bean("movieFinder")
public MovieFinder getMovieFinder() {
MovieFinder movieFinder = new MovieFinder();
movieFinder.setId(11111);
return movieFinder;
}
}
结果:
11111:current movie finder find a moviee …
4.按照修饰的属性的名称方式注入,直接保持修饰的属性名称和@Bean中的beanName一致即可
3.总结
- @Resource注解,可以实现按照名称方式注入,一般按照修饰的属性名称方式注入
- 如果不能找到直接按照类型匹配,如果有多个同类型的那么报错
- 可以指定按照指定名称匹配,需要设置@Resource的name属性