Questions tagged [annotations]

In programming, annotations are used to add information to a code element which cannot be expressed by the type system.

In programming, annotations are used to add information to a code element which cannot be expressed by the type system.

Java annotations

Up to annotations were only usable inside comments and were used for denoting special information like the author of a class or method or references to other entities.

In some cases they were also used for code generation, by facilitating the toolchain.

As these annotations were part of the code, it was not possible to use them in any way at run time since they were not part of the byte code.

With , annotations became a proper part of java syntax. Annotations can be defined using a syntax similar to the definition of interfaces. They can be used to annotate classes, methods, fields, parameters and packages.

Depending on the definition an annotation is available in source code, byte code or run time. Therefore they can be used for code generation, byte code manipulation at class loading time and via reflection at run time.

For info more see Wiki page and docs.oracle.com

C# Attributes

Attributes are a similar concept to Java annotations, they provide a powerful method of associating declarative information with C# code (types, methods, properties, and so forth). Once associated with a program entity, the attribute can be queried at run time and used in any number of ways.

13026 questions
125
votes
5 answers

Init method in Spring Controller (annotation version)

I'm converting a controller to the newer annotation version. In the old version I used to specify the init method in springmvc-servlet.xml using: How can I specify the init…
Krt_Malta
  • 9,265
  • 18
  • 53
  • 91
125
votes
5 answers

Name attribute in @Entity and @Table

I have a doubt, because name attribute is there in both @Entity and @Table For example, I'm allowed to have same value for name attribute @Entity(name = "someThing") @Table(name = "someThing") and I can have different names as well for same class …
user2728475
123
votes
12 answers

annotation to make a private method public only for test classes

Who has a solution for that common need. I have a class in my application. some methods are public, as they are part of the api, and some are private, as they for internal use of making the internal flow more readable now, say I want to write a unit…
Kumetix
  • 1,231
  • 2
  • 8
  • 3
120
votes
5 answers

can someone please explain me @MapsId in hibernate?

Can someone please explain to me @MapsId in hibernate? I'm having a hard time understanding it. It would be great if one could explain it with an example and in what kind of use cases is it most applicable?
brainydexter
  • 19,826
  • 28
  • 77
  • 115
120
votes
9 answers

Java Annotations

What is the purpose of annotations in Java? I have this fuzzy idea of them as somewhere in between a comment and actual code. Do they affect the program at run time? What are their typical usages? Are they unique to Java? Is there a C++ equivalent?
Lehane
  • 47,588
  • 14
  • 53
  • 53
119
votes
2 answers

Why java classes do not inherit annotations from implemented interfaces?

I'm using a Dependency Injection framework (Guice's AOP to intercept some method calls specifically). My class implements an interface and I would like to annotate the interface methods so the framework could select the right methods. Even if the…
Boris Pavlović
  • 63,078
  • 28
  • 122
  • 148
117
votes
23 answers

@Autowired - No qualifying bean of type found for dependency

I've started my project by creating entities, services and JUnit tests for services using Spring and Hibernate. All of this works great. Then I've added spring-mvc to make this web application using many different step-by-step tutorials, but when…
Radzikowski
  • 2,377
  • 4
  • 27
  • 39
116
votes
7 answers

Neither BindingResult nor plain target object for bean name available as request attribute

I'm just learning the ropes of Spring 3's annotation fu and I've stumbled upon the newb's nightmare exception. Would appreciate any help. here's the form jsp code: .... …
KG -
  • 7,130
  • 12
  • 56
  • 72
115
votes
6 answers

How to use @Parcelize now that kotlin-android-extensions is being deprecated?

How do I replace annotation class Parcelize from package kotlinx.android.parcel with @Parcelize which is not coming from the kotlin-android-extensions plugin?
vepzfe
  • 4,217
  • 5
  • 26
  • 46
115
votes
12 answers

Do I need elements in persistence.xml?

I have very simple persistance.xml file:
Michał Mech
  • 2,035
  • 4
  • 17
  • 26
113
votes
4 answers

Meaning of Android Studio error: Not annotated parameter overrides @NonNull parameter

I'm trying out Android Studio. Upon creating a new project and adding a default onSaveInstanceState method to the create MyActivity class, when I try to commit the code to Git, I get a strange error I don't understand. The code is this: The error I…
Monomo
  • 1,193
  • 2
  • 7
  • 5
113
votes
4 answers

@Transactional(propagation=Propagation.REQUIRED)

if some one can explain what this annotation do and when exactly we use it : @Transactional(propagation=Propagation.REQUIRED) Thanks
Adil
  • 4,503
  • 10
  • 46
  • 63
110
votes
9 answers

How to scan multiple paths using the @ComponentScan annotation?

I'm using Spring 3.1 and bootstrapping an application using the @Configuration and @ComponentScan attributes. The actual start is done with new AnnotationConfigApplicationContext(MyRootConfigurationClass.class); This Configuration class is…
Programming Guy
  • 7,259
  • 11
  • 50
  • 59
107
votes
1 answer

How to annotate a type that's a class object (instead of a class instance)?

What is the proper way to annotate a function argument that expects a class object instead of an instance of that class? In the example below, some_class argument is expected to be a type instance (which is a class), but the problem here is that…
Gomes J. A.
  • 1,235
  • 2
  • 9
  • 9
107
votes
6 answers

How do annotations like @Override work internally in Java?

Can anybody explain to me how annotations work internally in java? I know how we can create custom annotations by using java.lang.annotation library in java. But I still don't get how it's working internally for example, the @Override annotation. I…
Chirag Dasani
  • 1,205
  • 2
  • 10
  • 11