문제 상황
@Resource 애너테이션을 사용하려 했더니 Resource cannot be resolved to a type 문구가 떴다.
방법 1. pom.xml에 dependency 추가 (둘 중 택1)
1-1. pom.xml 에 javax.annotation dependency 추가
<!-- https://mvnrepository.com/artifact/javax.annotation/javax.annotation-api -->
<dependency>
<groupId>javax.annotation</groupId>
<artifactId>javax.annotation-api</artifactId>
<version>1.3.2</version>
</dependency>
OR
1-2. pom.xml 에 tomcat-annotations-api dependency 추가
* <version> : 설치된 톰캣 버전에 맞추기
<!-- https://mvnrepository.com/artifact/org.apache.tomcat/tomcat-annotations-api -->
<dependency>
<groupId>org.apache.tomcat</groupId>
<artifactId>tomcat-annotations-api</artifactId>
<version>8.5.73</version>
</dependency>
방법 2. Apache Tomcat 라이브러리 추가
1. 해당 프로젝트 우클릭 🠒 Build Path 🠒 Configure Build Path... 클릭
2. Java Build Path 🠒 Libraries 🠒 Classpath 선택 🠒 Add Library 클릭
3. Server Runtime 선택 후 Next 🠒 Apache Tomcat 선택 후 Finish
4. Apache Tomcat 라이브러리 추가 확인 후 Apply and Close 클릭
5. javax.annotation.Resource import 하기
Resource annotation의 위치
@Resource는 annotaions-api의 javax.annotation 패키지 내에 있기 때문에 pom.xml 파일에 따로 dependency를 추가하거나, 톰캣 라이브러리를 추가해야 사용 가능하다. 톰캣 라이브러리를 추가했을 경우, 해당 라이브러리에서 Resource.class를 확인할 수 있다.