Iffo
Iffo

Reputation: 353

Spring @Autowired on all setter in a class

Is possible to specify that all setter should be autowired with one annotation?

This is my class:

@Component
public class MyClass {

    private static Bean1 bean1;
    //...
    private static BeanN beanN;

    public static Bean1 getBean1() {
        return bean1;
    }
    @Autowired
    public void setBean1(Bean1 bean1) {
        MyClass.bean1 = bean1;
    }
    //...
    public static BeanN getBeanN() {
        return beanN;
    }
    @Autowired
    public void setBeanN(BeanN beanN) {
        MyClass.beanN = beanN;
    }
}

Upvotes: 0

Views: 249

Answers (1)

Sotirios Delimanolis
Sotirios Delimanolis

Reputation: 279880

No. There is no such built-in annotation. Also, Spring doesn't care that your method is to be interpreted as a bean mutator (a setter). Any method can be annotated with @Autowired and Spring will try to invoke it with the appropriate arguments.


Since the whole point of Spring is dependency injection, there's no reason for you to have static fields. Just inject the bean where you need it.

Upvotes: 3

Related Questions