1,851 questions
497
votes
27
answers
986k
views
When do you use Java's @Override annotation and why?
What are the best practices for using Java's @Override annotation and why?
It seems like it would be overkill to mark every single overridden method with the @Override annotation. Are there ...
2645
votes
30
answers
1.2m
views
What's the difference between @Component, @Repository & @Service annotations in Spring?
Can @Component, @Repository, and @Service annotations be used interchangeably in Spring or do they provide any particular functionality besides acting as a notation device?
In other words, if I have a ...
1401
votes
26
answers
467k
views
Which @NotNull Java annotation should I use? [closed]
I'm looking to make my code more readable as well as use tooling like IDE code inspection and/or static code analysis (FindBugs and Sonar) to avoid NullPointerExceptions. Many of the tools seem ...
95
votes
6
answers
134k
views
ggplot2 - annotate outside of plot
I would like to associate sample size values with points on a plot. I can use geom_text to position the numbers near the points, but this is messy. It would be much cleaner to line them up along the ...
302
votes
12
answers
238k
views
Scanning Java annotations at runtime
How do I search the whole classpath for an annotated class?
I'm doing a library and I want to allow the users to annotate their classes, so when the Web application starts I need to scan the whole ...
286
votes
8
answers
206k
views
Does Spring @Transactional attribute work on a private method?
If I have a @Transactional annotation on a private method in a Spring bean, does the annotation have any effect?
If the @Transactional annotation is on a public method, it works and opens a ...
724
votes
15
answers
357k
views
Difference between <context:annotation-config> and <context:component-scan>
I'm learning Spring 3 and I don't seem to grasp the functionality behind <context:annotation-config> and <context:component-scan>.
From what I've read they seem to handle different ...
600
votes
19
answers
286k
views
Where does the @Transactional annotation belong?
Should you place the @Transactional in the DAO classes and/or their methods or is it better to annotate the Service classes that are using the DAO objects? Or does it make sense to annotate both ...
751
votes
17
answers
515k
views
Spring: @Component versus @Bean
I understand that @Component annotation was introduced in Spring 2.5 to get rid of XML bean definition by using classpath scanning.
@Bean was introduced in spring 3.0 and can be used with @...
305
votes
25
answers
326k
views
Injecting Mockito mocks into a Spring bean
I would like to inject a Mockito mock object into a Spring (3+) bean for the purposes of unit testing with JUnit. My bean dependencies are currently injected by using the @Autowired annotation on ...
195
votes
6
answers
271k
views
How to supply value to an annotation from a Constant java
I am thinking this may not be possible in Java because annotation and its parameters are resolved at compile time. I have an interface as follows,
public interface FieldValues {
String[] FIELD1 = ...
156
votes
22
answers
134k
views
Hibernate Annotations - Which is better, field or property access?
This question is somewhat related to Hibernate Annotation Placement Question.
But I want to know which is better? Access via properties or access via fields?
What are the advantages and disadvantages ...
117
votes
12
answers
169k
views
Do I need <class> elements in persistence.xml?
I have very simple persistance.xml file:
<?xml version="1.0" encoding="UTF-8"?>
<persistence version="1.0"
xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/...
459
votes
10
answers
503k
views
Only using @JsonIgnore during serialization, but not deserialization
I have a user object that is sent to and from the server. When I send out the user object, I don't want to send the hashed password to the client. So, I added @JsonIgnore on the password property, but ...
164
votes
5
answers
428k
views
@RequestBody and @ResponseBody annotations in Spring
Can someone explain the @RequestBody and @ResponseBody annotations in Spring 3? What are they for? Any examples would be great.
251
votes
8
answers
103k
views
Why is it not possible to extend annotations in Java?
I don't understand why there is no inheritance in Java annotations, just as Java classes. I think it would be very useful.
For example: I want to know if a given annotation is a validator. With ...
532
votes
8
answers
411k
views
Difference between @Before, @BeforeClass, @BeforeEach and @BeforeAll
What is the main difference between
@Before and @BeforeClass
and in JUnit 5 @BeforeEach and @BeforeAll
@After and @AfterClass
According to the JUnit Api @Before is used in the following case:
When ...
123
votes
2
answers
72k
views
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 ...
454
votes
11
answers
305k
views
@Resource vs @Autowired
Which annotation, @Resource (jsr250) or @Autowired (Spring-specific) should I use in DI?
I have successfully used both in the past, @Resource(name="blah") and @Autowired @Qualifier("blah")
My ...
136
votes
16
answers
84k
views
Xml configuration versus Annotation based configuration [closed]
In a few large projects i have been working on lately it seems to become increasingly important to choose one or the other (XML or Annotation). As projects grow, consistency is very important for ...
118
votes
9
answers
218k
views
How to inject a Map using the @Value Spring Annotation?
How can I inject values into a Map from the properties file using the @Value annotation in Spring?
My Spring Java class is and I tried using the $, but got the following error message:
Could not ...
62
votes
3
answers
88k
views
@WebServlet annotation with Tomcat 7 [duplicate]
In my application, I had a servlet which was defined like this in the web.xml:
<servlet>
<display-name>Notification Servlet</display-name>
<servlet-name>...
9
votes
1
answer
6k
views
Emulate annotation inheritance for interfaces and methods with AspectJ
Often people ask AspectJ questions like this one, so I want to answer it in a place I can easily link to later.
I have this marker annotation:
package de.scrum_master.app;
import java.lang....
34
votes
3
answers
65k
views
Java Annotations values provided in dynamic manner
I want to provide annotations with some values generated by some methods.
I tried this so far:
public @interface MyInterface {
String aString();
}
@MyInterface(aString = MyClass....
394
votes
8
answers
486k
views
Why does JPA have a @Transient annotation?
Java has the transient keyword. Why does JPA have @Transient instead of simply using the already existing java keyword?
136
votes
10
answers
229k
views
Is it possible to read the value of a annotation in java?
this is my code:
@Column(columnName="firstname")
private String firstName;
@Column(columnName="lastname")
private String lastName;
public String getFirstName() {
return ...
311
votes
20
answers
590k
views
Setting default values for columns in JPA
Is it possible to set a default value for columns in JPA, and if, how is it done using annotations?
237
votes
13
answers
115k
views
How and where are Annotations used in Java?
What are the major areas that we can use Annotations? Is the feature a replacement for XML based configuration?
143
votes
11
answers
331k
views
How to annotate MYSQL autoincrement field with JPA annotations
Straight to the point, problem is saving the object Operator into MySQL DB.
Prior to save, I try to select from this table and it works, so is connection to db.
Here is my Operator object:
@Entity
...
63
votes
10
answers
64k
views
What is the use of marker interfaces in Java?
When there is nothing to implement in the marker interfaces like Serializable What is the use of implementing it?
505
votes
15
answers
250k
views
Should we @Override an interface's method implementation?
Should a method that implements an interface method be annotated with @Override?
The javadoc of the Override annotation says:
Indicates that a method declaration is intended to override a method ...
39
votes
1
answer
8k
views
What is the purpose of @NamedArg annotation in javaFX 8?
I would like to know what is the use case of @NamedArg annotation in JavaFX 8
The javadoc does not give us more details,
Javadoc : Annotation that provides information about argument's name.
And no ...
481
votes
14
answers
501k
views
JsonMappingException: No suitable constructor found for type [simple type, class ]: can not instantiate from JSON object
I am getting the following error when trying to get a JSON request and process it:
org.codehaus.jackson.map.JsonMappingException: No suitable constructor found for type [simple type, class com.myweb....
311
votes
4
answers
145k
views
Which types can be used for Java annotation members?
Today I wanted to create my first annotation interface following this documentation and I got this compiler error
Invalid type for annotation member":
public @interface MyAnnotation {
Object ...
198
votes
4
answers
65k
views
How does lombok work?
I met lombok today.
I'm very anxious to know how it works.
A Java Geek Article gives some clues but it's not perfectly clear to me:
Java 6 removes apt and make javac able
to manage annotations, ...
86
votes
4
answers
85k
views
Adding Java Annotations at Runtime
Is it possible to add an annotation to an object (in my case in particular, a Method) at runtime?
For a bit more explanation: I have two modules, moduleA and moduleB. moduleB depends upon moduleA, ...
215
votes
5
answers
78k
views
How do different retention policies affect my annotations?
Can anyone explain in a clear way the practical differences between the java.lang.annotation.RetentionPolicy constants SOURCE, CLASS, and RUNTIME?
I'm also not exactly sure what the phrase "retaining ...
119
votes
7
answers
448k
views
Neither BindingResult nor plain target object for bean name available as request attribute [duplicate]
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:
<form:form method="POST" ...
109
votes
2
answers
260k
views
Get rid of "The value for annotation attribute must be a constant expression" message [duplicate]
I use annotation in my code, and I try to use value which determine in run time.
I define my list as static final (lst), and I add to this list some elements.
When I use lst.get(i), I get ...
4
votes
1
answer
7k
views
JFreechart series tool tip above shape annotation
I have an XYPlot on which are series and a couple of dynamically added shape annotations with no fill (hence each of the series points are visible). Is it possible to display the series tool tips(that ...
138
votes
10
answers
107k
views
Why does Eclipse complain about @Override on interface methods?
I have an existing project that uses @Override on methods that override interface methods, rather than superclass methods. I cannot alter this in code, but I would like Eclpse to stop complaining ...
161
votes
5
answers
146k
views
Is there something like Annotation Inheritance in java?
I'm exploring annotations and came to a point where some annotations seems to have a hierarchy among them.
I'm using annotations to generate code in the background for Cards. There are different ...
61
votes
5
answers
125k
views
How to map JSON field names to different object field names?
What is the equiv way in Jackson json annotation for the following jax-b annotations?
I need to produce json rather than xml and need to know the conventional jackson annotations that is equivalently ...
176
votes
7
answers
143k
views
Which annotation should I use: @IdClass or @EmbeddedId
The JPA (Java Persistence API) specification has 2 different ways to specify entity composite keys: @IdClass and @EmbeddedId.
I'm using both annotations on my mapped entities, but it turns out to be ...
55
votes
8
answers
42k
views
Why is javac failing on @Override annotation
Eclipse is adding @Override annotations when I implement methods of an interface. Eclipse seems to have no problem with this. And our automated build process from Cruise Control seems to have no ...
43
votes
3
answers
82k
views
@ModelAttribute annotation, when to use it?
Lets say we have an entity Person, a controller PersonController and an edit.jsp page (creating a new or editing an existing person)
Controller
@RequestMapping(value = "/edit", method = ...
40
votes
2
answers
31k
views
How to mix inheritance strategies with JPA annotations and Hibernate?
According to the Hibernate Reference Documentation it should be possible to mix different inheritance mapping strategies when using Hibernate's XML-Metadata:
http://docs.jboss.org/hibernate/stable/...
33
votes
11
answers
65k
views
hibernate column name issues
@Column(name="DateOfBirth")
private Date dateOfBirth;
I specifically need the above code to create a column named "DateOfBirth," instead Hibernate gives me a column named date_of_birth. How can I ...
24
votes
2
answers
62k
views
Hibernate : How override an attribute from mapped super class
The generic entity, super class:
@MappedSuperclass
public abstract class GenericEntity {
private Integer id;
public Integer getId() {return id;}
public void setId(Integer id) {this.id = ...
108
votes
3
answers
16k
views
Why doesn't a missing annotation cause a ClassNotFoundException at runtime?
Consider the following code:
A.java:
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
@Retention(RetentionPolicy.RUNTIME)
@interface A{}
C.java:
import java.util....