How do different retention policies affect my annotations?

RetentionPolicy.SOURCE: Discard during the compile. These annotations don’t make any sense after the compile has completed, so they aren’t written to the bytecode. Example: @Override, @SuppressWarnings RetentionPolicy.CLASS: Discard during class load. Useful when doing bytecode-level post-processing. Somewhat surprisingly, this is the default. RetentionPolicy.RUNTIME: Do not discard. The annotation should be available for reflection at runtime. … Read more

codestyle; put javadoc before or after annotation?

Before the annotation, since the annotation is code that “belongs” to the class. See examples with javadoc in the official documentation. Here’s a random example I found in another official Java page: /** * Delete multiple items from the list. * * @deprecated Not for public use. * This method is expected to be retained … Read more

intellij incorrectly saying no beans of type found for autowired repository

I had this same issue when creating a Spring Boot application using their @SpringBootApplication annotation. This annotation represents @Configuration, @EnableAutoConfiguration and @ComponentScan according to the spring reference. As expected, the new annotation worked properly and my application ran smoothly but, Intellij kept complaining about unfulfilled @Autowire dependencies. As soon as I changed back to using … Read more

How and where are Annotations used in Java?

Annotations are meta-meta-objects which can be used to describe other meta-objects. Meta-objects are classes, fields and methods. Asking an object for its meta-object (e.g. anObj.getClass() ) is called introspection. The introspection can go further and we can ask a meta-object what are its annotations (e.g. aClass.getAnnotations). Introspection and annotations belong to what is called reflection … Read more

Why is it not possible to extend annotations in Java?

About the reason why it wasn’t designed that way you can find the answer in the JSR 175 Design FAQ, where it says: Why don’t you support annotation subtyping (where one annotation type extends another)? It complicates the annotation type system, and makes it much more difficult to write “Specific Tools”. … “Specific Tools” — … Read more

Does Spring @Transactional attribute work on a private method?

The answer your question is no – @Transactional will have no effect if used to annotate private methods. The proxy generator will ignore them. This is documented in Spring Manual chapter 10.5.6: Method visibility and @Transactional When using proxies, you should apply the @Transactional annotation only to methods with public visibility. If you do annotate … Read more

Setting default values for columns in JPA

You can do the following: @Column(name=”price”) private double price = 0.0; There! You’ve just used zero as the default value. Note this will serve you if you’re only accessing the database from this application. If other applications also use the database, then you should make this check from the database using Cameron’s columnDefinition annotation attribute, … Read more

Scanning Java annotations at runtime

Use org.springframework.context.annotation.ClassPathScanningCandidateComponentProvider API A component provider that scans the classpath from a base package. It then applies exclude and include filters to the resulting classes to find candidates. ClassPathScanningCandidateComponentProvider scanner = new ClassPathScanningCandidateComponentProvider(<DO_YOU_WANT_TO_USE_DEFALT_FILTER>); scanner.addIncludeFilter(new AnnotationTypeFilter(<TYPE_YOUR_ANNOTATION_HERE>.class)); for (BeanDefinition bd : scanner.findCandidateComponents(<TYPE_YOUR_BASE_PACKAGE_HERE>)) System.out.println(bd.getBeanClassName());