Spring @Required Annotation deprecated?

Viewed 807

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?

2 Answers

Just use @Autowired.

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

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;
   }
Related