2

I am trying to add @Required Annotation to my bean but complier says it's deprecated.

public class Product {
    private String id;
    
    public String getId() {
        return id;
    }

    @Required
    public void setId(String id) {
        this.id = id;
    }
}

Is there a different annotation for this?

3 Answers 3

3

Read the documentation:

Deprecated as of 5.1, in favor of using constructor injection for required settings (or a custom InitializingBean implementation).

public class Product {
   private String id;

   public Product(String id) {
      this.id = id;
   }

   public String getId() {
      return id;
   }
2

Just use @Autowired.

@Autowired
public void setId(String id) {
    this.id = id;
}
0

One of the best answer i got is this :

@Autowired(required = true)
private String yourVariable;

if you set it required to false your String value will be null and you won't get an error

if you just typed @Autowired, then it is required = true by default, try these to understand and read console error messages

Not the answer you're looking for? Browse other questions tagged or ask your own question.