When the JVM loads the annotation class

I found that if I use annotation, the program will not throw a ClassNotFoundException .

 class A { @Transactional public void insert() { //insert something } } 

Tomcat starts successfully without javaee-api-7.0.jar , which contains the javax.transaction.Transactional class

This confuses me a lot, should the JVM ClassNotFoundException when it loads class A ?

+5
source share
1 answer

No, it should not. Annotations are just metadata. He expected the bytecode containing annotations to work fine even if the annotation is not in the classpath. Of course, if some library is trying to actually access the annotation, which is not in the classpath, it will not work.

This is explicitly supported to be able, for example

  • add annotations used by static bytecode static analysis tools such as FindBugs, which are useless when running code in production
  • to be able to use individual JPA clients in the client, even if the server is the only one that actually uses JPA annotations
+5
source

Source: https://habr.com/ru/post/1243287/


All Articles